Skip to content

HttpServletStreamableServerTransportProvider leaves session sweeper thread alive after close #1342

Open

Description

@michel-flux

Description

Since MCP Java SDK 2.1.0, constructing and closing an
HttpServletStreamableServerTransportProvider leaves one live, non-daemon
streamable-http-server-transport-session-sweeper thread. Repeating the
create/close cycle accumulates threads and can prevent the JVM from exiting.
This reproduces without a servlet container or an MCP client.

Versions and result

Tested on JDK 25 with mcp-core and mcp-json-jackson2 at matching versions:

Version Live sweeper threads after one close
2.0.1 0 (sweeper not present)
2.1.0 1, non-daemon
2.2.0 1, non-daemon

The same behavior occurs when the transport is used in a real server: after
two start/stop cycles, two sweeper threads remain and the JVM does not exit.

Minimal reproduction

import com.fasterxml.jackson.databind.ObjectMapper;
import io.modelcontextprotocol.json.jackson2.JacksonMcpJsonMapper;
import io.modelcontextprotocol.server.transport.HttpServletStreamableServerTransportProvider;

public class SessionSweeperShutdownRepro {
    public static void main(String[] args) throws Exception {
        var provider = HttpServletStreamableServerTransportProvider.builder()
                .jsonMapper(new JacksonMcpJsonMapper(new ObjectMapper()))
                .mcpEndpoint("/mcp")
                .build();
        provider.closeGracefully().block();
        Thread.sleep(250);
        var sweepers = Thread.getAllStackTraces().keySet().stream()
                .filter(Thread::isAlive)
                .filter(thread -> thread.getName().startsWith(
                        "streamable-http-server-transport-session-sweeper"))
                .toList();
        System.out.println("Remaining sweepers: " + sweepers.size());
        sweepers.forEach(thread -> System.out.println(
                thread.getName() + " daemon=" + thread.isDaemon()));
    }
}

Expected behavior

closeGracefully().block() (and destroy()) should stop transport-owned
background work. Repeated create/close cycles should not accumulate threads,
and the JVM should be able to exit normally. Session eviction must continue
to work while the provider is running.

Likely cause and suggested fix

startSessionSweeper() creates a dedicated Schedulers.newSingle(...) for
Flux.interval(...), but retains only the Flux subscription Disposable.
closeGracefully() disposes the subscription, not the scheduler, leaving its
non-daemon thread alive. destroy() delegates to the same close path.

Please retain ownership of the Scheduler and dispose it during normal close
and failed-start cleanup, alongside the subscription. Cleanup should be
idempotent. A regression test should cover repeated create/close cycles,
thread termination, continued idle-session eviction, and active-stream close.
Disabling session sweeping or merely making its thread daemon would conceal
the lifecycle bug and/or undo idle-session cleanup.

Related: #1130 introduced session sweeping. This is distinct from the
HttpClient SelectorManager leak in #620 and the disconnected-client socket
issue in #1021.

Activity

  1. ChaseMoon233 commented on Nov 28, 2026

    @ChaseMoon233

    Confirming the root cause from the code: startSessionSweeper() retains only the Flux subscription's Disposable, not the dedicated Schedulers.newSingle(...) behind it, so closeGracefully() (and destroy(), which delegates to it) disposes the subscription but leaves the scheduler's non-daemon thread running.

    The fix I'd propose: keep a reference to the Scheduler next to the subscription and dispose it on close and on failed-start cleanup, idempotently, plus a regression test covering repeated create/close cycles and thread termination.

    Happy to pick this up if a maintainer is OK with it.

  2. ChaseMoon233 commented on Nov 28, 2026

    @ChaseMoon233

    Following up: I've reproduced the leak locally and have a fix verified with regression tests.

    The root cause is as described in the issue: startSessionSweeper() keeps only the subscription Disposable, while the dedicated Schedulers.newSingle(...) behind it is never disposed, so its non-daemon thread (streamable-http-server-transport-session-sweeper) survives closeGracefully() and destroy().

    Fix approach: retain the Scheduler in a field next to the subscription and dispose it on close (idempotently, after disposing the subscription), and clean up both schedulers if sweeper startup fails in the constructor.

    Verification (JDK 17, baseline 6c0a020):

    • New regression tests fail before the fix: 1 sweeper thread still alive after closeGracefully(); 3 leaked threads after 3 create/close cycles.
    • After the fix: HttpServletStreamableServerTransportProviderTests 3/3 and KeepAliveSchedulerTests 12/12 pass, and spring-javaformat:validate passes.

    Happy to open a PR as soon as this issue is assigned to me, per the repo's intake rules.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions