From 50af9b3c8bf0c9016ac6ca1729eb40ed3fc7566b Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Wed, 11 Nov 2026 08:50:37 +0000 Subject: [PATCH 01/17] Destroy a WebRTC peer whose client left before it connected 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. --- server/internal/session/session.go | 32 +++++ server/internal/session/session_test.go | 172 ++++++++++++++++++++++++ server/internal/webrtc/track.go | 14 ++ server/internal/webrtc/track_test.go | 79 +++++++++++ 4 files changed, 297 insertions(+) create mode 100644 server/internal/webrtc/track_test.go diff --git a/server/internal/session/session.go b/server/internal/session/session.go index 686c9611a..eb5e31ce3 100644 --- a/server/internal/session/session.go +++ b/server/internal/session/session.go @@ -212,6 +212,28 @@ func (session *SessionCtx) DisconnectWebSocketPeer(websocketPeer types.WebSocket session.websocketPeer = nil } session.websocketMu.Unlock() + + // A WebRTC peer that has not connected yet needs this websocket to do so: + // it carries the answer and the candidates. Without it the peer would stay + // half-open for good and keep the capture pipelines it subscribed to + // running with nobody watching. + session.destroyPendingWebRTCPeer() +} + +// Destroy the WebRTC peer if it exists but has not connected. A connected peer +// is left alone: it does not need the websocket to keep streaming. +// +// The websocket peer must be cleared before this is called, and a WebRTC peer +// must be set before SetWebRTCPeer looks at the websocket peer. That order +// makes sure one of the two sees the other, whichever happens first. +func (session *SessionCtx) destroyPendingWebRTCPeer() { + if session.state.IsWatching { + return + } + + if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil { + webrtcPeer.Destroy() + } } // Destroy WebSocket peer disconnects the peer and destroys it. It ensures that the peer is @@ -256,6 +278,16 @@ func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) { if webrtcPeer != nil && webrtcPeer != session.webrtcPeer { webrtcPeer.Destroy() } + + // The websocket can go away while a peer is being created for it. Its + // disconnect found no peer to destroy then, so it is done here. + session.websocketMu.Lock() + hasWebSocket := session.websocketPeer != nil + session.websocketMu.Unlock() + + if !hasWebSocket { + session.destroyPendingWebRTCPeer() + } } // Set if current webrtc peer is connected or not. Since there might be lefover calls from diff --git a/server/internal/session/session_test.go b/server/internal/session/session_test.go index 2a2dddac6..d685d9066 100644 --- a/server/internal/session/session_test.go +++ b/server/internal/session/session_test.go @@ -39,3 +39,175 @@ func TestLegacyIsHost(t *testing.T) { }) } } + +type fakeWebSocketPeer struct{} + +func (fakeWebSocketPeer) Send(event string, payload any) {} +func (fakeWebSocketPeer) Ping() error { return nil } +func (fakeWebSocketPeer) Destroy(reason string) {} + +// fakeWebRTCPeer counts how often it was destroyed. The embedded interface is +// nil: these tests must not reach any other method. +type fakeWebRTCPeer struct { + types.WebRTCPeer + destroyed int +} + +func (p *fakeWebRTCPeer) Destroy() { p.destroyed++ } + +// A client can ask for media and vanish before it answers the offer. Its peer +// never connects, so nothing but the session can end it; left alone it keeps +// the capture pipelines it subscribed to running with nobody watching. +func TestWebRTCPeerThatNeverConnectedIsDestroyedWithItsSession(t *testing.T) { + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + + tests := []struct { + name string + connected bool + end func(manager *SessionManagerCtx, session types.Session, websocket types.WebSocketPeer) error + wantDestroyed int + }{ + { + name: "websocket closes before the peer connects", + end: func(_ *SessionManagerCtx, session types.Session, websocket types.WebSocketPeer) error { + session.DisconnectWebSocketPeer(websocket, false) + return nil + }, + wantDestroyed: 1, + }, + { + name: "websocket closes while the peer is streaming", + connected: true, + end: func(_ *SessionManagerCtx, session types.Session, websocket types.WebSocketPeer) error { + session.DisconnectWebSocketPeer(websocket, false) + return nil + }, + wantDestroyed: 0, + }, + { + name: "websocket drops and may still reconnect", + end: func(_ *SessionManagerCtx, session types.Session, websocket types.WebSocketPeer) error { + session.DisconnectWebSocketPeer(websocket, true) + return nil + }, + wantDestroyed: 0, + }, + { + name: "session is deleted before the peer connects", + end: func(manager *SessionManagerCtx, session types.Session, _ types.WebSocketPeer) error { + return manager.Delete(session.ID()) + }, + wantDestroyed: 1, + }, + { + name: "session is disconnected before the peer connects", + end: func(manager *SessionManagerCtx, session types.Session, _ types.WebSocketPeer) error { + return manager.Disconnect(session.ID()) + }, + wantDestroyed: 1, + }, + { + name: "session is deleted while the peer is streaming", + connected: true, + end: func(manager *SessionManagerCtx, session types.Session, _ types.WebSocketPeer) error { + return manager.Delete(session.ID()) + }, + wantDestroyed: 1, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + manager := New(&config.Session{}) + session, _, err := manager.Create("member", profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + websocket := &fakeWebSocketPeer{} + session.ConnectWebSocketPeer(websocket) + // stops the timer of a delayed disconnect + t.Cleanup(func() { session.DisconnectWebSocketPeer(websocket, false) }) + peer := &fakeWebRTCPeer{} + session.SetWebRTCPeer(peer) + if tt.connected { + session.SetWebRTCConnected(peer, true) + } + + if err := tt.end(manager, session, websocket); err != nil { + t.Fatalf("end session: %v", err) + } + + if peer.destroyed != tt.wantDestroyed { + t.Errorf("peer destroyed %d times, want %d", peer.destroyed, tt.wantDestroyed) + } + }) + } +} + +// Creating a peer takes a while, and the websocket that asked for it can be +// gone by the time the peer is attached to the session. Its disconnect could +// not destroy a peer that did not exist yet. +func TestWebRTCPeerSetAfterItsWebSocketLeftIsDestroyed(t *testing.T) { + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + + tests := []struct { + name string + leave func(manager *SessionManagerCtx, session types.Session, websocket types.WebSocketPeer) error + wantDestroyed int + }{ + { + name: "websocket still connected", + leave: func(*SessionManagerCtx, types.Session, types.WebSocketPeer) error { + return nil + }, + wantDestroyed: 0, + }, + { + name: "websocket dropped and may still reconnect", + leave: func(_ *SessionManagerCtx, session types.Session, websocket types.WebSocketPeer) error { + session.DisconnectWebSocketPeer(websocket, true) + return nil + }, + wantDestroyed: 0, + }, + { + name: "websocket closed", + leave: func(_ *SessionManagerCtx, session types.Session, websocket types.WebSocketPeer) error { + session.DisconnectWebSocketPeer(websocket, false) + return nil + }, + wantDestroyed: 1, + }, + { + name: "session deleted", + leave: func(manager *SessionManagerCtx, session types.Session, _ types.WebSocketPeer) error { + return manager.Delete(session.ID()) + }, + wantDestroyed: 1, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + manager := New(&config.Session{}) + session, _, err := manager.Create("member", profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + websocket := &fakeWebSocketPeer{} + session.ConnectWebSocketPeer(websocket) + // stops the timer of a delayed disconnect + t.Cleanup(func() { session.DisconnectWebSocketPeer(websocket, false) }) + + if err := tt.leave(manager, session, websocket); err != nil { + t.Fatalf("leave: %v", err) + } + peer := &fakeWebRTCPeer{} + session.SetWebRTCPeer(peer) + + if peer.destroyed != tt.wantDestroyed { + t.Errorf("peer destroyed %d times, want %d", peer.destroyed, tt.wantDestroyed) + } + }) + } +} diff --git a/server/internal/webrtc/track.go b/server/internal/webrtc/track.go index 8ddc7ea29..1fc1b557a 100644 --- a/server/internal/webrtc/track.go +++ b/server/internal/webrtc/track.go @@ -22,10 +22,13 @@ type Track struct { sample chan types.Sample paused bool + closed bool stream types.StreamSinkManager streamMu sync.Mutex } +var ErrTrackClosed = errors.New("track is closed") + type trackOption func(*Track) func WithRtcpChan(rtcp chan []rtcp.Packet) trackOption { @@ -64,6 +67,10 @@ func NewTrack(logger zerolog.Logger, codec codec.RTPCodec, connection *webrtc.Pe } func (t *Track) Shutdown() { + t.streamMu.Lock() + t.closed = true + t.streamMu.Unlock() + t.RemoveStream() close(t.sample) } @@ -119,6 +126,13 @@ func (t *Track) SetStream(stream types.StreamSinkManager) (bool, error) { t.streamMu.Lock() defer t.streamMu.Unlock() + // A peer can be destroyed while it is still being set up. A listener added + // after that would never be removed, and its samples would be written to a + // closed channel. + if t.closed { + return false, ErrTrackClosed + } + // if we already listen to the stream, do nothing if t.stream == stream { return false, nil diff --git a/server/internal/webrtc/track_test.go b/server/internal/webrtc/track_test.go new file mode 100644 index 000000000..f7c6f6e7b --- /dev/null +++ b/server/internal/webrtc/track_test.go @@ -0,0 +1,79 @@ +package webrtc + +import ( + "errors" + "testing" + + "github.com/pion/webrtc/v3" + "github.com/rs/zerolog" + + "github.com/m1k1o/neko/server/pkg/types" + "github.com/m1k1o/neko/server/pkg/types/codec" +) + +// listenerCountingStream counts its listeners. The embedded interface is nil: +// these tests must not reach any other method. +type listenerCountingStream struct { + types.StreamSinkManager + added, removed int +} + +func (s *listenerCountingStream) AddListener(types.SampleListener) error { + s.added++ + return nil +} + +func (s *listenerCountingStream) RemoveListener(types.SampleListener) error { + s.removed++ + return nil +} + +func newTestTrack(t *testing.T) *Track { + t.Helper() + connection, err := webrtc.NewPeerConnection(webrtc.Configuration{}) + if err != nil { + t.Fatalf("create peer connection: %v", err) + } + t.Cleanup(func() { connection.Close() }) + + track, err := NewTrack(zerolog.Nop(), codec.VP8(), connection) + if err != nil { + t.Fatalf("create track: %v", err) + } + return track +} + +func TestTrackStopsListeningOnShutdown(t *testing.T) { + track := newTestTrack(t) + stream := &listenerCountingStream{} + + if _, err := track.SetStream(stream); err != nil { + t.Fatalf("set stream: %v", err) + } + track.Shutdown() + + if stream.added != 1 || stream.removed != 1 { + t.Errorf("listener added %d times and removed %d times, want 1 and 1", stream.added, stream.removed) + } +} + +// A peer can be destroyed between its creation and the request that selects +// its video. The track is shut down by then; listening to a stream after that +// would keep the pipeline running for a track nobody removes. +func TestTrackDoesNotListenAfterShutdown(t *testing.T) { + track := newTestTrack(t) + stream := &listenerCountingStream{} + + track.Shutdown() + + if _, err := track.SetStream(stream); !errors.Is(err, ErrTrackClosed) { + t.Errorf("set stream after shutdown returned %v, want %v", err, ErrTrackClosed) + } + // unpausing is what starts audio + track.SetPaused(true) + track.SetPaused(false) + + if stream.added != 0 { + t.Errorf("listener added %d times after shutdown, want 0", stream.added) + } +} From a84c7136afba9dc077016298bec95bc5cdfef2e7 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:34:58 +0000 Subject: [PATCH 02/17] Track whether the session's WebRTC peer has connected per peer 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. --- server/internal/session/session.go | 21 ++++++--- server/internal/session/session_test.go | 58 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/server/internal/session/session.go b/server/internal/session/session.go index eb5e31ce3..1872914df 100644 --- a/server/internal/session/session.go +++ b/server/internal/session/session.go @@ -30,7 +30,10 @@ type SessionCtx struct { wsDelayedTimer *time.Timer webrtcPeer types.WebRTCPeer - webrtcMu sync.Mutex + // whether webrtcPeer has connected; unlike state.IsWatching this is never + // left over from a peer that was replaced + webrtcConnected bool + webrtcMu sync.Mutex } func (session *SessionCtx) ID() string { @@ -149,6 +152,8 @@ func (session *SessionCtx) ConnectWebSocketPeer(websocketPeer types.WebSocketPee // but after a delay. If the peer is connected again before the delay, the disconnect will be // cancelled. // +// A WebRTC peer that has not connected is destroyed with the websocket peer. +// // If the peer is not the current peer or the peer is nil, it will be ignored. func (session *SessionCtx) DisconnectWebSocketPeer(websocketPeer types.WebSocketPeer, delayed bool) { session.websocketMu.Lock() @@ -227,11 +232,11 @@ func (session *SessionCtx) DisconnectWebSocketPeer(websocketPeer types.WebSocket // must be set before SetWebRTCPeer looks at the websocket peer. That order // makes sure one of the two sees the other, whichever happens first. func (session *SessionCtx) destroyPendingWebRTCPeer() { - if session.state.IsWatching { - return - } + session.webrtcMu.Lock() + webrtcPeer, connected := session.webrtcPeer, session.webrtcConnected + session.webrtcMu.Unlock() - if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil { + if webrtcPeer != nil && !connected { webrtcPeer.Destroy() } } @@ -272,6 +277,9 @@ func (session *SessionCtx) Send(event string, payload any) { // Set webrtc peer and destroy the old one, if there is old one. func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) { session.webrtcMu.Lock() + if webrtcPeer != session.webrtcPeer { + session.webrtcConnected = false + } session.webrtcPeer, webrtcPeer = webrtcPeer, session.webrtcPeer session.webrtcMu.Unlock() @@ -299,6 +307,9 @@ func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) { func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, connected bool) { session.webrtcMu.Lock() isCurrentPeer := webrtcPeer == session.webrtcPeer + if isCurrentPeer { + session.webrtcConnected = connected + } session.webrtcMu.Unlock() if !isCurrentPeer { diff --git a/server/internal/session/session_test.go b/server/internal/session/session_test.go index d685d9066..53231952b 100644 --- a/server/internal/session/session_test.go +++ b/server/internal/session/session_test.go @@ -84,6 +84,16 @@ func TestWebRTCPeerThatNeverConnectedIsDestroyedWithItsSession(t *testing.T) { }, wantDestroyed: 0, }, + { + name: "websocket closes while the peer is streaming and was set again", + connected: true, + end: func(_ *SessionManagerCtx, session types.Session, websocket types.WebSocketPeer) error { + session.SetWebRTCPeer(session.GetWebRTCPeer()) + session.DisconnectWebSocketPeer(websocket, false) + return nil + }, + wantDestroyed: 0, + }, { name: "websocket drops and may still reconnect", end: func(_ *SessionManagerCtx, session types.Session, websocket types.WebSocketPeer) error { @@ -211,3 +221,51 @@ func TestWebRTCPeerSetAfterItsWebSocketLeftIsDestroyed(t *testing.T) { }) } } + +// A client that is watching can ask for media again, which replaces its peer. +// The session goes on counting as watching, because the closing of a replaced +// peer is ignored, but the new peer has not connected. +func TestWebRTCPeerThatReplacedAStreamingOneIsDestroyedUnlessItConnected(t *testing.T) { + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + + tests := []struct { + name string + connected bool + wantDestroyed int + }{ + {name: "websocket closes before the new peer connects", wantDestroyed: 1}, + {name: "websocket closes while the new peer is streaming", connected: true, wantDestroyed: 0}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + manager := New(&config.Session{}) + session, _, err := manager.Create("member", profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + websocket := &fakeWebSocketPeer{} + session.ConnectWebSocketPeer(websocket) + replaced := &fakeWebRTCPeer{} + session.SetWebRTCPeer(replaced) + session.SetWebRTCConnected(replaced, true) + + peer := &fakeWebRTCPeer{} + session.SetWebRTCPeer(peer) + if tt.connected { + session.SetWebRTCConnected(peer, true) + } + // the replaced peer reports its closing whenever it gets to it + session.SetWebRTCConnected(replaced, false) + + session.DisconnectWebSocketPeer(websocket, false) + + if replaced.destroyed != 1 { + t.Errorf("replaced peer destroyed %d times, want 1", replaced.destroyed) + } + if peer.destroyed != tt.wantDestroyed { + t.Errorf("peer destroyed %d times, want %d", peer.destroyed, tt.wantDestroyed) + } + }) + } +} From 25cad762e6d4a08f570a067287720dbdbcf7cc60 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:04:14 +0000 Subject: [PATCH 03/17] Do not destroy a peer that is already gone when a session ends 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. --- server/internal/session/manager.go | 10 +++++---- server/internal/session/session_test.go | 30 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/server/internal/session/manager.go b/server/internal/session/manager.go index a638595bc..b32d05198 100644 --- a/server/internal/session/manager.go +++ b/server/internal/session/manager.go @@ -159,8 +159,9 @@ func (manager *SessionManagerCtx) Delete(id string) error { session.DestroyWebSocketPeer("session deleted") } - if session.State().IsWatching { - session.GetWebRTCPeer().Destroy() + // a closing peer is cleared from another goroutine, so it can be gone already + if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil && session.State().IsWatching { + webrtcPeer.Destroy() } manager.emmiter.Emit("deleted", session) @@ -182,8 +183,9 @@ func (manager *SessionManagerCtx) Disconnect(id string) error { session.DestroyWebSocketPeer("session disconnected") } - if session.State().IsWatching { - session.GetWebRTCPeer().Destroy() + // a closing peer is cleared from another goroutine, so it can be gone already + if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil && session.State().IsWatching { + webrtcPeer.Destroy() } return nil diff --git a/server/internal/session/session_test.go b/server/internal/session/session_test.go index 53231952b..329a62e23 100644 --- a/server/internal/session/session_test.go +++ b/server/internal/session/session_test.go @@ -269,3 +269,33 @@ func TestWebRTCPeerThatReplacedAStreamingOneIsDestroyedUnlessItConnected(t *test }) } } + +// A closing peer is cleared from the session on another goroutine. A session +// that is deleted or disconnected at that moment can still count as watching +// with no peer left to destroy. +func TestSessionThatCountsAsWatchingWithoutAPeerCanBeEnded(t *testing.T) { + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + + tests := []struct { + name string + end func(manager *SessionManagerCtx, id string) error + }{ + {name: "session is deleted", end: (*SessionManagerCtx).Delete}, + {name: "session is disconnected", end: (*SessionManagerCtx).Disconnect}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + manager := New(&config.Session{}) + session, _, err := manager.Create("member", profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + session.(*SessionCtx).state.IsWatching = true + + if err := tt.end(manager, session.ID()); err != nil { + t.Fatalf("end session: %v", err) + } + }) + } +} From 127f678119137c22407389267407b5a20297feea Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:04:25 +0000 Subject: [PATCH 04/17] Keep the WebRTC peer of a websocket that took over 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. --- server/internal/session/session.go | 10 +++++++-- server/internal/session/session_test.go | 30 ++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/server/internal/session/session.go b/server/internal/session/session.go index 1872914df..5e22efcd3 100644 --- a/server/internal/session/session.go +++ b/server/internal/session/session.go @@ -213,7 +213,8 @@ func (session *SessionCtx) DisconnectWebSocketPeer(websocketPeer types.WebSocket session.manager.emmiter.Emit("disconnected", session) session.websocketMu.Lock() - if websocketPeer == session.websocketPeer { + isCurrentPeer = websocketPeer == session.websocketPeer + if isCurrentPeer { session.websocketPeer = nil } session.websocketMu.Unlock() @@ -222,7 +223,12 @@ func (session *SessionCtx) DisconnectWebSocketPeer(websocketPeer types.WebSocket // it carries the answer and the candidates. Without it the peer would stay // half-open for good and keep the capture pipelines it subscribed to // running with nobody watching. - session.destroyPendingWebRTCPeer() + // + // Another websocket may have connected while the listeners above ran. The + // peer is then left for that websocket to finish. + if isCurrentPeer { + session.destroyPendingWebRTCPeer() + } } // Destroy the WebRTC peer if it exists but has not connected. A connected peer diff --git a/server/internal/session/session_test.go b/server/internal/session/session_test.go index 329a62e23..8c37a33cf 100644 --- a/server/internal/session/session_test.go +++ b/server/internal/session/session_test.go @@ -40,7 +40,8 @@ func TestLegacyIsHost(t *testing.T) { } } -type fakeWebSocketPeer struct{} +// Not empty, so that two of them are never the same pointer. +type fakeWebSocketPeer struct{ _ bool } func (fakeWebSocketPeer) Send(event string, payload any) {} func (fakeWebSocketPeer) Ping() error { return nil } @@ -299,3 +300,30 @@ func TestSessionThatCountsAsWatchingWithoutAPeerCanBeEnded(t *testing.T) { }) } } + +// A client can be back on a new websocket before the old one is done leaving, +// because the session's listeners run in between. A peer the new websocket +// asked for is not the old one's to destroy. +func TestWebRTCPeerOfAWebSocketThatTookOverIsKept(t *testing.T) { + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + + manager := New(&config.Session{}) + session, _, err := manager.Create("member", profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + websocket := &fakeWebSocketPeer{} + session.ConnectWebSocketPeer(websocket) + + peer := &fakeWebRTCPeer{} + manager.OnDisconnected(func(session types.Session) { + session.ConnectWebSocketPeer(&fakeWebSocketPeer{}) + session.SetWebRTCPeer(peer) + }) + + session.DisconnectWebSocketPeer(websocket, false) + + if peer.destroyed != 0 { + t.Errorf("peer destroyed %d times, want 0", peer.destroyed) + } +} From 61e8c567209896869dc99f88d2d2e898dbe11e58 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:04:25 +0000 Subject: [PATCH 05/17] Test a paused track that is unpaused after shutdown 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. --- server/internal/webrtc/track_test.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/server/internal/webrtc/track_test.go b/server/internal/webrtc/track_test.go index f7c6f6e7b..1c2da0400 100644 --- a/server/internal/webrtc/track_test.go +++ b/server/internal/webrtc/track_test.go @@ -69,8 +69,24 @@ func TestTrackDoesNotListenAfterShutdown(t *testing.T) { if _, err := track.SetStream(stream); !errors.Is(err, ErrTrackClosed) { t.Errorf("set stream after shutdown returned %v, want %v", err, ErrTrackClosed) } - // unpausing is what starts audio + + if stream.added != 0 { + t.Errorf("listener added %d times after shutdown, want 0", stream.added) + } +} + +// The audio track gets its stream while it is paused, when the peer is +// created, and only listens once it is unpaused. That can come after the peer +// was destroyed. +func TestPausedTrackDoesNotListenAfterShutdown(t *testing.T) { + track := newTestTrack(t) + stream := &listenerCountingStream{} + track.SetPaused(true) + if _, err := track.SetStream(stream); err != nil { + t.Fatalf("set stream: %v", err) + } + track.Shutdown() track.SetPaused(false) if stream.added != 0 { From 627b53cb263a0e2767e2e42d1c5ee59ba87e1861 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:26:27 +0000 Subject: [PATCH 06/17] Do not destroy a peer that is already gone after a profile change 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. --- server/internal/session/session.go | 6 +++++- server/internal/session/session_test.go | 26 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/server/internal/session/session.go b/server/internal/session/session.go index 5e22efcd3..6aca92ecf 100644 --- a/server/internal/session/session.go +++ b/server/internal/session/session.go @@ -54,7 +54,11 @@ func (session *SessionCtx) profileChanged() { // otherwise webrtc destroy would trigger websocket reconnect. In case of kick event, webrtc destroy is called // before websocket destroy that delivers the information about the kick. time.AfterFunc(time.Second, func() { - session.GetWebRTCPeer().Destroy() + // the websocket destroyed below takes a peer that has not connected + // with it, so it can be gone by now + if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil { + webrtcPeer.Destroy() + } }) } diff --git a/server/internal/session/session_test.go b/server/internal/session/session_test.go index 8c37a33cf..88964af9a 100644 --- a/server/internal/session/session_test.go +++ b/server/internal/session/session_test.go @@ -2,6 +2,8 @@ package session import ( "testing" + "testing/synctest" + "time" "github.com/m1k1o/neko/server/internal/config" "github.com/m1k1o/neko/server/pkg/types" @@ -301,6 +303,30 @@ func TestSessionThatCountsAsWatchingWithoutAPeerCanBeEnded(t *testing.T) { } } +// A session that may no longer connect has its peer destroyed a moment after +// its websocket. The websocket takes a peer that has not connected with it, +// so a session that counts as watching can have no peer left by then. +func TestSessionThatCountsAsWatchingWithoutAPeerCanLoseItsLogin(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + + manager := New(&config.Session{}) + session, _, err := manager.Create("member", profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + session.(*SessionCtx).state.IsWatching = true + + profile.CanLogin = false + if err := manager.Update(session.ID(), profile); err != nil { + t.Fatalf("update session: %v", err) + } + + // the peer is destroyed from a timer + time.Sleep(2 * time.Second) + }) +} + // A client can be back on a new websocket before the old one is done leaving, // because the session's listeners run in between. A peer the new websocket // asked for is not the old one's to destroy. From ffe20f368e2848a96e1ec931f2dd65f84c9dcea1 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:10:50 +0000 Subject: [PATCH 07/17] Destroy the peer that is left when a session ends, whatever its state 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. --- server/internal/session/manager.go | 4 +- server/internal/session/session_test.go | 55 +++++++++++++------------ 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/server/internal/session/manager.go b/server/internal/session/manager.go index b32d05198..25de3c1b2 100644 --- a/server/internal/session/manager.go +++ b/server/internal/session/manager.go @@ -160,7 +160,7 @@ func (manager *SessionManagerCtx) Delete(id string) error { } // a closing peer is cleared from another goroutine, so it can be gone already - if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil && session.State().IsWatching { + if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil { webrtcPeer.Destroy() } @@ -184,7 +184,7 @@ func (manager *SessionManagerCtx) Disconnect(id string) error { } // a closing peer is cleared from another goroutine, so it can be gone already - if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil && session.State().IsWatching { + if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil { webrtcPeer.Destroy() } diff --git a/server/internal/session/session_test.go b/server/internal/session/session_test.go index 88964af9a..6ff367e3c 100644 --- a/server/internal/session/session_test.go +++ b/server/internal/session/session_test.go @@ -49,14 +49,15 @@ func (fakeWebSocketPeer) Send(event string, payload any) {} func (fakeWebSocketPeer) Ping() error { return nil } func (fakeWebSocketPeer) Destroy(reason string) {} -// fakeWebRTCPeer counts how often it was destroyed. The embedded interface is -// nil: these tests must not reach any other method. +// fakeWebRTCPeer records whether it was destroyed. Like the real peer it can +// be destroyed more than once. The embedded interface is nil: these tests +// must not reach any other method. type fakeWebRTCPeer struct { types.WebRTCPeer - destroyed int + destroyed bool } -func (p *fakeWebRTCPeer) Destroy() { p.destroyed++ } +func (p *fakeWebRTCPeer) Destroy() { p.destroyed = true } // A client can ask for media and vanish before it answers the offer. Its peer // never connects, so nothing but the session can end it; left alone it keeps @@ -68,7 +69,7 @@ func TestWebRTCPeerThatNeverConnectedIsDestroyedWithItsSession(t *testing.T) { name string connected bool end func(manager *SessionManagerCtx, session types.Session, websocket types.WebSocketPeer) error - wantDestroyed int + wantDestroyed bool }{ { name: "websocket closes before the peer connects", @@ -76,7 +77,7 @@ func TestWebRTCPeerThatNeverConnectedIsDestroyedWithItsSession(t *testing.T) { session.DisconnectWebSocketPeer(websocket, false) return nil }, - wantDestroyed: 1, + wantDestroyed: true, }, { name: "websocket closes while the peer is streaming", @@ -85,7 +86,7 @@ func TestWebRTCPeerThatNeverConnectedIsDestroyedWithItsSession(t *testing.T) { session.DisconnectWebSocketPeer(websocket, false) return nil }, - wantDestroyed: 0, + wantDestroyed: false, }, { name: "websocket closes while the peer is streaming and was set again", @@ -95,7 +96,7 @@ func TestWebRTCPeerThatNeverConnectedIsDestroyedWithItsSession(t *testing.T) { session.DisconnectWebSocketPeer(websocket, false) return nil }, - wantDestroyed: 0, + wantDestroyed: false, }, { name: "websocket drops and may still reconnect", @@ -103,21 +104,21 @@ func TestWebRTCPeerThatNeverConnectedIsDestroyedWithItsSession(t *testing.T) { session.DisconnectWebSocketPeer(websocket, true) return nil }, - wantDestroyed: 0, + wantDestroyed: false, }, { name: "session is deleted before the peer connects", end: func(manager *SessionManagerCtx, session types.Session, _ types.WebSocketPeer) error { return manager.Delete(session.ID()) }, - wantDestroyed: 1, + wantDestroyed: true, }, { name: "session is disconnected before the peer connects", end: func(manager *SessionManagerCtx, session types.Session, _ types.WebSocketPeer) error { return manager.Disconnect(session.ID()) }, - wantDestroyed: 1, + wantDestroyed: true, }, { name: "session is deleted while the peer is streaming", @@ -125,7 +126,7 @@ func TestWebRTCPeerThatNeverConnectedIsDestroyedWithItsSession(t *testing.T) { end: func(manager *SessionManagerCtx, session types.Session, _ types.WebSocketPeer) error { return manager.Delete(session.ID()) }, - wantDestroyed: 1, + wantDestroyed: true, }, } @@ -151,7 +152,7 @@ func TestWebRTCPeerThatNeverConnectedIsDestroyedWithItsSession(t *testing.T) { } if peer.destroyed != tt.wantDestroyed { - t.Errorf("peer destroyed %d times, want %d", peer.destroyed, tt.wantDestroyed) + t.Errorf("peer destroyed = %t, want %t", peer.destroyed, tt.wantDestroyed) } }) } @@ -166,14 +167,14 @@ func TestWebRTCPeerSetAfterItsWebSocketLeftIsDestroyed(t *testing.T) { tests := []struct { name string leave func(manager *SessionManagerCtx, session types.Session, websocket types.WebSocketPeer) error - wantDestroyed int + wantDestroyed bool }{ { name: "websocket still connected", leave: func(*SessionManagerCtx, types.Session, types.WebSocketPeer) error { return nil }, - wantDestroyed: 0, + wantDestroyed: false, }, { name: "websocket dropped and may still reconnect", @@ -181,7 +182,7 @@ func TestWebRTCPeerSetAfterItsWebSocketLeftIsDestroyed(t *testing.T) { session.DisconnectWebSocketPeer(websocket, true) return nil }, - wantDestroyed: 0, + wantDestroyed: false, }, { name: "websocket closed", @@ -189,14 +190,14 @@ func TestWebRTCPeerSetAfterItsWebSocketLeftIsDestroyed(t *testing.T) { session.DisconnectWebSocketPeer(websocket, false) return nil }, - wantDestroyed: 1, + wantDestroyed: true, }, { name: "session deleted", leave: func(manager *SessionManagerCtx, session types.Session, _ types.WebSocketPeer) error { return manager.Delete(session.ID()) }, - wantDestroyed: 1, + wantDestroyed: true, }, } @@ -219,7 +220,7 @@ func TestWebRTCPeerSetAfterItsWebSocketLeftIsDestroyed(t *testing.T) { session.SetWebRTCPeer(peer) if peer.destroyed != tt.wantDestroyed { - t.Errorf("peer destroyed %d times, want %d", peer.destroyed, tt.wantDestroyed) + t.Errorf("peer destroyed = %t, want %t", peer.destroyed, tt.wantDestroyed) } }) } @@ -234,10 +235,10 @@ func TestWebRTCPeerThatReplacedAStreamingOneIsDestroyedUnlessItConnected(t *test tests := []struct { name string connected bool - wantDestroyed int + wantDestroyed bool }{ - {name: "websocket closes before the new peer connects", wantDestroyed: 1}, - {name: "websocket closes while the new peer is streaming", connected: true, wantDestroyed: 0}, + {name: "websocket closes before the new peer connects", wantDestroyed: true}, + {name: "websocket closes while the new peer is streaming", connected: true, wantDestroyed: false}, } for _, tt := range tests { @@ -263,11 +264,11 @@ func TestWebRTCPeerThatReplacedAStreamingOneIsDestroyedUnlessItConnected(t *test session.DisconnectWebSocketPeer(websocket, false) - if replaced.destroyed != 1 { - t.Errorf("replaced peer destroyed %d times, want 1", replaced.destroyed) + if !replaced.destroyed { + t.Error("replaced peer was not destroyed") } if peer.destroyed != tt.wantDestroyed { - t.Errorf("peer destroyed %d times, want %d", peer.destroyed, tt.wantDestroyed) + t.Errorf("peer destroyed = %t, want %t", peer.destroyed, tt.wantDestroyed) } }) } @@ -349,7 +350,7 @@ func TestWebRTCPeerOfAWebSocketThatTookOverIsKept(t *testing.T) { session.DisconnectWebSocketPeer(websocket, false) - if peer.destroyed != 0 { - t.Errorf("peer destroyed %d times, want 0", peer.destroyed) + if peer.destroyed { + t.Error("peer of the websocket that took over was destroyed") } } From f6450f1372a06b74361ba416b90b7fae0d34b765 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:11:03 +0000 Subject: [PATCH 08/17] Keep the peer of a websocket that connected after the old one was cleared 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. --- server/internal/session/session.go | 40 ++++++++++++------------ server/internal/session/session_test.go | 41 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 20 deletions(-) diff --git a/server/internal/session/session.go b/server/internal/session/session.go index 6aca92ecf..e05c44abc 100644 --- a/server/internal/session/session.go +++ b/server/internal/session/session.go @@ -217,8 +217,7 @@ func (session *SessionCtx) DisconnectWebSocketPeer(websocketPeer types.WebSocket session.manager.emmiter.Emit("disconnected", session) session.websocketMu.Lock() - isCurrentPeer = websocketPeer == session.websocketPeer - if isCurrentPeer { + if websocketPeer == session.websocketPeer { session.websocketPeer = nil } session.websocketMu.Unlock() @@ -227,26 +226,33 @@ func (session *SessionCtx) DisconnectWebSocketPeer(websocketPeer types.WebSocket // it carries the answer and the candidates. Without it the peer would stay // half-open for good and keep the capture pipelines it subscribed to // running with nobody watching. - // - // Another websocket may have connected while the listeners above ran. The - // peer is then left for that websocket to finish. - if isCurrentPeer { - session.destroyPendingWebRTCPeer() - } + session.destroyPendingWebRTCPeer() } -// Destroy the WebRTC peer if it exists but has not connected. A connected peer -// is left alone: it does not need the websocket to keep streaming. +// Destroy the WebRTC peer if it has not connected and no websocket peer is +// left to connect it. A connected peer is left alone: it does not need the +// websocket to keep streaming. // // The websocket peer must be cleared before this is called, and a WebRTC peer -// must be set before SetWebRTCPeer looks at the websocket peer. That order -// makes sure one of the two sees the other, whichever happens first. +// must be set before it. That order makes sure a disconnecting websocket and +// a peer being set see each other, whichever happens first. func (session *SessionCtx) destroyPendingWebRTCPeer() { session.webrtcMu.Lock() webrtcPeer, connected := session.webrtcPeer, session.webrtcConnected session.webrtcMu.Unlock() - if webrtcPeer != nil && !connected { + if webrtcPeer == nil || connected { + return + } + + // Another websocket may have connected by now, and the peer may be one it + // asked for. Looking at the websocket after the peer makes sure a peer + // that was set while a websocket is connected is left to that websocket. + session.websocketMu.Lock() + hasWebSocket := session.websocketPeer != nil + session.websocketMu.Unlock() + + if !hasWebSocket { webrtcPeer.Destroy() } } @@ -299,13 +305,7 @@ func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) { // The websocket can go away while a peer is being created for it. Its // disconnect found no peer to destroy then, so it is done here. - session.websocketMu.Lock() - hasWebSocket := session.websocketPeer != nil - session.websocketMu.Unlock() - - if !hasWebSocket { - session.destroyPendingWebRTCPeer() - } + session.destroyPendingWebRTCPeer() } // Set if current webrtc peer is connected or not. Since there might be lefover calls from diff --git a/server/internal/session/session_test.go b/server/internal/session/session_test.go index 6ff367e3c..856a6c557 100644 --- a/server/internal/session/session_test.go +++ b/server/internal/session/session_test.go @@ -1,6 +1,7 @@ package session import ( + "runtime" "testing" "testing/synctest" "time" @@ -354,3 +355,43 @@ func TestWebRTCPeerOfAWebSocketThatTookOverIsKept(t *testing.T) { t.Error("peer of the websocket that took over was destroyed") } } + +// The new websocket can also connect after the old one was cleared from the +// session, and ask for a peer before the old one looks for a peer to destroy. +func TestWebRTCPeerOfAWebSocketThatConnectedAfterTheOldOneWasClearedIsKept(t *testing.T) { + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + + manager := New(&config.Session{}) + created, _, err := manager.Create("member", profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + session := created.(*SessionCtx) + websocket := &fakeWebSocketPeer{} + session.ConnectWebSocketPeer(websocket) + + // holds the old websocket where it looks for a peer to destroy + session.webrtcMu.Lock() + disconnected := make(chan struct{}) + go func() { + defer close(disconnected) + session.DisconnectWebSocketPeer(websocket, false) + }() + for cleared := false; !cleared; { + session.websocketMu.Lock() + cleared = session.websocketPeer == nil + session.websocketMu.Unlock() + runtime.Gosched() + } + + // the lock is held, so the peer is set as SetWebRTCPeer would set it + session.ConnectWebSocketPeer(&fakeWebSocketPeer{}) + peer := &fakeWebRTCPeer{} + session.webrtcPeer, session.webrtcConnected = peer, false + session.webrtcMu.Unlock() + <-disconnected + + if peer.destroyed { + t.Error("peer of the websocket that connected was destroyed") + } +} From f9d4a0da63456bbfa91038eabcd3da7e71b02e7c Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:11:05 +0000 Subject: [PATCH 09/17] Test with a real peer that a client that left stops listening 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. --- server/internal/webrtc/manager_test.go | 144 +++++++++++++++++++++++++ server/internal/webrtc/track_test.go | 21 ++++ 2 files changed, 165 insertions(+) create mode 100644 server/internal/webrtc/manager_test.go diff --git a/server/internal/webrtc/manager_test.go b/server/internal/webrtc/manager_test.go new file mode 100644 index 000000000..4f29fc3c2 --- /dev/null +++ b/server/internal/webrtc/manager_test.go @@ -0,0 +1,144 @@ +package webrtc + +import ( + "testing" + "time" + + "github.com/m1k1o/neko/server/internal/config" + "github.com/m1k1o/neko/server/internal/session" + "github.com/m1k1o/neko/server/pkg/types" + "github.com/m1k1o/neko/server/pkg/types/codec" + "github.com/m1k1o/neko/server/pkg/utils" +) + +// listenerCountingCapture has one audio and one video stream. The embedded +// interface is nil: these tests must not reach any other method. +type listenerCountingCapture struct { + types.CaptureManager + audio, video *listenerCountingStream +} + +func (c *listenerCountingCapture) Audio() types.StreamSinkManager { return c.audio } +func (c *listenerCountingCapture) Video() types.StreamSelectorManager { return c } + +func (c *listenerCountingCapture) IDs() []string { return []string{c.video.ID()} } +func (c *listenerCountingCapture) Codec() codec.RTPCodec { return c.video.Codec() } +func (c *listenerCountingCapture) GetStream(types.StreamSelector) (types.StreamSinkManager, bool) { + return c.video, true +} + +func (c *listenerCountingCapture) listeners() int { + return c.audio.listeners() + c.video.listeners() +} + +type nopWebSocketPeer struct{ _ bool } + +func (nopWebSocketPeer) Send(event string, payload any) {} +func (nopWebSocketPeer) Ping() error { return nil } +func (nopWebSocketPeer) Destroy(reason string) {} + +// What the signal/request handler does for a client that wants video and audio. +func requestMedia(manager *WebRTCManagerCtx, session types.Session) error { + _, peer, err := manager.CreatePeer(session) + if err != nil { + return err + } + + err = peer.SetVideo(types.PeerVideoRequest{Selector: &types.StreamSelector{ID: "test"}}) + if err != nil { + return err + } + + disabled := false + return peer.SetAudio(types.PeerAudioRequest{Disabled: &disabled}) +} + +// A real peer is closed on its own goroutines: destroying it only starts that. +// These clients ask for media and leave without answering the offer. In the +// end nothing may listen to the capture streams. +func TestPeerOfAClientThatLeftStopsListening(t *testing.T) { + capture := &listenerCountingCapture{ + audio: &listenerCountingStream{codec: codec.Opus()}, + video: &listenerCountingStream{codec: codec.VP8()}, + } + manager := New(nil, capture, &config.WebRTC{ICETrickle: true}) + sessions := session.New(&config.Session{}) + + request := func(t *testing.T, session types.Session) { + t.Helper() + if err := requestMedia(manager, session); err != nil { + t.Fatalf("request media: %v", err) + } + if capture.listeners() == 0 { + t.Fatal("nothing listens after the request") + } + } + + tests := []struct { + name string + run func(t *testing.T, session types.Session, websocket types.WebSocketPeer) + }{ + { + name: "websocket closes after the offer", + run: func(t *testing.T, session types.Session, websocket types.WebSocketPeer) { + request(t, session) + session.DisconnectWebSocketPeer(websocket, false) + }, + }, + { + name: "session is deleted after the offer", + run: func(t *testing.T, session types.Session, _ types.WebSocketPeer) { + request(t, session) + if err := sessions.Delete(session.ID()); err != nil { + t.Fatalf("delete session: %v", err) + } + }, + }, + { + name: "client asks again and its websocket closes", + run: func(t *testing.T, session types.Session, websocket types.WebSocketPeer) { + request(t, session) + request(t, session) + session.DisconnectWebSocketPeer(websocket, false) + }, + }, + { + name: "websocket closes while the peer is created", + run: func(t *testing.T, session types.Session, websocket types.WebSocketPeer) { + session.DisconnectWebSocketPeer(websocket, false) + // the peer is closed when it is set, which fails the request + if err := requestMedia(manager, session); err == nil { + t.Error("request media succeeded without a websocket") + } + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // metrics are registered once per session id + id, err := utils.NewUID(16) + if err != nil { + t.Fatalf("create session id: %v", err) + } + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + session, _, err := sessions.Create(id, profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + websocket := &nopWebSocketPeer{} + session.ConnectWebSocketPeer(websocket) + + tt.run(t, session, websocket) + + deadline := time.Now().Add(5 * time.Second) + for capture.listeners() != 0 || session.GetWebRTCPeer() != nil { + if time.Now().After(deadline) { + t.Fatalf("%d listeners left, peer left: %t", + capture.listeners(), session.GetWebRTCPeer() != nil) + } + time.Sleep(time.Millisecond) + } + }) + } +} diff --git a/server/internal/webrtc/track_test.go b/server/internal/webrtc/track_test.go index 1c2da0400..fa44c487a 100644 --- a/server/internal/webrtc/track_test.go +++ b/server/internal/webrtc/track_test.go @@ -2,6 +2,7 @@ package webrtc import ( "errors" + "sync" "testing" "github.com/pion/webrtc/v3" @@ -15,19 +16,39 @@ import ( // these tests must not reach any other method. type listenerCountingStream struct { types.StreamSinkManager + codec codec.RTPCodec + + mu sync.Mutex added, removed int } +func (s *listenerCountingStream) ID() string { return "test" } + +func (s *listenerCountingStream) Codec() codec.RTPCodec { return s.codec } + func (s *listenerCountingStream) AddListener(types.SampleListener) error { + s.mu.Lock() + defer s.mu.Unlock() + s.added++ return nil } func (s *listenerCountingStream) RemoveListener(types.SampleListener) error { + s.mu.Lock() + defer s.mu.Unlock() + s.removed++ return nil } +func (s *listenerCountingStream) listeners() int { + s.mu.Lock() + defer s.mu.Unlock() + + return s.added - s.removed +} + func newTestTrack(t *testing.T) *Track { t.Helper() connection, err := webrtc.NewPeerConnection(webrtc.Configuration{}) From a90d5c8028aee79f0bd102c8e4adfaffb2db7890 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:10:45 +0000 Subject: [PATCH 10/17] Test that a peer is destroyed when its dropped websocket stays away 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. --- server/internal/session/session_test.go | 63 +++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/server/internal/session/session_test.go b/server/internal/session/session_test.go index 856a6c557..a24dbc458 100644 --- a/server/internal/session/session_test.go +++ b/server/internal/session/session_test.go @@ -159,6 +159,69 @@ func TestWebRTCPeerThatNeverConnectedIsDestroyedWithItsSession(t *testing.T) { } } +// A websocket that dropped is given time to come back, and a peer that has not +// connected is kept for that long. When the time is up the peer is destroyed, +// unless the client is back on a new websocket by then. +func TestWebRTCPeerThatNeverConnectedIsKeptWhileItsWebSocketMayReconnect(t *testing.T) { + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + + tests := []struct { + name string + // how long after the drop the client is back, if it comes back + reconnectAfter time.Duration + // how long after the drop the peer is looked at + after time.Duration + wantDestroyed bool + }{ + { + name: "time is not up", + after: WS_DELAYED_DURATION - time.Second, + wantDestroyed: false, + }, + { + name: "time is up", + after: WS_DELAYED_DURATION + time.Second, + wantDestroyed: true, + }, + { + name: "client came back in time", + reconnectAfter: WS_DELAYED_DURATION - time.Second, + after: WS_DELAYED_DURATION + time.Second, + wantDestroyed: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + manager := New(&config.Session{}) + session, _, err := manager.Create("member", profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + websocket := &fakeWebSocketPeer{} + session.ConnectWebSocketPeer(websocket) + // stops the timer of a delayed disconnect + defer session.DisconnectWebSocketPeer(websocket, false) + peer := &fakeWebRTCPeer{} + session.SetWebRTCPeer(peer) + + session.DisconnectWebSocketPeer(websocket, true) + if tt.reconnectAfter != 0 { + time.Sleep(tt.reconnectAfter) + session.ConnectWebSocketPeer(&fakeWebSocketPeer{}) + } + time.Sleep(tt.after - tt.reconnectAfter) + synctest.Wait() + + if peer.destroyed != tt.wantDestroyed { + t.Errorf("peer destroyed = %t, want %t", peer.destroyed, tt.wantDestroyed) + } + }) + }) + } +} + // Creating a peer takes a while, and the websocket that asked for it can be // gone by the time the peer is attached to the session. Its disconnect could // not destroy a peer that did not exist yet. From 5e15a27addb89d28dea624bb193316c3d550e29a Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:10:48 +0000 Subject: [PATCH 11/17] Destroy a peer that has not connected when its session may no longer 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. --- server/internal/session/session.go | 7 ++--- server/internal/session/session_test.go | 35 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/server/internal/session/session.go b/server/internal/session/session.go index e05c44abc..7318ef4bd 100644 --- a/server/internal/session/session.go +++ b/server/internal/session/session.go @@ -49,13 +49,14 @@ func (session *SessionCtx) profileChanged() { session.ClearHost() } - if (!session.profile.CanConnect || !session.profile.CanLogin || !session.profile.CanWatch) && session.state.IsWatching { + if !session.profile.CanConnect || !session.profile.CanLogin || !session.profile.CanWatch { // TODO: Needed for legacy implementation. Websocket must die before webrtc and deliver signal close message // otherwise webrtc destroy would trigger websocket reconnect. In case of kick event, webrtc destroy is called // before websocket destroy that delivers the information about the kick. time.AfterFunc(time.Second, func() { - // the websocket destroyed below takes a peer that has not connected - // with it, so it can be gone by now + // A peer that has not connected is destroyed too, or it could + // still connect. There may be no peer at all, or the websocket + // destroyed below took one that had not connected with it. if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil { webrtcPeer.Destroy() } diff --git a/server/internal/session/session_test.go b/server/internal/session/session_test.go index a24dbc458..d9855ed09 100644 --- a/server/internal/session/session_test.go +++ b/server/internal/session/session_test.go @@ -60,6 +60,11 @@ type fakeWebRTCPeer struct { func (p *fakeWebRTCPeer) Destroy() { p.destroyed = true } +// A change of profile also sets the paused state of the peer. +type pausableWebRTCPeer struct{ fakeWebRTCPeer } + +func (*pausableWebRTCPeer) SetPaused(isPaused bool) error { return nil } + // A client can ask for media and vanish before it answers the offer. Its peer // never connects, so nothing but the session can end it; left alone it keeps // the capture pipelines it subscribed to running with nobody watching. @@ -392,6 +397,36 @@ func TestSessionThatCountsAsWatchingWithoutAPeerCanLoseItsLogin(t *testing.T) { }) } +// A session that may no longer watch keeps its websocket, so a peer that has +// not connected is not taken with it. Left alone it could still connect, and +// the session would watch without being allowed to. +func TestWebRTCPeerThatNeverConnectedIsDestroyedWhenItsSessionMayNoLongerWatch(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + + manager := New(&config.Session{}) + session, _, err := manager.Create("member", profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + session.ConnectWebSocketPeer(&fakeWebSocketPeer{}) + peer := &pausableWebRTCPeer{} + session.SetWebRTCPeer(peer) + + profile.CanWatch = false + if err := manager.Update(session.ID(), profile); err != nil { + t.Fatalf("update session: %v", err) + } + + // the peer is destroyed from a timer + time.Sleep(2 * time.Second) + synctest.Wait() + if !peer.destroyed { + t.Error("peer was not destroyed") + } + }) +} + // A client can be back on a new websocket before the old one is done leaving, // because the session's listeners run in between. A peer the new websocket // asked for is not the old one's to destroy. From 9f230ef5b56ca6fddadf8020f4969f6492c025d5 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:10:50 +0000 Subject: [PATCH 12/17] Say when a peer that has not connected needs the websocket 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. --- server/internal/session/session.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server/internal/session/session.go b/server/internal/session/session.go index 7318ef4bd..a6934b101 100644 --- a/server/internal/session/session.go +++ b/server/internal/session/session.go @@ -223,10 +223,12 @@ func (session *SessionCtx) DisconnectWebSocketPeer(websocketPeer types.WebSocket } session.websocketMu.Unlock() - // A WebRTC peer that has not connected yet needs this websocket to do so: - // it carries the answer and the candidates. Without it the peer would stay - // half-open for good and keep the capture pipelines it subscribed to - // running with nobody watching. + // A WebRTC peer that has not connected yet is not waited for. Until the + // client has answered, the peer needs this websocket to connect at all: + // without it the peer would stay half-open for good and keep the capture + // pipelines it subscribed to running with nobody watching. Once the client + // has answered, the peer could still connect without the websocket, but a + // client whose websocket is gone for good has left. session.destroyPendingWebRTCPeer() } From 0721fe2f305958a11615335c2181fdf49b82a26c Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sun, 15 Nov 2026 03:46:21 +0000 Subject: [PATCH 13/17] Keep the peer of a session that may watch again 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. --- server/internal/session/session.go | 10 ++++++ server/internal/session/session_test.go | 48 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/server/internal/session/session.go b/server/internal/session/session.go index a6934b101..59469754e 100644 --- a/server/internal/session/session.go +++ b/server/internal/session/session.go @@ -54,6 +54,16 @@ func (session *SessionCtx) profileChanged() { // otherwise webrtc destroy would trigger websocket reconnect. In case of kick event, webrtc destroy is called // before websocket destroy that delivers the information about the kick. time.AfterFunc(time.Second, func() { + // The profile can change again before the timer fires. A peer of + // a session that may watch again is not this timer's to destroy. + session.manager.sessionsMu.Lock() + profile := session.profile + session.manager.sessionsMu.Unlock() + + if profile.CanConnect && profile.CanLogin && profile.CanWatch { + return + } + // A peer that has not connected is destroyed too, or it could // still connect. There may be no peer at all, or the websocket // destroyed below took one that had not connected with it. diff --git a/server/internal/session/session_test.go b/server/internal/session/session_test.go index d9855ed09..1795821e7 100644 --- a/server/internal/session/session_test.go +++ b/server/internal/session/session_test.go @@ -427,6 +427,54 @@ func TestWebRTCPeerThatNeverConnectedIsDestroyedWhenItsSessionMayNoLongerWatch(t }) } +// The peer of a session that may no longer watch is destroyed a moment later. +// A session that may watch again by then keeps the peer it has. +func TestWebRTCPeerIsKeptWhenItsSessionMayWatchAgain(t *testing.T) { + tests := []struct { + name string + connected bool + }{ + {name: "peer that has not connected"}, + {name: "peer that is streaming", connected: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + + manager := New(&config.Session{}) + session, _, err := manager.Create("member", profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + session.ConnectWebSocketPeer(&fakeWebSocketPeer{}) + + revoked := profile + revoked.CanWatch = false + if err := manager.Update(session.ID(), revoked); err != nil { + t.Fatalf("update session: %v", err) + } + if err := manager.Update(session.ID(), profile); err != nil { + t.Fatalf("update session: %v", err) + } + + peer := &pausableWebRTCPeer{} + session.SetWebRTCPeer(peer) + if tt.connected { + session.SetWebRTCConnected(peer, true) + } + + time.Sleep(2 * time.Second) + synctest.Wait() + if peer.destroyed { + t.Error("peer was destroyed") + } + }) + }) + } +} + // A client can be back on a new websocket before the old one is done leaving, // because the session's listeners run in between. A peer the new websocket // asked for is not the old one's to destroy. From 53eeadf27b96a2091a0bf0a75db962ca36c015dc Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sun, 15 Nov 2026 03:46:21 +0000 Subject: [PATCH 14/17] Give each client in the leaving peer test its own capture 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. --- server/internal/webrtc/manager_test.go | 74 +++++++++++++++----------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/server/internal/webrtc/manager_test.go b/server/internal/webrtc/manager_test.go index 4f29fc3c2..745639cca 100644 --- a/server/internal/webrtc/manager_test.go +++ b/server/internal/webrtc/manager_test.go @@ -53,61 +53,64 @@ func requestMedia(manager *WebRTCManagerCtx, session types.Session) error { return peer.SetAudio(types.PeerAudioRequest{Disabled: &disabled}) } +// leavingClient is a session with a websocket on a capture of its own, so +// that what one client leaves listening is not counted for the next. +type leavingClient struct { + capture *listenerCountingCapture + manager *WebRTCManagerCtx + sessions *session.SessionManagerCtx + session types.Session + websocket types.WebSocketPeer +} + +func (c *leavingClient) request(t *testing.T) { + t.Helper() + if err := requestMedia(c.manager, c.session); err != nil { + t.Fatalf("request media: %v", err) + } + if c.capture.listeners() == 0 { + t.Fatal("nothing listens after the request") + } +} + // A real peer is closed on its own goroutines: destroying it only starts that. // These clients ask for media and leave without answering the offer. In the // end nothing may listen to the capture streams. func TestPeerOfAClientThatLeftStopsListening(t *testing.T) { - capture := &listenerCountingCapture{ - audio: &listenerCountingStream{codec: codec.Opus()}, - video: &listenerCountingStream{codec: codec.VP8()}, - } - manager := New(nil, capture, &config.WebRTC{ICETrickle: true}) - sessions := session.New(&config.Session{}) - - request := func(t *testing.T, session types.Session) { - t.Helper() - if err := requestMedia(manager, session); err != nil { - t.Fatalf("request media: %v", err) - } - if capture.listeners() == 0 { - t.Fatal("nothing listens after the request") - } - } - tests := []struct { name string - run func(t *testing.T, session types.Session, websocket types.WebSocketPeer) + run func(t *testing.T, c *leavingClient) }{ { name: "websocket closes after the offer", - run: func(t *testing.T, session types.Session, websocket types.WebSocketPeer) { - request(t, session) - session.DisconnectWebSocketPeer(websocket, false) + run: func(t *testing.T, c *leavingClient) { + c.request(t) + c.session.DisconnectWebSocketPeer(c.websocket, false) }, }, { name: "session is deleted after the offer", - run: func(t *testing.T, session types.Session, _ types.WebSocketPeer) { - request(t, session) - if err := sessions.Delete(session.ID()); err != nil { + run: func(t *testing.T, c *leavingClient) { + c.request(t) + if err := c.sessions.Delete(c.session.ID()); err != nil { t.Fatalf("delete session: %v", err) } }, }, { name: "client asks again and its websocket closes", - run: func(t *testing.T, session types.Session, websocket types.WebSocketPeer) { - request(t, session) - request(t, session) - session.DisconnectWebSocketPeer(websocket, false) + run: func(t *testing.T, c *leavingClient) { + c.request(t) + c.request(t) + c.session.DisconnectWebSocketPeer(c.websocket, false) }, }, { name: "websocket closes while the peer is created", - run: func(t *testing.T, session types.Session, websocket types.WebSocketPeer) { - session.DisconnectWebSocketPeer(websocket, false) + run: func(t *testing.T, c *leavingClient) { + c.session.DisconnectWebSocketPeer(c.websocket, false) // the peer is closed when it is set, which fails the request - if err := requestMedia(manager, session); err == nil { + if err := requestMedia(c.manager, c.session); err == nil { t.Error("request media succeeded without a websocket") } }, @@ -116,6 +119,13 @@ func TestPeerOfAClientThatLeftStopsListening(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + capture := &listenerCountingCapture{ + audio: &listenerCountingStream{codec: codec.Opus()}, + video: &listenerCountingStream{codec: codec.VP8()}, + } + manager := New(nil, capture, &config.WebRTC{ICETrickle: true}) + sessions := session.New(&config.Session{}) + // metrics are registered once per session id id, err := utils.NewUID(16) if err != nil { @@ -129,7 +139,7 @@ func TestPeerOfAClientThatLeftStopsListening(t *testing.T) { websocket := &nopWebSocketPeer{} session.ConnectWebSocketPeer(websocket) - tt.run(t, session, websocket) + tt.run(t, &leavingClient{capture, manager, sessions, session, websocket}) deadline := time.Now().Add(5 * time.Second) for capture.listeners() != 0 || session.GetWebRTCPeer() != nil { From 2908bfb7214e4851a181c0019be58d0d56486e42 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sun, 15 Nov 2026 03:46:21 +0000 Subject: [PATCH 15/17] Name the leaving peer subtest after what it does The websocket is disconnected before the peer is created, not while. --- server/internal/webrtc/manager_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/internal/webrtc/manager_test.go b/server/internal/webrtc/manager_test.go index 745639cca..a56cf9d48 100644 --- a/server/internal/webrtc/manager_test.go +++ b/server/internal/webrtc/manager_test.go @@ -106,7 +106,7 @@ func TestPeerOfAClientThatLeftStopsListening(t *testing.T) { }, }, { - name: "websocket closes while the peer is created", + name: "websocket is gone when the peer is set", run: func(t *testing.T, c *leavingClient) { c.session.DisconnectWebSocketPeer(c.websocket, false) // the peer is closed when it is set, which fails the request From 8640762aadf844d2a9809f289f10d4174eac6055 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:15:53 +0000 Subject: [PATCH 16/17] Do not wait for a busy peer when destroying it 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. --- server/internal/webrtc/manager_test.go | 74 +++++++++++++++++++++++--- server/internal/webrtc/peer.go | 18 ++++++- server/internal/webrtc/track_test.go | 10 ++++ 3 files changed, 93 insertions(+), 9 deletions(-) diff --git a/server/internal/webrtc/manager_test.go b/server/internal/webrtc/manager_test.go index a56cf9d48..7411235ed 100644 --- a/server/internal/webrtc/manager_test.go +++ b/server/internal/webrtc/manager_test.go @@ -141,14 +141,72 @@ func TestPeerOfAClientThatLeftStopsListening(t *testing.T) { tt.run(t, &leavingClient{capture, manager, sessions, session, websocket}) - deadline := time.Now().Add(5 * time.Second) - for capture.listeners() != 0 || session.GetWebRTCPeer() != nil { - if time.Now().After(deadline) { - t.Fatalf("%d listeners left, peer left: %t", - capture.listeners(), session.GetWebRTCPeer() != nil) - } - time.Sleep(time.Millisecond) - } + waitUntilNothingListens(t, capture, session) }) } } + +func waitUntilNothingListens(t *testing.T, capture *listenerCountingCapture, session types.Session) { + t.Helper() + + deadline := time.Now().Add(5 * time.Second) + for capture.listeners() != 0 || session.GetWebRTCPeer() != nil { + if time.Now().After(deadline) { + t.Fatalf("%d listeners left, peer left: %t", + capture.listeners(), session.GetWebRTCPeer() != nil) + } + time.Sleep(time.Millisecond) + } +} + +// The first listener of a stream starts its capture pipeline, which takes +// seconds, and its peer is busy for that long. A session is logged out under +// a lock that every login needs, so ending it must not wait for the pipeline. +func TestSessionIsDeletedWithoutWaitingForItsPeerToStartAStream(t *testing.T) { + capture := &listenerCountingCapture{ + audio: &listenerCountingStream{codec: codec.Opus()}, + video: &listenerCountingStream{ + codec: codec.VP8(), + starting: make(chan struct{}), + start: make(chan struct{}), + }, + } + manager := New(nil, capture, &config.WebRTC{ICETrickle: true}) + sessions := session.New(&config.Session{}) + + // metrics are registered once per session id + id, err := utils.NewUID(16) + if err != nil { + t.Fatalf("create session id: %v", err) + } + profile := types.MemberProfile{CanLogin: true, CanConnect: true, CanWatch: true} + session, _, err := sessions.Create(id, profile) + if err != nil { + t.Fatalf("create session: %v", err) + } + session.ConnectWebSocketPeer(&nopWebSocketPeer{}) + + requested := make(chan struct{}) + go func() { + defer close(requested) + // fails or not, depending on how far it got when the peer closed + _ = requestMedia(manager, session) + }() + <-capture.video.starting + + deleted := make(chan error, 1) + go func() { deleted <- sessions.Delete(id) }() + + select { + case err := <-deleted: + if err != nil { + t.Errorf("delete session: %v", err) + } + case <-time.After(5 * time.Second): + t.Error("deleting the session waits for the stream to start") + } + + close(capture.video.start) + <-requested + waitUntilNothingListens(t, capture, session) +} diff --git a/server/internal/webrtc/peer.go b/server/internal/webrtc/peer.go index 3310b624d..ffce16fbf 100644 --- a/server/internal/webrtc/peer.go +++ b/server/internal/webrtc/peer.go @@ -108,10 +108,26 @@ func (peer *WebRTCPeerCtx) SetCandidate(candidate webrtc.ICECandidateInit) error } // TODO: Add shutdown function? +// +// A peer that is busy is not waited for: it is closed once it is free. A peer +// is busy for seconds while its first listener starts a capture pipeline, and +// sessions are ended under a lock that every login needs. func (peer *WebRTCPeerCtx) Destroy() { - peer.mu.Lock() + if !peer.mu.TryLock() { + go func() { + peer.mu.Lock() + defer peer.mu.Unlock() + + peer.close() + }() + return + } defer peer.mu.Unlock() + peer.close() +} + +func (peer *WebRTCPeerCtx) close() { var err error // if peer connection is not closed, close it diff --git a/server/internal/webrtc/track_test.go b/server/internal/webrtc/track_test.go index fa44c487a..896b1b237 100644 --- a/server/internal/webrtc/track_test.go +++ b/server/internal/webrtc/track_test.go @@ -20,6 +20,11 @@ type listenerCountingStream struct { mu sync.Mutex added, removed int + + // When set, a listener is not added until start is closed, like one that + // has to wait for a capture pipeline. The first to wait closes starting. + starting, start chan struct{} + startingOnce sync.Once } func (s *listenerCountingStream) ID() string { return "test" } @@ -27,6 +32,11 @@ func (s *listenerCountingStream) ID() string { return "test" } func (s *listenerCountingStream) Codec() codec.RTPCodec { return s.codec } func (s *listenerCountingStream) AddListener(types.SampleListener) error { + if s.start != nil { + s.startingOnce.Do(func() { close(s.starting) }) + <-s.start + } + s.mu.Lock() defer s.mu.Unlock() From a670b8dfc70c7af55341e8455d0aafb15027ee40 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:05:26 +0000 Subject: [PATCH 17/17] Remove the bus watch when a pipeline is destroyed The watch added for every pipeline was never removed. It holds a reference to the pipeline's bus, so each destroyed pipeline left its bus and the two sockets of the bus's control socket pair behind. --- server/pkg/gst/gst.c | 6 ++++++ server/pkg/gst/gst_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/server/pkg/gst/gst.c b/server/pkg/gst/gst.c index f1cc9a14a..468a2688b 100644 --- a/server/pkg/gst/gst.c +++ b/server/pkg/gst/gst.c @@ -216,6 +216,12 @@ void gstreamer_pipeline_destory(GstPipelineCtx *ctx) { // set null state gst_element_set_state(GST_ELEMENT(ctx->pipeline), GST_STATE_NULL); + // the watch holds a reference to the bus, which would keep it and its + // control sockets after the pipeline is gone + GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(ctx->pipeline)); + gst_bus_remove_watch(bus); + gst_object_unref(bus); + if (ctx->appsink) { gst_object_unref(ctx->appsink); ctx->appsink = NULL; diff --git a/server/pkg/gst/gst_test.go b/server/pkg/gst/gst_test.go index 52ac0bf2b..80c64c423 100644 --- a/server/pkg/gst/gst_test.go +++ b/server/pkg/gst/gst_test.go @@ -1,6 +1,7 @@ package gst import ( + "os" "strings" "testing" "time" @@ -95,6 +96,38 @@ func TestCreatePipelineReleasesLockBeforeSlowCUDAProbe(t *testing.T) { } } +func TestDestroyClosesPipelineFiles(t *testing.T) { + openFiles := func() int { + entries, err := os.ReadDir("/proc/self/fd") + if err != nil { + t.Fatalf("count open files: %v", err) + } + return len(entries) + } + + runPipeline := func() { + p, err := CreatePipeline("fakesrc num-buffers=1 ! fakesink") + if err != nil { + t.Fatalf("CreatePipeline() error = %v", err) + } + p.Play() + p.Destroy() + } + + // the first pipeline opens files that GStreamer keeps for the process + runPipeline() + + const pipelines = 10 + before := openFiles() + for i := 0; i < pipelines; i++ { + runPipeline() + } + + if left := openFiles() - before; left > 0 { + t.Fatalf("%d pipelines left %d files open", pipelines, left) + } +} + func TestNVENCFailureDetail(t *testing.T) { t.Parallel()