An InterruptedException is caught and the interrupt is lost

tx0/java:swallowed-interruptrecommendedwarningbug, concurrency

What it finds

catch (InterruptedException e) {} loses the request to stop: catching the exception clears the thread's interrupt status, so the code further up never learns of it and keeps the thread running. Calling Thread.currentThread().interrupt() restores it, and throwing passes it on.

Reported are catch clauses for InterruptedException whose block neither throws nor calls a method named interrupt.

From the rule's source, swallowed_interrupt.rs.

Example from the rule's tests
class Waiter {  void pause(Object lock) {    try {      Thread.sleep(10);    } catch (InterruptedException e) {tx0 reports: the interrupt status is lost here    }    try {      lock.wait();    } catch (InterruptedException e) {      Thread.currentThread().interrupt();    }    try {      Thread.sleep(10);    } catch (java.io.UncheckedIOException | InterruptedException e) {tx0 reports: the interrupt status is lost      System.out.println(e);    }    try {      Thread.sleep(10);    } catch (InterruptedException e) {      throw new IllegalStateException(e);    }  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:swallowed-interrupt" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:swallowed-interrupt] reason="..."

tx0 explain tx0/java:swallowed-interrupt shows the same in the terminal. Found a false positive? Report it.