fix(agents): keep downstream-cancelled stream channels from leaking unhandled rejections - #2362
Open
anzemur wants to merge 1 commit into
Open
fix(agents): keep downstream-cancelled stream channels from leaking unhandled rejections#2362anzemur wants to merge 1 commit into
anzemur wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 32f2784 The changes in this PR will be included in the next version bump. This PR includes changesets to release 39 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…nhandled rejections
anzemur
force-pushed
the
fix/stream-channel-downstream-cancel
branch
from
August 28, 2026 11:56
ca6e5e7 to
32f2784
Compare
anzemur
marked this pull request as ready for review
August 28, 2026 11:57
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.
What
Since 1.7.0,
forwardAudiocancels its reader on abort (reader.cancel()with no reason). Cancelling the readable side of a stream channel errors the writable side withundefined, so every later fire-and-forgetwrite()/close()from a realtime producer rejects withundefinedand surfaces as an unhandled promise rejection (Error: undefined, no application frames).In production (google realtime plugin,
livekit-agentsworker on node 22) this fires on every barge-in: the plugin keeps writing the frames that arrive after the interrupt into the cancelled audio channel (realtime_api.tsgen.audioChannel.write(audioFrame)/markCurrentGenerationDone→audioChannel.close()). We saw ~640 events across 54 users in 24h after upgrading.Fix
At the
createStreamChannelseam, so every producer is covered:writer.closedso the channel learns about downstream cancellation / errors, flipsisClosed, and debug-logs the transition (also fixes the staleclosedgetter).write()attaches a handler to each write promise (no more unhandled rejections from fire-and-forget producers) while returning the original promise, so awaiting callers keep exact backpressure and rejection semantics.close()swallows "already closed or errored" failures instead of onlyTypeErrorinstances — anundefinedcancel reason slipped past thee instanceof Errorcheck.tryLog()export next tolog()- it returns the logger orundefinedbeforeinitializeLogger()has run (log()throws there, and a throw inside these handlers would recreate the unhandled rejection).Complementary one-liner:
forwardAudionow cancels withnew Error('audio forwarding aborted')so anything that still observes the reason sees a real error instead ofundefined. Note this line alone does NOT fix the bug - it only relabels the rejections; thecreateStreamChannelhardening is what removes them (verified below).Tests
closed; an awaitedwrite()after cancel still rejects with the cancel reason.agentssuite passes (2283 tests).Live verification
Ran our production voice agent (google realtime plugin, real LiveKit + Gemini session) with this fix applied to the installed 1.7.0 dist:
interrupt()closes the audio channel viamarkCurrentGenerationDone()whenever the generation already has output, so a scripted interrupt lands after that window. A deterministic replay of the plugin's exact post-cancel calls (gen.audioChannel.write(audioFrame)andaudioChannel.close()afterforwardAudio'sreader.cancel()) shows 8 unhandled rejections unpatched (including theundefined-reason ones we see in Sentry) vs 0 patched, on both the ESM and CJS builds.stream channel writable errored or was cancelled downstream) doubles as a detector for the bug condition in the wild.