Skip to content

Add keepalive to responseInputStream timeout scheduler executor to ma…#6756

Open
RanVaknin wants to merge 1 commit intomasterfrom
rvaknin/fix-thread-leak-in-toInputStream
Open

Add keepalive to responseInputStream timeout scheduler executor to ma…#6756
RanVaknin wants to merge 1 commit intomasterfrom
rvaknin/fix-thread-leak-in-toInputStream

Conversation

@RanVaknin
Copy link
Contributor

Addresses: #6567

Background and context

ResponseInputStream uses a static ScheduledExecutorService to abort streams that aren't read within 60 seconds (to prevent connection leaks). The executor's thread never terminates because core threads are kept alive indefinitely by default. This causes the response-input-stream-timeout-scheduler thread to persist for the lifetime of the JVM, even when no streams exist.

To fix this, we can set allowCoreThreadTimeOut(true) and a 60s keep-alive on the executor. The thread now dies after 60 seconds of idleness and respawns on demand (matches TransferManagerConfiguration.java).

Testing

Since there is a timeout and a keep alive mechanism it's hard to write a deterministic test to prove the fix works. Additionally, adding a test with a sleep method of >1min is not feasible.
The following test fails on Master, and passes after the code change introduced in the PR:

    @Test
    void schedulerThread_shouldTerminateAfterIdleTimeout() throws Exception {
        ResponseInputStream<Object> responseInputStream = responseInputStream(Duration.ofSeconds(1));
        responseInputStream.read();
        responseInputStream.close();

        Thread.sleep(65000);

        Set<String> schedulerThreads = new HashSet<>();

        for (Thread thread : Thread.getAllStackTraces().keySet()) {
            if (thread.getName().contains("response-input-stream-timeout-scheduler")) {
                schedulerThreads.add(thread.getName());
            }
        }
        assertThat(schedulerThreads).isEmpty();
    }

@RanVaknin RanVaknin requested a review from a team as a code owner February 28, 2026 00:02
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant