HttpServletStreamableServerTransportProvider leaves session sweeper thread alive after close

オープン
#1,342 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
54/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
java
領域
backend, devtools

調査の方向性

Start in the HttpServletStreamableServerTransportProvider implementation, especially startSessionSweeper(), closeGracefully(), and destroy(); trace scheduler ownership and failed-start cleanup. Then inspect the transport lifecycle tests and add coverage for repeated create/close cycles, idle-session eviction, and active-stream close. Done means owned sweeper threads terminate on close while eviction continues during normal operation.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Java
スター
3.8k
フォーク
1.1k
平均マージ
1日 3時間
マージ済み PR(30日)
38

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modelcontextprotocol/java-sdk のほかの issue

modelcontextprotocol/java-sdk の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。