Skip to content

grpc-js: Complete streaming write callbacks when the call ends - #3134

Open
eisen0419 wants to merge 1 commit into
grpc:masterfrom
eisen0419:fix/streaming-ended-call-write-callback
Open

eisen0419 wants to merge 1 commit into
grpc:masterfrom
eisen0419:fix/streaming-ended-call-write-callback

Conversation

@eisen0419

Copy link
Copy Markdown

Fixes #3114

Problem

When a client-streaming or bidirectional-streaming write is held in @grpc/grpc-js (by HTTP/2 flow control, the retry buffer, name resolution, load-balancing subchannel pick, an active client interceptor, or a SingleSubchannelChannel awaiting call setup) and the call ends via cancel(), deadline expiration, or the server finishing the RPC, the write callback passed to stream.write(chunk, cb) remained pending indefinitely instead of settling with an error.

Solution

  • Settle in-flight and queued write callbacks across Http2SubchannelCall, SingleSubchannelChannel, LoadBalancingCall, RetryingCall, ResolvingCall, InterceptingCall, and the public ClientWritableStreamImpl / ClientDuplexStreamImpl surface streams when the call terminates before those writes are accepted by the transport.
  • Record terminalWriteError on the surface stream before invoking callbacks so buffered and re-entrant writes share the same failure, while passing undefined to Node's internal _write callback so 'finish' / end(cb) and readable buffered bidirectional responses remain intact.
  • Forward write() after end() / destroy() directly to Writable/Duplex and roll back callback-queue entries if super.write throws synchronously (such as write(null, cb)).
  • Preserve FIFO ordering for multiple writes queued ahead of child creation in ResolvingCall, InterceptingCall (while start metadata is pending), and SingleSubchannelChannel, and avoid double-invoking interceptor write callbacks when next() is repeated after cancel().

Verification

  • npx tsc -p . in packages/grpc-js
  • GRPC_EXPERIMENTAL_ENABLE_OUTLIER_DETECTION=true npx mocha --timeout 20000 --exit build/test/test-streaming-write-callback.js build/test/test-retry.js build/test/test-client.js (71 passing)

A write held by HTTP/2 flow control, the retry buffer, name resolution,
or load balancing never ran its callback if the call was cancelled,
exceeded its deadline, or the server finished it. Invoke each pending
callback once with that error, and leave buffered bidi responses readable.

Publish the terminal write error before either callback so a queued
write sees it. Pass write() through to Node after end() or destroy().
Keep later retry-buffer callbacks until earlier ones settle, queue every
message held for name resolution, and do not run an interceptor write
callback twice after cancel.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grpc-js: write callback never called when a streaming call ends while the write is held by flow control

1 participant