Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

fix(plugins): close a plugin's SSE streams when it unloads

Gabe Kangas33ed1e8094cc
diff --git a/pluginhost/pluginhost.go b/pluginhost/pluginhost.go
index 48895cccc7..643ef3b092 100644
--- a/pluginhost/pluginhost.go
+++ b/pluginhost/pluginhost.go
@@ -669,7 +669,13 @@ func New(ctx context.Context, deps Deps) (*Host, error) {
return nil
})
env.Timer = timerHub
- manager.SetOnUnload(timerHub.CancelForPlugin)
+ // On unload (disable/reload/uninstall/disk-removal) cancel the plugin's
+ // pending timers AND close its open SSE streams, so neither lingers after
+ // the plugin is gone.
+ manager.SetOnUnload(func(slug string) {
+ timerHub.CancelForPlugin(slug)
+ sseHub.CloseForPlugin(slug)
+ })
// Fire a once-a-second tick to plugins that subscribe (onTick), and which
// also drives nothing else — host-scheduled timers run independently. The
diff --git a/services/plugins/server.go b/services/plugins/server.go
index 14fff2e183..76a5638b41 100644
--- a/services/plugins/server.go
+++ b/services/plugins/server.go
@@ -338,7 +338,11 @@ func (s *Server) serveSSE(w http.ResponseWriter, r *http.Request, p *Loaded, cha
select {
case <-ctx.Done():
return
- case frame := <-stream:
+ case frame, ok := <-stream:
+ if !ok {
+ // The hub closed the stream (plugin disabled/reloaded/uninstalled).
+ return
+ }
if _, err := w.Write(frame); err != nil {
return
}
diff --git a/services/plugins/sse.go b/services/plugins/sse.go
index e0e551c7f9..ab55c4842e 100644
--- a/services/plugins/sse.go
+++ b/services/plugins/sse.go
@@ -82,19 +82,50 @@ func (h *SSEHub) Subscribe(pluginName, channel string) (<-chan []byte, func(), u
once.Do(func() {
h.mu.Lock()
defer h.mu.Unlock()
- if subs, ok := h.subscribers[key]; ok {
- delete(subs, client)
- if len(subs) == 0 {
- delete(h.subscribers, key)
- }
+ subs, ok := h.subscribers[key]
+ if !ok {
+ return // already torn down (e.g. CloseForPlugin closed the stream)
+ }
+ if _, present := subs[client]; !present {
+ return
+ }
+ delete(subs, client)
+ if len(subs) == 0 {
+ delete(h.subscribers, key)
+ }
+ // Guard against a count that CloseForPlugin already cleared, so a
+ // late unsubscribe can't drive it negative.
+ if h.connectionCounts[pluginName] > 0 {
+ h.connectionCounts[pluginName]--
}
- h.connectionCounts[pluginName]--
})
}
return client.send, unsubscribe, connID, true
}
+// CloseForPlugin terminates every open SSE connection for a plugin: it closes
+// each client's send channel (signalling the serving goroutine to exit) and
+// drops the plugin's subscriptions and connection count. Called when a plugin
+// is disabled, reloaded, or uninstalled so its streams don't linger as zombies
+// — emitting keep-alives forever and holding slots against the per-plugin cap
+// across a re-enable. Safe to call when the plugin has no connections.
+func (h *SSEHub) CloseForPlugin(pluginName string) {
+ h.mu.Lock()
+ defer h.mu.Unlock()
+ prefix := pluginName + "\x00"
+ for key, subs := range h.subscribers {
+ if !strings.HasPrefix(key, prefix) {
+ continue
+ }
+ for client := range subs {
+ close(client.send)
+ }
+ delete(h.subscribers, key)
+ }
+ delete(h.connectionCounts, pluginName)
+}
+
// Publish frames event+data once and delivers it to every client subscribed
// to (pluginName, channel). Delivery is non-blocking per client: if a
// client's buffer is full (a slow consumer), the message is dropped for that
diff --git a/services/plugins/sse_test.go b/services/plugins/sse_test.go
index 08e603b31f..ec685b9415 100644
--- a/services/plugins/sse_test.go
+++ b/services/plugins/sse_test.go
@@ -3,8 +3,45 @@ package plugins
import (
"strings"
"testing"
+ "time"
)
+// TestSSEHub_CloseForPlugin is the C5 regression: disabling/uninstalling a
+// plugin must tear down its open SSE streams (closing the serving goroutines'
+// channels) and free its connection slots, rather than leaving zombies that
+// emit keep-alives forever and hold slots against the per-plugin cap.
+func TestSSEHub_CloseForPlugin(t *testing.T) {
+ h := NewSSEHub()
+ stream, unsub, _, ok := h.Subscribe("p", "overlay")
+ if !ok {
+ t.Fatal("subscribe failed")
+ }
+
+ h.CloseForPlugin("p")
+
+ // The serving goroutine learns to exit via a closed stream channel.
+ select {
+ case _, open := <-stream:
+ if open {
+ t.Fatal("expected the stream channel to be closed")
+ }
+ case <-time.After(time.Second):
+ t.Fatal("CloseForPlugin did not close the stream channel")
+ }
+
+ // The serving goroutine's deferred unsubscribe still runs after the close;
+ // it must not drive the connection count negative.
+ unsub()
+ if c := h.connectionCounts["p"]; c != 0 {
+ t.Fatalf("connection count should be 0 after close + unsubscribe, got %d", c)
+ }
+
+ // A torn-down plugin's slots are freed for a clean re-enable.
+ if _, _, _, ok := h.Subscribe("p", "overlay"); !ok {
+ t.Fatal("re-subscribe after CloseForPlugin should succeed")
+ }
+}
+
func TestSSEHubPublishFansOutToSubscribers(t *testing.T) {
h := NewSSEHub()