Commit 521a356
fix: pool shutdown hang + per-connection leak from the peer-state callback (#4)
PR #3 added an `on_peer_connection_state_change` callback that captures a
clone of the per-session `SupervisorEvent` sender and a strong `Arc` back to
the `RTCPeerConnection`. Two regressions followed:
1. Shutdown hang. The pool's per-worker forwarder ran
`while let Some(event) = worker_events_rx.recv().await`, which only ends
when every sender clone drops. The clone captured by the WebRTC callback
outlives the session, so the channel never closed — the pool future never
completed after cancellation and `SharingHandle::stop` hung forever.
(Surfaced by spark-sharing's `starts_emits_and_stops_the_unprivileged_pool`
once its pin advanced onto a main containing #3; unbounded-rs's own tests
missed it because they drive a mock `run_session` that never clones the
sender into a real WebRTC callback.)
2. Per-connection memory leak. `state_connection = connection.clone()` inside
the callback forms an `Arc<RTCPeerConnection>` <-> handler reference cycle,
so the connection (and its captured sender) is never freed even after
`close()` — one leaked connection per session over a long-running pool.
Fixes:
- supervisor.rs: the worker forwards inline via `tokio::select!` and stops when
`supervise_peer_proxy` completes, draining the queue once (the final
`Stopped` is enqueued before it returns, so nothing is lost). Shutdown no
longer depends on a leaked sender clone dropping.
- peer_proxy.rs: the callback holds `Weak<RTCPeerConnection>` and upgrades it
on the Connected transition, breaking the cycle so the connection and its
captured sender are freed when the session ends.
Adds `pool_shuts_down_when_a_session_registers_a_state_callback`, which drives
the real peer proxy through the pool and asserts shutdown completes after
cancellation — it hangs (5s timeout) without either fix.
Co-authored-by: Adam Fisk <a@lantern.io>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent a00a15c commit 521a356
2 files changed
Lines changed: 87 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
191 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
192 | 196 | | |
193 | 197 | | |
194 | 198 | | |
| |||
220 | 224 | | |
221 | 225 | | |
222 | 226 | | |
223 | | - | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
224 | 233 | | |
225 | 234 | | |
226 | 235 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
| 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 | + | |
111 | 133 | | |
112 | 134 | | |
113 | | - | |
114 | | - | |
115 | | - | |
| 135 | + | |
116 | 136 | | |
117 | 137 | | |
118 | 138 | | |
| |||
411 | 431 | | |
412 | 432 | | |
413 | 433 | | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
414 | 483 | | |
415 | 484 | | |
416 | 485 | | |
| |||
0 commit comments