Repository navigation
Commit 3750763
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
- lib/src/core
- test/core
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
230 | 230 | | |
231 | 231 | | |
232 | 232 | | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
233 | 237 | | |
234 | 238 | | |
235 | 239 | | |
| |||
316 | 320 | | |
317 | 321 | | |
318 | 322 | | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
319 | 334 | | |
320 | 335 | | |
321 | 336 | | |
322 | 337 | | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
323 | 341 | | |
324 | 342 | | |
325 | 343 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
140 | 197 | | |
0 commit comments