Repository navigation
fix(sdk): end gRPC request sides when response trailers arrive - #2356
Merged
Merged
Conversation
A grpc-go server that finishes a bidi RPC while the client request side is still open sends trailers and then RST_STREAM(NO_ERROR). Node, Bun, and Deno then report the complete, still-buffered response as a premature close, so a finished Run, recheck, or cancel failed with TransportError and a non-OK status arrived as Unknown (nodejs/node#66693). The Node transport's session manager now ends each stream's request side as soon as its trailers arrive, before the reset is processed. Genuine resets and errors still fail. Adds deterministic raw-HTTP/2 regression tests, including flow-controlled backpressure and abort cases. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
peppescg
force-pushed
the
fix/1785-h2-premature-close
branch
from
October 15, 2026 13:45
8a94656 to
3a94de7
Compare
Merged
2 tasks
10 of 14 tasks
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
END_STREAM, thenRST_STREAM(NO_ERROR). If the response bytes are still buffered when that reset arrives, Node emitsaborted. Its stream iterator then rejects the complete response withERR_STREAM_PREMATURE_CLOSEinstead of EOF (nodejs/node#66693). Connect surfaces this asUnknown, and the SDK reportsTransportError: The request could not reach the mecatl server, even though the run completed. A real non-OK status sent before such a reset is also masked asUnknown.Converse,RecheckMcpAuthorizationandCancelMcpAuthorization. Any consumer that is not reading when the server completes hits the bug. The CI flake in test(sdk): investigate flaky gRPC MCP authorization premature close #1785 is thecancelContinuation()instance: the SDK awaits the unary cancel while the terminal event, trailers and reset arrive.createNodeTransportends each stream's request side when its response trailers arrive. Node already ends that side when it processes the reset; doing it first stops Node from treating a completed response as aborted. A reset before the trailers, a non-zero reset, or a non-OK status still fail. The same fix applies on Node, Bun, and Deno.Issue relationship
Closes #1785
Type of change
Before | After
Real
mecatl-sdk-authorization-fixture(grpc-go). The SDK consumer pauses 300 ms after starting aRun, or after the firstrecheck()event, before draining it.Run3/3 fail, slowrecheck3/3 failRun: 4/4 failRun: 24/24 failTest plan
Baseline checks
task lint): one pre-existing gosec G703 finding inenvironment/microvm/launch_ownership_darwin.go:754. It is a macOS-only file, unchanged since feat: add microVM execution environments #580; CI lints on Linux and never sees it. All other modules report 0 issues.task test): not run separately;task test:racecovers the same suite.task test:race): every package passes except four tests that fail only on this macOS machine, and fail the same way outside the sandbox:TestConnectUsesSavedServerCAForLazyTLSRPC(macOS x509 verifier),TestMecatedCLICompositionValidatesMicroVMDefaultAfterReadiness(/dev/fdsocket path),TestStartupCompatibilityEmbeddedRejected,TestRunInDirUsesDescriptorAndPassesArgumentsOpaquely(env: bashmissing from the testPATH). This PR changes no Go code.go run ./cmd/mecademo): tool call, permission ask with approval, and final result.task docs): no Markdown changes.task site:build): no user-docs changes.task api:check): engine untouched.TypeScript SDK
tsc --noEmit, unit suite 385/385.task e2e(Node and Bun 1.4.1 matrix): 36/36.task denoon Deno 2.9.3 and 2.9.7: all checks pass.test/node-transport-completion.test.ts, 7 tests. A raw HTTP/2 peer pins grpc-go's frame order. A PING barrier makes the tests deterministic without sleeps. With the stock transport, the first three fail:NO_ERRORreset → EOF;NO_ERRORreset → that status, notUnknown;Canceled, no hang;Canceled, no hang;createNodeTransport:Changes
sdk/typescript/src/node-transport.tsResponseCompletingSessionManagerends a stream's request side on its response trailers;createNodeTransportuses it.sdk/typescript/test/node-transport-completion.test.tsUser-facing change
On the Node, Bun, and Deno gRPC transport, a completed
Run,recheck(), orcancel()no longer fails withTransportError/ premature close when the consumer reads slowly. A server error sent just before such a reset now arrives with its real status.Special notes for reviewers
Why this fix and not a custom response reader. The investigation in #1785 proposed replacing Node's response iterator below Connect (candidate A). I built that variant too and measured both against the same tests:
The custom reader covers two more edge cases, but it takes over body reading for every gRPC call and depends on fine stream-state semantics in Node, Bun, and Deno. Writing it exposed two traps:
end, which hangs a naive reader.We prefer the smaller change now and keep the reader as a follow-up if the edge cases matter in practice.
Known limits of this PR
transport.stream()and starts reading only after the whole response arrived and the stream closed, Node 24.20+/26.3+ (http2: async iteration reports premature close with buffered responses in v26.3.0+ and v24.20.0 nodejs/node#65677) and Bun report a premature close. With a grpc-go server this already fails today on Node 24/26; on Bun it is new with this change. The SDK's high-level and raw clients start reading as soon as headers arrive, so they never reach this state.ERR_STREAM_WRITE_AFTER_ENDon the stream and the read fails. That behaviour is unchanged from today.API compatibility. No wire, proto, or server change. The client half-closes only after the server's
END_STREAM, when the RPC is already finished server-side, so permission-ask and disconnect semantics are unchanged. Public SDK API unchanged.Upstream: nodejs/node#66693 (this bug; maintainer expects a fix for v26/main, not v24), nodejs/node#65677 (late-read variant), connectrpc/connect-es#1752 (resets before trailers; compatible with this change).
🤖 Generated with Claude Code