feat: add cancel_event param and HTTP timeouts to connect()#66
Merged
peterfoldes merged 1 commit intomainfrom Apr 29, 2026
Merged
feat: add cancel_event param and HTTP timeouts to connect()#66peterfoldes merged 1 commit intomainfrom
peterfoldes merged 1 commit intomainfrom
Conversation
b414cf1 to
e88d773
Compare
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.
e88d773 to
8d3e6ab
Compare
ClayMav
approved these changes
Apr 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Makes
connect()andconnect_direct()interruptible from another thread viacancel_event: threading.Event, and adds timeouts to all HTTP/WebSocket calls that previously could hang indefinitely.Why: The MCP server runs
connect()inasyncio.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. Withcancel_event, the MCP server can signal cancellation and threads exit within seconds.Changes:
cancel_eventparam onconnect()andconnect_direct()(optional, backward compatible)requests.post(),requests.get(), andwebsockets.sync.client.connect(open_timeout=)_check_cancelled()helper called before each network call and in tenacitybefore_sleepInterfaceError("Connection cancelled by caller")propagates cleanly through exception handlerstests/test_driver.py