Repository navigation
Conversation
A client that asks for media and goes away before it answers the offer leaves a WebRTC peer that never connects. Nothing destroys such a peer: the connection state handler only acts on a peer that disconnects or fails, and deleting or disconnecting a session only destroys a peer that is watching. The peer keeps its listeners on the capture streams, so the video and audio pipelines keep encoding for the life of the process with nobody watching. A peer needs the websocket to finish connecting, so one that has not connected is now destroyed when its session's websocket goes away. The websocket can also go away while the peer is still being created; SetWebRTCPeer then destroys the peer it is given. A track that was shut down refuses a stream, so a peer destroyed between its creation and the request that selects its video cannot start a pipeline nothing stops.
Whether a peer had connected was read from the session's watching state. That state is left over from a peer that was replaced: the closing of a replaced peer is ignored, so the session goes on counting as watching until the new peer connects. A client that was watching, asked for media again and left before it answered therefore still left a peer that nothing destroyed, with both pipelines running. The session now records under the peer's lock whether the current peer has connected, and resets that when the peer is replaced.
Deleting or disconnecting a session destroys the websocket first, which now closes a peer that has not connected. That peer clears itself from the session on another goroutine. If the session still counted as watching, from a peer that was replaced, the peer could be gone between the check of the watching state and the call to destroy it, and the call went to a nil peer.
A disconnecting websocket runs the session's listeners before it is cleared from the session, and a client can be back on a new websocket in the meantime. The old websocket is then no longer the current one and is not cleared, but the peer that has not connected was destroyed all the same, including one the new websocket had just asked for. It is now destroyed only by the websocket that was cleared.
The audio track gets its stream while it is paused and only listens once it is unpaused. The test that unpaused a track after shutdown used one that never had a stream, so it would have passed if shutdown left the stream of a paused track in place.
A session that may no longer connect has its websocket destroyed at once and its peer a second later, from a timer. Destroying the websocket now closes a peer that has not connected, and that peer clears itself from the session. If the session still counted as watching, from a peer that was replaced, there was no peer left when the timer fired, and the call went to a nil peer.
Deleting or disconnecting a session read the session's watching state after it had destroyed the websocket. Destroying the websocket closes a peer that has not connected, and that peer writes the watching state on another goroutine when it has closed, at the same time as the read. The read decided nothing that the peer does not: a peer that is still there is destroyed, and destroying it twice does no harm. The tests record whether a peer was destroyed instead of how often.
…ared A disconnecting websocket destroyed the peer that had not connected whenever it had cleared itself from the session. A client can be back on a new websocket right after that, and ask for a peer before the old websocket looks for one to destroy. The old websocket then destroyed the peer of the new one. SetWebRTCPeer could do the same to a peer that had replaced the one it was given. The peer is now destroyed only if no websocket is connected, and the websocket is looked at after the peer: a peer that was set while a websocket is connected is never taken from it. That check also covers a websocket that took over before the old one was cleared, and the one SetWebRTCPeer made itself.
The session tests use a peer that is destroyed at once. A real peer is closed on other goroutines, where it clears itself from the session and shuts its tracks down. This test runs the WebRTC manager and the session manager together with clients that leave without answering the offer, and counts the listeners left on the capture streams.
A websocket that dropped may reconnect for a while, and a peer that has not connected is kept for that long. The tests only looked at the peer right after the drop. Nothing failed if the delay never ran out, or if running out it took the peer of a client that had come back.
…watch A session that loses its right to watch has its peer destroyed, but only if it counted as watching. A peer that had not connected was left alone, and the session keeps its websocket, so the client could still answer the offer. The peer then connected and streamed to a session that may not watch. The peer is now destroyed whether or not it has connected.
The comment said such a peer needs the websocket to connect and stays half-open for good without it. That holds until the client has answered. After that the peer can connect on its own, or fails some seconds later; it is destroyed all the same, because the client has left.
The timer that destroys the peer of a session that may no longer watch did not look at the profile again when it fired. Since it no longer waits for the session to count as watching, a session that lost the right to watch and got it back within the second lost a peer it created in between.
The subtests shared one capture, so listeners left by one were counted again by every subtest after it, and the check that something listens after a request could not fail once an earlier subtest had left some.
The websocket is disconnected before the peer is created, not while.
Until the first listener's capture pipeline has started, the peer is busy. A leaving client's logout destroys its peer, so it waited for the pipeline, and every other login waited behind it.
This was referenced Nov 15, 2026
A permission grant and a new peer could land between the timer's profile check and the lookup of the peer to close. Capture the peer under the profile lock and destroy it after releasing both locks. Share the viewer setup in session tests and exercise replacement after a permission grant.
yummybomb
force-pushed
the
hypeship/destroy-half-open-webrtc-peer
branch
from
November 18, 2026 18:35
a670b8d to
8505631
Compare
yummybomb
added a commit
that referenced
this pull request
Nov 18, 2026
## Summary Each capture pipeline adds a watch to its GStreamer bus. Destroying the pipeline didn't remove the watch, so the bus and the two file descriptors for its control socket pair stayed alive. A viewer who starts and stops both audio and video leaves four descriptors behind, even on a normal disconnect. Remove the watch during pipeline teardown. This is independent of the WebRTC peer cleanup in #33 and can land first. ## Testing - `TestDestroyClosesPipelineFiles` warms up GStreamer, runs ten pipelines, and checks the process's file descriptor count. On `master` those ten pipelines left 20 descriptors open. With this change the count did not grow. - `go test -race -count=20 ./pkg/gst` passed with GStreamer 1.28.2. `go vet ./pkg/gst` passed. - In the earlier combined viewer tests, the server had 103 open files after 20 viewers without the fix versus 23 with it. The package test above checks this change in isolation. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Teardown order in native GStreamer code affects long-lived viewers; incorrect bus handling could regress shutdown, but the change is narrowly scoped with a regression test for FD leaks. > > **Overview** > Fixes a **file descriptor leak** when capture pipelines are torn down: `gstreamer_pipeline_destory` now **removes the bus watch** and unrefs the bus after stopping the pipeline, so the bus and its control-socket FDs are not kept alive after destroy. > > Adds **`TestDestroyClosesPipelineFiles`**, which warms GStreamer once, runs ten create/play/destroy cycles, and asserts the process FD count does not grow. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 7c3ebdc. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-authored-by: yummybomb <19238148+yummybomb@users.noreply.github.com>
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.
Summary
A client that asks for media and leaves before WebRTC connects left the video and audio pipelines running for the life of the process. A peer that has not connected is now destroyed when its session's websocket goes away.
Ending a session does not wait for a busy peer: otherwise a client leaving during capture startup would hold up every other login. The independent GStreamer file descriptor fix is in #36 and can land first.
Why it happened
signal/requestcreates the peer and subscribes its tracks to the capture streams, which starts their pipelines. From then on the peer only goes away through its connection state:disconnectedandfaileddestroy it,closedremoves its listeners. A peer whose offer is never answered stays innew, where none of that happens. Deleting or disconnecting the session does not help, because both only destroy a peer that is watching.The streams stop when their last listener leaves, so one such peer keeps both pipelines encoding. Viewers that come and go afterwards do not stop them.
The legacy
/wsendpoint requests media as soon as a client connects, so opening a page that uses it and closing or reloading it before the stream starts is enough.What changes
DisconnectWebSocketPeerdestroys the session's WebRTC peer if it has not connected and the session has no websocket left. A connected peer is left alone as before, a delayed disconnect still gives the client its time to come back, and a peer asked for by a websocket that took over is kept.SetWebRTCPeerdoes the same when the websocket is already gone, because a client can leave while its peer is being created. It selects the replaced peer underwebrtcMuand destroys it after unlocking.DeleteandDisconnectdestroy whatever peer is still there instead of reading the watching state, and skip a peer that is already gone. They destroy the websocket first, which can now close the peer, and a closing peer clears itself from the session on another goroutine.Track.SetStreamreturnsErrTrackClosedafterShutdown. A peer destroyed betweenCreatePeerand theSetVideothat follows it would otherwise add a listener that nothing removes.Destroyon a WebRTC peer no longer waits for a peer that is busy. The peer is closed once it is free, in the same order as before.SetVideoholds the peer's lock while the first listener starts the capture pipeline, and a session is logged out under the member manager's login lock. The logout of a client that left during that start waited for the pipeline, and every other login and logout waited behind it.Testing
internal/sessiontests reconnect timing, peer replacement and session teardown. TheDeleteandDisconnecttests close the websocket first and check that a streaming peer survives until the session manager destroys it.internal/webrtctests the track after shutdown. With real WebRTC peers and a listener-counting capture, it checks that audio and video both return to zero after an unanswered or replaced offer, and that a request made after the websocket is gone leaves no peer or listeners.internal/webrtcalso deletes a session while its peer waits for a stream to start. Without the change toDestroythe delete waits for the stream.server/,go test ./...,go test -race -count=20 ./internal/session ./internal/webrtc, andgo vet ./internal/session ./internal/webrtcpassed.Earlier end-to-end measurements
These measurements are from before the PRs were split. The GPU runs were not repeated for this revision.
v3.0.8-v1.6.3, with and without this change, and looked 9 s after each client had left:/api/ws: requests media, leaves after the offer/api/ws: requests media, leaves at once or 300 ms later/ws: leaves after the offer/ws: leaves 0, 20, 100, 300, 1000 or 3000 ms after connecting/wsclients at once, leaving after 0 to 600 msWithout the change each of these left GPU memory at about 885 MiB against 370 MiB idle and the server at 65 to 72% of a core against 0, unchanged 40 s later; the server was restarted between cases so that each one started clean. With it, memory and CPU were back at idle in every case, the server never restarted or logged a panic, and a real viewer afterwards got its first frame 1.4 s after connecting. These runs were made before the per-peer connected state and the commits after it. The next point repeats them with the per-peer state and nonblocking close.
nvh264enc, 1080p60) I ran the combined build againstmaster. The 11 kinds of client above and three more, two that drop the TCP connection after the offer and one that answers before it leaves, all left the server idle with no encoder session 9 s later. Onmaster13 of the 14 were still encoding. Ordinary viewers, a second viewer leaving, and a websocket that closes while its peer streams behaved as onmaster: 60 fps, no lost packets, no panic. In Chrome with the bundled client, 41 page loads abandoned after 100 to 1500 ms left the server idle; onmasterit was still encoding.masterThe login was timed five times per build, with the server restarted before each. All 200 successive viewers got video on the combined build. The descriptor counts and their isolated regression test are in #36.
Deleteor in the profile change timer.What it does not do
What it does that
masterdid notmasterthe peer of a client that answered and left kept its listeners for 10 s, until ICE gave up.Destroyhas returned, once it is free. That is a peer whose first listener is starting a pipeline, and withwebrtc.icetrickleoff a peer that is still gathering candidates. Deleting a session whose peer is gathering does not wait, as onmaster; the peer is closed when gathering ends. BeforeDestroystopped waiting, that delete took 4.7 s with an ICE server that does not answer.