Skip to content

Add InterruptedExceptionHandling recipe (RSPEC-S2142)#838

Merged
timtebeek merged 7 commits intomainfrom
tim/interrupt-in-catch
Mar 20, 2026
Merged

Add InterruptedExceptionHandling recipe (RSPEC-S2142)#838
timtebeek merged 7 commits intomainfrom
tim/interrupt-in-catch

Conversation

@timtebeek
Copy link
Copy Markdown
Member

Summary

Adds a recipe that detects catch blocks handling InterruptedException and ensures they restore the interrupt flag by calling Thread.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

  • Recipe visits J.Try.Catch clauses and checks catch parameter type for InterruptedException
  • Handles both single-catch and multi-catch declarations
  • Uses nested visitor with MethodMatcher to detect existing interrupt calls
  • Inserts Thread.currentThread().interrupt(); as first statement via JavaTemplate when missing

Test Coverage

Includes six test cases covering empty catches, existing statements, already-present interrupt calls, multi-catch scenarios, and non-InterruptedException cases.

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.
Copy link
Copy Markdown
Contributor

@steve-aom-elliott steve-aom-elliott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Mar 20, 2026
@timtebeek
Copy link
Copy Markdown
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. 😅

@timtebeek timtebeek merged commit 3b9b037 into main Mar 20, 2026
1 check passed
@timtebeek timtebeek deleted the tim/interrupt-in-catch branch March 20, 2026 22:00
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants