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

Đang mở Phù hợp với người mới
#3,877 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

  • #3879 của @ar-ramchandra — đã đóng, không merge
  • #3926 của @EnesKartal — đã đóng, không merge

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend, networking

Hướng nghiên cứu

Bắt đầu với src/mcp/client/streamable_http.py:787-793 và kiểm tra cách streamable_http_client xử lý việc hủy trong khối finally, cũng như cách terminate_session gửi yêu cầu DELETE. Bổ sung kiểm thử hồi quy cho trường hợp bị hủy hoặc hết thời gian chờ trong quá trình teardown, sau đó xác minh DELETE đến được máy chủ và nhóm tác vụ bị hủy.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
25k
Fork
4.2k
Merge trung bình
10 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modelcontextprotocol/python-sdk

Tất cả issue của modelcontextprotocol/python-sdk

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.