Unshielded session teardown in streamable_http_client aborts DELETE under cancellation, leaking server session

オープン 初心者向け
#3,877 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

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

  • #3879 @ar-ramchandra による — マージされずにクローズ
  • #3926 @EnesKartal による — マージされずにクローズ

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

まず src/mcp/client/streamable_http.py:787-793 から始め、streamable_http_client が finally ブロックでキャンセルをどのように処理するか、また terminate_session が DELETE リクエストをどのように送信するかを確認してください。ティアダウン中のキャンセルまたはタイムアウトに対する回帰テストを追加し、DELETE がサーバーに届き、タスクグループがキャンセルされることを確認してください。

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

説明

bug v1 v2
Release Line

2.x (main branch)

Problem Description

In src/mcp/client/streamable_http.py:787-793, streamable_http_client cleans up the session and cancels the task group in a finally: block:

            try:
                yield read_stream, write_stream
            finally:
                if transport.session_id and terminate_on_close:
                    await transport.terminate_session(client)
                tg.cancel_scope.cancel()

When caller execution exits due to cancellation or timeout (for example, wrapping the client session in anyio.move_on_after(...) or cancelling the enclosing task), the active cancel scope is already cancelled when entering the finally: block.

Because transport.terminate_session(client) performs an asynchronous HTTP DELETE request without a shielded cancel scope:

  1. The DELETE request raises CancelledError (or anyio.get_cancelled_exc_class()) immediately at the first checkpoint.
  2. terminate_session catches Exception, but not BaseException / CancelledError. The exception propagates, aborting the DELETE request before it can reach the server.
  3. The server session is leaked and remains open until remote timeout.
  4. Line 792 (tg.cancel_scope.cancel()) is never executed because the exception unwinds out of the finally block before reaching it.
Expected Behavior

Teardown logic in finally: blocks that issues network calls should be executed inside a shielded cancel scope (with an optional bounded timeout) so that cancellation of the client caller does not prevent sending the DELETE session termination request, nor block task group cancellation:

            finally:
                with anyio.CancelScope(shield=True):
                    if transport.session_id and terminate_on_close:
                        with anyio.move_on_after(5):
                            await transport.terminate_session(client)
                    tg.cancel_scope.cancel()
Environment
  • Python: 3.11+
  • MCP Python SDK: 2.x (main branch)

I would be happy to submit a PR with test coverage if assigned.


Assisted by AI; reviewed and verified.

主要言語
Python
スター
25k
フォーク
4.2k
平均マージ
10分
マージ済み PR(30日)
1

環境構築

はじめの一歩

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

modelcontextprotocol/python-sdk のほかの issue

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

似ている issue

Python の issue をもっと見る

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

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