Skip to content

feat: add cancel_event param and HTTP timeouts to connect()#66

Merged
peterfoldes merged 1 commit intomainfrom
peter/add-cancel-event-and-http-timeouts
Apr 29, 2026
Merged

feat: add cancel_event param and HTTP timeouts to connect()#66
peterfoldes merged 1 commit intomainfrom
peter/add-cancel-event-and-http-timeouts

Conversation

@peterfoldes
Copy link
Copy Markdown
Contributor

Summary

Makes connect() and connect_direct() interruptible from another thread via cancel_event: threading.Event, and adds timeouts to all HTTP/WebSocket calls that previously could hang indefinitely.

Why: The MCP server runs connect() in asyncio.to_thread(). When clients disconnect, task.cancel() can't kill OS threads — zombie threads accumulate until the thread pool is exhausted and all new queries queue forever. With cancel_event, the MCP server can signal cancellation and threads exit within seconds.

Changes:

  • New cancel_event param on connect() and connect_direct() (optional, backward compatible)
  • 30s timeout on requests.post(), requests.get(), and websockets.sync.client.connect(open_timeout=)
  • _check_cancelled() helper called before each network call and in tenacity before_sleep
  • InterfaceError("Connection cancelled by caller") propagates cleanly through exception handlers
  • 9 new tests in tests/test_driver.py

@peterfoldes peterfoldes force-pushed the peter/add-cancel-event-and-http-timeouts branch 3 times, most recently from b414cf1 to e88d773 Compare April 29, 2026 03:08
Make connect() and connect_direct() interruptible from another thread
via a threading.Event. Add 30s timeouts to all HTTP requests and the
WebSocket handshake to prevent indefinite hangs.

This enables callers (e.g. MCP server) to cleanly abort connection
attempts when clients disconnect, preventing zombie thread accumulation
and thread pool exhaustion.
@peterfoldes peterfoldes force-pushed the peter/add-cancel-event-and-http-timeouts branch from e88d773 to 8d3e6ab Compare April 29, 2026 03:10
@peterfoldes peterfoldes marked this pull request as ready for review April 29, 2026 03:11
@peterfoldes peterfoldes requested a review from a team as a code owner April 29, 2026 03:11
@peterfoldes peterfoldes requested review from ClayMav and sfishel18 and removed request for a team April 29, 2026 03:11
@peterfoldes peterfoldes merged commit bf3410c into main Apr 29, 2026
7 checks passed
@peterfoldes peterfoldes deleted the peter/add-cancel-event-and-http-timeouts branch April 29, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants