VAPI-4011 fix(v1): harden unpublish against races, unknown ids, and disconnect - #21
Merged
Merged
Conversation
- Run unpublish's transceiver cleanup and renegotiation under one publishMutex section; do the same for publish's attach + negotiate. - unpublish with ids that match no published stream is now a no-op instead of unpublishing every stream. - Stop local tracks first, wait for the publish peer to be connected, and reject with a clear error if renegotiation still fails. - unpublish after disconnect stops tracks locally instead of throwing. - republishStreams skips streams unpublished mid-reconnect and stops any tracks it reacquired for them. - AudioLevelDetector gains stop(); unpublish releases its AudioContext and sampling interval. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
Keeps the test valid once init() republishes only on a reconnect (#18). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The wait can last 10 s and would otherwise block publish() and gateway ICE-restart offers. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Merged
4 tasks
stampercasey
approved these changes
Sep 23, 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
Part of VAPI-4011. Fixes six bugs in the v1
unpublishpath.unpublishremoved transceivers outsidepublishMutex.offerPublishSdpsends the offer to the gateway beforesetLocalDescription, so a concurrent removal made the applied offer stale. Now transceiver cleanup runs under the mutex.publish()attach + negotiate also runs in one mutex section, which closes the race from the other side. The body ofofferPublishSdpmoves intonegotiatePublishSdp, which does not lock.connected(the gateway rejects offers before that) and negotiates. The wait runs outsidepublishMutex, so it does not blockpublish()or a gateway ICE-restart offer for up to 10 s. If that still fails, the SDK rejects with aBandwidthRtcErrorthat says the stream was unpublished locally.unpublish()afterdisconnect()threw aTypeError. Now it stops tracks locally and returns.republishStreamsnow skips streams that are no longer published and stops any tracks it reacquired for them.AudioLevelDetectorleaked anAudioContextand asetIntervalper publish. Added an idempotentstop(). The detector is stored onPublishedStreamand stopped on unpublish.Notes
tscand all 123 tests.negotiatePublishSdpinstead ofofferPublishSdp, becausepublish()no longer goes through the locking wrapper.Test plan
npm test: 5 suites, 106 tests pass (10 new)npx tsc --noEmitclean🤖 Generated with Claude Code