Add InterruptedExceptionHandling recipe (RSPEC-S2142)#838
Merged
Conversation
Implements recipe that inserts Thread.currentThread().interrupt() as the first statement in catch blocks that catch InterruptedException but don't already restore the interrupt flag. Handles both single and multi-catch clauses.
…anceof Refactor hasThreadInterruptCall to use .reduce() for idiomatic early return. In multi-catch blocks, wrap Thread.currentThread().interrupt() in an instanceof check to avoid setting the interrupt flag for non-InterruptedException.
src/main/java/org/openrewrite/staticanalysis/InterruptedExceptionHandling.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/InterruptedExceptionHandling.java
Outdated
Show resolved
Hide resolved
steve-aom-elliott
approved these changes
Mar 20, 2026
Contributor
There was a problem hiding this comment.
I think it looks good other than whether we need to confirm that a Thread.currentThread().interrupt() is actually reachable under current conditions in the catch block:
#838 (comment)
Member
Author
|
Hi thanks! Agree that the presence of a conditional interrupt is likely less than ideal, but then we can only hope the folks knew what they were doing. The case we aim to solve here is where folks likely did not know what they were doing, and had forgotten to call that interrupt, as we've likely all done at some point. 😅 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a recipe that detects catch blocks handling
InterruptedExceptionand ensures they restore the interrupt flag by callingThread.currentThread().interrupt()as the first statement. This addresses SonarQube rule RSPEC-S2142, which requires proper handling of the thread's interrupted state to prevent suppressing interruption signals.Implementation
J.Try.Catchclauses and checks catch parameter type forInterruptedExceptionMethodMatcherto detect existing interrupt callsThread.currentThread().interrupt();as first statement viaJavaTemplatewhen missingTest Coverage
Includes six test cases covering empty catches, existing statements, already-present interrupt calls, multi-catch scenarios, and non-InterruptedException cases.