Commit 06e5f58
authored
fix(client): harden SFU reconnection and ICE-restart recovery (#2285)
### 💡 Overview
Four reconnection-robustness changes for the core client.
**1. Signal-close revival hardening (`StreamSfuClient`, `Call`).** A
wedged signal WebSocket can be detected as dead by two independent
sources (the health watchdog and the transport `close` event), which on
a stuck socket can arrive seconds apart. Revival is now driven by a
one-shot latch so it fires at most once per client, triggers immediately
on an unhealthy close instead of waiting for the (possibly long-delayed)
`close` event, and ignores closes from a superseded client so a stale
socket cannot drive a reconnect after a new client has swapped in.
**2. Subscriber negotiation-failure recovery (`Subscriber`).**
`Subscriber.negotiate` reset `isIceRestarting` only as the last
statement of its happy path, so a negotiation failure left the flag
stuck at `true` and disabled all future automatic ICE-restart /
reconnect handling on the subscriber. Negotiation is now wrapped in
try/catch/finally so the flag always clears; on failure the remote
description is rolled back when the peer connection is still awaiting an
answer (`have-remote-offer`), the rollback is itself guarded so it can
never mask the original error, the failure is traced, and the subscriber
escalates via `tryRestartIce` so a failed restart falls through to a
reconnect instead of being silently dropped.
**3. Revert the stability-gated publisher fast-reconnect skip (`Call`,
`BasePeerConnection`).** `main` skips the publisher's ICE restart on a
fast reconnect when the publisher PC still looks stable (`isStable()`),
to avoid SDP/ICE churn on signal-WS-only drops. In practice that skip
can strand a cratered send-side bandwidth estimate: a "stable" publisher
PC never restarts ICE, so the network-route-change BWE reset never fires
and the encoder stays bitrate-starved after a handover. This restores
the earlier behavior (on a fast reconnect the publisher's ICE is
restarted unconditionally whenever it is publishing) and removes the
now-unused `isStable()` helper.
**4. Generation-aware ICE trickle buffer (`IceTrickleBuffer`,
`BasePeerConnection`, `Subscriber`).** After an ICE restart the SFU
rotates its ICE credentials and trickles candidates for the new
generation, but `IceTrickleBuffer` (an unbounded `ReplaySubject`)
replayed every buffered candidate on each renegotiation, re-adding
superseded-generation candidates against the current generation and
contributing to stuck (DTLS-`connecting`) subscribers. The buffer is now
generation-aware: each peer connection declares the active generation
via `updateActiveGeneration` (derived from the applied remote
description's `ice-ufrag`), and the candidate streams emit only the
current generation, hold not-yet-applied (future) generations until they
become active, and drop superseded ones; candidates with no detectable
generation fail open. On a failed subscriber negotiation the buffer is
restored to the rolled-back generation.
### 📝 Implementation notes
- `StreamSfuClient`: `handleWebSocketClose` becomes `notifySignalClose`,
guarded by a `signalClosed` one-shot latch; `close()` notifies on a
non-clean close without waiting for the transport event.
- `Call.handleSfuSignalClose` ignores closes from any client that is no
longer the active `sfuClient`.
- `Call` fast reconnect: back to `restoreICE(sfuClient, {
includeSubscriber: false })` (publisher restart governed by
`includePublisher`'s `true` default); the `isStable()`-gated skip is
gone.
- `Subscriber.negotiate` wrapped in try/catch/finally; guarded rollback
on `have-remote-offer`; `subscriber.negotiationFailed` trace;
`tryRestartIce` escalation on failure.
- `BasePeerConnection.isStable()` removed (no remaining callers).
- `IceTrickleBuffer`: per-peer `ReplaySubject`s replaced by per-peer
`CandidateGenerationBuffer`s (mutable store + live `Subject`, exposed as
`subscriber` / `publisher`); `updateActiveGeneration(peerType, sdp)`
sets the active generation from the remote `ice-ufrag` and evicts
superseded candidates.
- `BasePeerConnection.addTrickledIceCandidates` declares the active
generation from `pc.remoteDescription`, then subscribes; the
candidate-string / SDP ufrag helpers (`getCandidateUfrag`,
`parseIceUfrag`, `toJSON`) are consolidated in
`rtc/helpers/iceCandiates.ts`.
- `Subscriber.negotiate` snapshots the committed generation and restores
it in the buffer when a negotiation rolls back.
- Tests: `StreamSfuClient` revival, `Call` superseded-client guard,
`Subscriber` success / failure / rollback / rollback-failure branches;
removed the `isStable()` and FAST-skip tests. Added `IceTrickleBuffer`
generation emit/hold/drop/fail-open, `iceCandiates` helper, and
subscriber trickle wiring + rollback-restore coverage.
🎫 Ticket:
https://linear.app/stream/issue/REACT-1016/improve-bandwidth-estimation-ramp-up-and-harden-ice-restarts1 parent c5803fc commit 06e5f58
15 files changed
Lines changed: 988 additions & 222 deletions
File tree
- packages/client/src
- __tests__
- coordinator/connection
- __tests__
- rtc
- __tests__
- helpers
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1333 | 1333 | | |
1334 | 1334 | | |
1335 | 1335 | | |
1336 | | - | |
1337 | | - | |
1338 | | - | |
1339 | | - | |
1340 | | - | |
1341 | | - | |
1342 | | - | |
1343 | | - | |
1344 | | - | |
1345 | | - | |
1346 | | - | |
1347 | | - | |
1348 | | - | |
1349 | | - | |
1350 | | - | |
1351 | | - | |
1352 | | - | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
1353 | 1339 | | |
1354 | 1340 | | |
1355 | 1341 | | |
| |||
1688 | 1674 | | |
1689 | 1675 | | |
1690 | 1676 | | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
| 1680 | + | |
1691 | 1681 | | |
1692 | 1682 | | |
1693 | 1683 | | |
| |||
1728 | 1718 | | |
1729 | 1719 | | |
1730 | 1720 | | |
| 1721 | + | |
1731 | 1722 | | |
1732 | | - | |
1733 | | - | |
1734 | | - | |
1735 | | - | |
| 1723 | + | |
| 1724 | + | |
| 1725 | + | |
| 1726 | + | |
1736 | 1727 | | |
1737 | 1728 | | |
1738 | 1729 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
167 | 175 | | |
168 | 176 | | |
169 | | - | |
| 177 | + | |
170 | 178 | | |
171 | 179 | | |
172 | | - | |
173 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
174 | 185 | | |
175 | 186 | | |
176 | 187 | | |
| |||
209 | 220 | | |
210 | 221 | | |
211 | 222 | | |
212 | | - | |
| 223 | + | |
213 | 224 | | |
214 | 225 | | |
215 | 226 | | |
| |||
311 | 322 | | |
312 | 323 | | |
313 | 324 | | |
314 | | - | |
| 325 | + | |
315 | 326 | | |
316 | 327 | | |
317 | 328 | | |
| |||
336 | 347 | | |
337 | 348 | | |
338 | 349 | | |
339 | | - | |
| 350 | + | |
340 | 351 | | |
341 | 352 | | |
342 | 353 | | |
| |||
371 | 382 | | |
372 | 383 | | |
373 | 384 | | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
379 | 392 | | |
380 | 393 | | |
381 | 394 | | |
| |||
392 | 405 | | |
393 | 406 | | |
394 | 407 | | |
395 | | - | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
396 | 411 | | |
397 | 412 | | |
398 | 413 | | |
| |||
401 | 416 | | |
402 | 417 | | |
403 | 418 | | |
404 | | - | |
405 | | - | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
406 | 422 | | |
407 | 423 | | |
408 | 424 | | |
| |||
711 | 727 | | |
712 | 728 | | |
713 | 729 | | |
714 | | - | |
715 | | - | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
716 | 733 | | |
717 | | - | |
718 | | - | |
719 | | - | |
| 734 | + | |
720 | 735 | | |
721 | 736 | | |
722 | 737 | | |
723 | 738 | | |
724 | 739 | | |
725 | 740 | | |
726 | 741 | | |
727 | | - | |
| 742 | + | |
728 | 743 | | |
729 | 744 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
41 | 46 | | |
42 | 47 | | |
43 | | - | |
| 48 | + | |
44 | 49 | | |
45 | 50 | | |
46 | 51 | | |
| |||
59 | 64 | | |
60 | 65 | | |
61 | 66 | | |
| 67 | + | |
62 | 68 | | |
63 | 69 | | |
64 | 70 | | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 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 | + | |
65 | 170 | | |
66 | 171 | | |
67 | 172 | | |
| |||
164 | 269 | | |
165 | 270 | | |
166 | 271 | | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
167 | 325 | | |
168 | 326 | | |
169 | 327 | | |
| |||
0 commit comments