Implement negotiation tracking based on offerId#1927
Open
Conversation
🦋 Changeset detectedLatest commit: 106574e The changes in this PR will be included in the next version bump. 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 |
Contributor
size-limit report 📦
|
…js into lukas/negotiaton-loo
1egoman
approved these changes
Apr 29, 2026
Contributor
1egoman
left a comment
There was a problem hiding this comment.
Generally the whole approach makes sense to me!
Comment on lines
+128
to
+132
| setTimeout(() => pub.answer(1), 10); // does not satisfy checkpoint=1 | ||
| setTimeout(() => { | ||
| const id = pub.startOffer(); // 2 | ||
| pub.answer(id); | ||
| }, 30); |
Contributor
There was a problem hiding this comment.
thought: Two things:
- I wonder if there's a way to do a lot of these same testing scenarios without explicit sleeps. Are the lengths of these delays meaningful here, or is the goal just to have a series of events all occur in a defined order all on their own event loop macrotasks?
- I think linearizing these would make the tests a bit easier to follow, so you could scan the test top to bottom and have to jump between different functions which get scheduled to run at different times. ie, something like:
// Some sort of explanation here
// does not satisfy checkpoint=1
pub.answer(1);
await sleep(20); // Or ideally if the delay isn't important, `sleep(0)` / `setImmediate`.
// Some sort of explanation here
const id = pub.startOffer(); // 2
pub.answer(id);
Comment on lines
+242
to
+246
| // Race: an answer past our checkpoint already arrived before we had a | ||
| // chance to subscribe. | ||
| if (this.publisher.latestAcknowledgedOfferId > checkpoint) { | ||
| resolve(); | ||
| return; |
Contributor
There was a problem hiding this comment.
suggestion: Maybe it's worth logging here so that it would be evident that there's a lot of spurious renegotiation occurring in the context of other issues? Not sure.
Comment on lines
+59
to
+63
|
|
||
| describe('PCTransportManager.negotiate', () => { | ||
| let originalRTCPeerConnection: unknown; | ||
|
|
||
| beforeEach(() => { |
Contributor
There was a problem hiding this comment.
Really glad to see some tests here for this! Looking forward to seeing this grow moving forward.
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.
Fix publisher negotiation hang under stacked offer cycles
Under specific conditions the Promise returned by
setCameraEnabled(true)could neither resolve nor reject, even though the track was successfully published from the server's perspective. Sessions hitting this also showed MaxListenersExceededWarning for negotiationComplete/negotiationStarted on the publisher within seconds of connecting.Root cause
PCTransportManager.negotiate()resolved on the next NegotiationComplete event from the publisher. That event only fires when an answer is processed andrenegotiate === false. If a newpublisher.negotiate()lands while an offer is in flight, the next answer recurses into another offer instead of emitting NegotiationComplete (PCTransport.ts:239-243). With enough cycle starts (data-channel sends, server-driven MediaSectionsRequirement, concurrent track publishes) the cycle never converges, the resettable timeout from #1813 keeps firing, and the Promise wedges. cleanup() also never offed theonce(NegotiationComplete)listener, which is why the warning surfaced.Change
Switch from an event-cycle-based wait to an offerId checkpoint:
checkpoint = publisher.latestOfferIdat the momentnegotiate()is called.PCEvents.OfferAnsweredfires for every successful answer, including ones that recurse via renegotiate=true (which the existing NegotiationComplete deliberately suppresses).cleanup()— no leaks across abort/timeout/error paths.Tests
PCTransportManager.test.ts covers:
Risk
Behavior change is contained to the publisher negotiation handshake.
latestOfferIdis exposed publicly (was private) and latestAcknowledgedOfferId is added — both internal SDK fields. No public API surface changes.