False positive in rule squid:S2142 *"InterruptedException" should not be ignored*

  • Versions:
    ** Sonarqube 7.9.1 (build 27448),
    ** Gradle Sonar plugin id “org.sonarqube” version "2.8"
  • Description:
    When calling Thread.sleep(...) you need to handle InterruptedException. Catching it will cause the above mentioned rule to raise an issue that describes to rethrow or call Thread.currentThread().interrupt().
    As any part in a try-catch-block should be a oneliner, exception handling and the proposed call are within a called method. But the rule still raises an issue.
  • Example:
private static void waitForNextExecution(Set<Task> running, LongSupplier waitTimeoutMillis) {
  try {
    Thread.sleep(waitTimeoutMillis.get());
  } catch (InterruptedException e) {
    cancelAllSubTasksAndInterrupt(running);
  }
}

private static void cancelAllSubTasksAndInterrupt(Set<Task> subTasks) {
  for (Task task : subTasks) {
    String key = task.getName();
    LOG.info("--- waitForNextExecution: Service interrupted. Cancel execution of task {}.", key);
    task.cancel(true);
  }
  Thread.currentThread().interrupt();
}

Hello @tsukasa,

Sorry for the late reply. I had a look into your issue. Yep, this situation should not be reported. And As far as I understand, it’s a limitation, because this check doesn’t go into the called methods.

I’ve created a ticket for it:

Regards,
Margarita