False Negative: Resources Should Be Closed (S2095): Java Executors not identified

Must-share information (formatted with Markdown):

  • SonarQube Developer Edition v8.1 - Java
  • Identify Executors Not Closed
  • Searched Resources

I am quite new to Sonarqube and just installed a new version of Sonarqube to improve quality on an existing Java codebase. A bug occured in some older code in production where a Executor had not been closed in a method;

    final ScheduledExecutorService autoAcknowledgementService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory()
.......
 autoAcknowledgementService.schedule(autoAcknowledgementRunnable, autoAcknowledgeDelay, TimeUnit.MILLISECONDS); //This line was not closed

We looked to see why this had not been picked up with SonarQube analysis and saw that the ‘Resources Should be Closed’ rule was looking for classes that implement the Closable or AutoCloseable interfaces.

Is there a way we can identify Executors that have not been closed?

Thanks

Hi,

Indeed rule S2095 does not cover ScheduledExecutorService class, as it’s about classes implementing Closeable and AutoCloseable (which can be refactored with try-with-resources).

I checked and looks like there is no other rule which would cover your case.