Sonar detecting not closed ExecutorService

Must-share information (formatted with Markdown):

  • SonarQube Server Enterprise Edition 9.9.9 - zip
  • sonar-scanner for maven 5.1.0.4751 (also with 4.0.0.4121)
  • jdk21 executing sonar-scanner.
  • Project on Java 11

Hi there, I have a problem with a project using Java 11 and ExecutorService. Sonar is rising an issue

Here is the obfuscated code (in order to preserve our customer privacy):

@Override
    public void write(List<? extends SomethingDto> items) {

        [They do things]

        ExecutorService executorService = null;

        try {

            executorService = Executors.newFixedThreadPool(Constants.BATCH_SIZE);

            [They do things]

            CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
        } catch (Exception ex) {
            [They log]
            throw ex;
        } finally {
            [They log]
            if (executorService != null) {
                executorService.shutdown();
            }
        }
    }

Sonar is encouraging us to close ExecutorService or using try-with-resources. In order to use try-with-resources, object must implement AutoCloseable. ExecutorService implements it on Java 19:

Since we are using Java 11, we must close it manually in a finally. We are setting Java version in the pom properties:

<properties>
  <maven.compiler.source>11</maven.compiler.source>
  <maven.compiler.target>11</maven.compiler.target>
</properties>

I think ExecutorService in this case is correctly closed after usage. Is there something I am missing?

Why it keeps us raising this issue?

Hey @mbastardo.excentia

Java 19 came out well after SonarQube v9.9 LTA was released.

Could you try and reproduce the issue with, at a minimum, v2025.1 LTA? Using the latest version of SonarQube for IDE in a supported IDE would also make it clearer if this is still an issue.

Hi, Colin.

Customer is currently at the last steps in updating their complex pipelines in order to upgrade to 2025.1 LTA, so we don’t currently have the possibility to do so.

Is there something we could do yet to check it in 9.9 LTA?

Thank you!

This. :slight_smile:

So sorry, Colin. Read it at a speed!!

But I installed a 2025.3 locally and It worked like a charm :slight_smile:

imagen

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.