fix(mcp): close transport on failed/timed-out connections#19200
Merged
kitlangton merged 6 commits intodevfrom Mar 26, 2026
Merged
fix(mcp): close transport on failed/timed-out connections#19200kitlangton merged 6 commits intodevfrom
kitlangton merged 6 commits intodevfrom
Conversation
98e40b4 to
d6ddc5c
Compare
When withTimeout rejects during MCP connect, the transport (and its child process for stdio servers) was never closed. Extract a tryConnect helper that ensures transport.close() is always called on failure, eliminating process/connection leaks in all connect paths. Fixes #19168
Replace the async tryConnect helper with connectTransport using Effect.acquireUseRelease for guaranteed transport cleanup on failure. Convert create() from async function to Effect.gen and defs() to return an Effect. Callers now yield* directly instead of wrapping in Effect.promise.
…fect - fetchFromClient: async function -> Effect.tryPromise + Effect.map - collectFromConnected: compose directly with Effect fetchFromClient - closeClient: Effect.promise(.catch) -> Effect.tryPromise + Effect.ignore - Bus.publish calls: .catch() -> busPublish helper with Effect.tryPromise + Effect.ignore - startAuth: Effect.promise(async try/catch) -> Effect.tryPromise + Effect.catch - finishAuth: simplify inner async/await - create(): eliminate mutable let vars, extract connectRemote/connectLocal - Extract sanitize helper, CreateResult interface, busPublish helper - watch callback: use busPublish via Effect.runPromise - getConfig helper function for repeated Config.get() pattern
Move connectTransport, connectRemote, connectLocal, and create inside the layer's Effect.gen closure where they have direct access to the Bus service instance. Replace busPublish helper with bus.publish calls. Add Bus.layer to defaultLayer.
d6ddc5c to
b3e1bc6
Compare
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
tryConnect()helper that ensurestransport.close()is always called on failure, preventing orphaned child processes (stdio) and leaked HTTP connections (remote)Details
When
withTimeout(client.connect(transport), timeout)rejected increate(), the catch blocks never calledtransport.close(). This left:The fix extracts a
tryConnect(transport, timeout)function that wraps the connect call in try/catch and closes the transport on any error. Both the local and remote connect paths now use this helper, eliminating duplicated cleanup logic.Test plan
transport.close()is called when stdio connect hangs past timeouttransport.close()is called when remote connect hangs past timeoutFixes #19168