Skip to content

Commit 3750763

Browse files
Ignore a duplicated server answer (#1257)
Port of [livekit/client-sdk-js#2114](livekit/client-sdk-js#2114). ## The bug `Transport.setRemoteDescription` applied every answer it was handed, with no check that one was still wanted: ```dart try { await pc.setRemoteDescription(sd); } catch (e) { logger.warning('[$objectId] setRemoteDescription() failed with error: $e'); } ``` A proxy duplicating the signal message, or a server retry, therefore applies the same answer twice. The second apply happens with no local offer left to answer, which is an invalid transition. Because the failure is swallowed into a warning, the method then carries on and flushes `_pendingCandidates` and clears `restartingIce` a second time — side effects for a message that should have been dropped. ## Repro Added a `Transport.setRemoteDescription duplicate answers` group to `transport_negotiate_test.dart`. Against the unfixed code: ``` ignores a second copy of an answer that was already applied [E] Expected: v=0\r\nfirst Actual: v=0\r\nduplicate duplicate answer was applied to the peer connection ``` ## The fix Offer ids are not plumbed through this SDK, so the numbered-answer half of the upstream fix has no analogue here. What does apply is the rule JS falls back to for *unnumbered* answers: in `stable` there is no local offer left to answer, so an answer arriving then is a duplicate. An `_answerInFlight` flag covers a copy that arrives while the first is still being applied, and is released in a `finally` however the apply ends, so a genuine retry still works. This is consistent with how the transport already reasons about negotiation — `createAndSendOffer` likewise gates on `getSignalingState() == RTCSignalingStateHaveLocalOffer`. Only answers are gated; remote offers (normal subscriber traffic, which do arrive in `stable`) are untouched, and there is a test for that. The ICE-restart path at `transport.dart:368` calls `pc.setRemoteDescription` directly rather than going through this method, so it is unaffected. ## Verification `flutter test` 471/471, `flutter analyze lib/src/core/transport.dart` clean. The three new cases cover the duplicate, the still-wanted answer, and the remote-offer-in-stable case. ## One thing worth a reviewer's eye I could not get an automated test to cover *a second negotiation round* (offer → answer → offer → answer). `MockPeerConnection.signalingState` only reports `HaveLocalOffer` when `_remoteDescription` is null, so once any answer has been applied the mock reports `stable` forever, even after a fresh `setLocalDescription(offer)`. Real WebRTC moves to `have-local-offer` there, so the second answer is accepted in production — and no existing test exercises that path through `Transport.setRemoteDescription`, which is why the suite stays green. But that means the mock cannot currently prove it. If you'd rather, I can tighten the mock's state model in a follow-up so this is actually pinned down. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 8594479 commit 3750763

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎.changes/ignore-duplicate-answer‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
patch type="fixed" "A duplicated server answer is now ignored instead of being applied a second time, which failed on the peer connection and re-ran the transport's post-answer work for a message that should have been dropped"

‎lib/src/core/transport.dart‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ class Transport extends Disposable {
230230

231231
bool restartingIce = false;
232232
bool renegotiate = false;
233+
234+
/// Whether an answer is being applied right now, so a copy that arrives
235+
/// mid-apply is recognised as a duplicate too.
236+
bool _answerInFlight = false;
233237
TransportOnOffer? onOffer;
234238
TransportOnNegotiationError? onNegotiationError;
235239
Function? _cancelDebounce;
@@ -316,10 +320,24 @@ class Transport extends Disposable {
316320
return;
317321
}
318322

323+
if (sd.type == 'answer') {
324+
// Offers are not numbered on this transport, so the signaling state is what says
325+
// whether an answer is still wanted: in `stable` there is no local offer left to
326+
// answer, and a copy arriving mid-apply is caught by the in-flight flag.
327+
if (_answerInFlight || await pc.getSignalingState() == rtc.RTCSignalingState.RTCSignalingStateStable) {
328+
logger.warning('[$objectId] ignoring duplicate answer');
329+
return;
330+
}
331+
_answerInFlight = true;
332+
}
333+
319334
try {
320335
await pc.setRemoteDescription(sd);
321336
} catch (e) {
322337
logger.warning('[$objectId] setRemoteDescription() failed with error: $e');
338+
} finally {
339+
// released however the apply ends, so a genuine retry is still possible
340+
if (sd.type == 'answer') _answerInFlight = false;
323341
}
324342

325343
for (final candidate in _pendingCandidates) {

‎test/core/transport_negotiate_test.dart‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,61 @@ void main() {
137137
await expectLater(transport.createAndSendOffer(), throwsA(isA<NegotiationError>()));
138138
});
139139
});
140+
141+
group('Transport.setRemoteDescription duplicate answers', () {
142+
Future<(Transport, MockPeerConnection)> makeTransport() async {
143+
final pc = MockPeerConnection();
144+
final transport = await Transport.create(
145+
(Map<String, dynamic> configuration, [Map<String, dynamic>? constraints]) async => pc,
146+
connectOptions: const ConnectOptions(),
147+
);
148+
addTearDown(transport.dispose);
149+
transport.onOffer = (_) {};
150+
return (transport, pc);
151+
}
152+
153+
test('ignores a second copy of an answer that was already applied', () async {
154+
final (transport, pc) = await makeTransport();
155+
156+
await transport.createAndSendOffer();
157+
expect(pc.signalingState, rtc.RTCSignalingState.RTCSignalingStateHaveLocalOffer);
158+
159+
await transport.setRemoteDescription(rtc.RTCSessionDescription('v=0\r\nfirst', 'answer'));
160+
expect(pc.signalingState, rtc.RTCSignalingState.RTCSignalingStateStable);
161+
162+
// A proxy (or a server retry) can deliver a second copy of the answer that
163+
// was already applied. There is no local offer left to answer, so applying
164+
// it again is invalid. The payload differs only so the test can tell.
165+
await transport.setRemoteDescription(rtc.RTCSessionDescription('v=0\r\nduplicate', 'answer'));
166+
167+
final remote = await pc.getRemoteDescription();
168+
expect(
169+
remote?.sdp,
170+
'v=0\r\nfirst',
171+
reason: 'duplicate answer was applied to the peer connection',
172+
);
173+
});
174+
175+
test('still applies the answer to a pending offer', () async {
176+
final (transport, pc) = await makeTransport();
177+
178+
await transport.createAndSendOffer();
179+
expect(pc.signalingState, rtc.RTCSignalingState.RTCSignalingStateHaveLocalOffer);
180+
181+
await transport.setRemoteDescription(rtc.RTCSessionDescription('v=0\r\nwanted', 'answer'));
182+
183+
final remote = await pc.getRemoteDescription();
184+
expect(remote?.sdp, 'v=0\r\nwanted');
185+
});
186+
187+
test('still applies a remote offer while in stable', () async {
188+
final (transport, pc) = await makeTransport();
189+
190+
// Only answers are gated; a remote offer in stable is normal subscriber traffic.
191+
await transport.setRemoteDescription(rtc.RTCSessionDescription('v=0\r\nremote-offer', 'offer'));
192+
193+
final remote = await pc.getRemoteDescription();
194+
expect(remote?.sdp, 'v=0\r\nremote-offer');
195+
});
196+
});
140197
}

0 commit comments

Comments
 (0)