Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(plugins): stop a self-subscribed emit from deadlocking the dispatcher
Gabe Kangasee3f8b137b53
diff --git a/services/plugins/dispatcher.go b/services/plugins/dispatcher.goindex 719d96acd4..7d035e9055 100644--- a/services/plugins/dispatcher.go+++ b/services/plugins/dispatcher.go@@ -98,19 +98,20 @@ func (d *Dispatcher) Dispatch(ctx context.Context, eventType string, payload anyd.Notify(ctx, eventType, final)}-// Filter runs the ordered filter chain for an event. Plugin errors are-// fail-open — the event passes through that plugin unchanged. Returns the-// final payload, whether the event survived, and a drop reason if not.-func (d *Dispatcher) Filter(ctx context.Context, eventType string, payload any) (any, bool, string) {- type subbed struct {- plugin *Loaded- priority int- }- var chain []subbed+// filterEntry is a plugin in an event's filter chain, paired with its priority.+type filterEntry struct {+ plugin *Loaded+ priority int+}++// filterChain returns the plugins subscribed to eventType's filter, ordered by+// priority (lower first), then slug for a stable tie-break.+func (d *Dispatcher) filterChain(eventType string) []filterEntry {+ var chain []filterEntryfor _, p := range d.snapshot() {for _, s := range p.Manifest.Subscriptions.Filter {if s.Event == eventType {- chain = append(chain, subbed{p, s.Priority})+ chain = append(chain, filterEntry{p, s.Priority})}}}@@ -120,9 +121,23 @@ func (d *Dispatcher) Filter(ctx context.Context, eventType string, payload any)}return chain[i].plugin.Manifest.Slug < chain[j].plugin.Manifest.Slug})+ return chain+}+// Filter runs the ordered filter chain for an event. Plugin errors are+// fail-open — the event passes through that plugin unchanged. Returns the+// final payload, whether the event survived, and a drop reason if not.+func (d *Dispatcher) Filter(ctx context.Context, eventType string, payload any) (any, bool, string) {+ chain := d.filterChain(eventType)+ emitter := emittingPluginSlug(ctx)current := payloadfor _, c := range chain {+ if c.plugin.Manifest.Slug == emitter {+ // Re-entrant self-delivery from the plugin's own synchronous emit;+ // see Notify. (The filter lock is already cancellable, so this is a+ // correctness/clarity guard rather than a deadlock fix.)+ continue+ }if c.plugin.IsDisabled() {// Auto-disabled by the strike system; skip silently.continue@@ -212,11 +227,19 @@ func (d *Dispatcher) Notify(ctx context.Context, eventType string, payload any)return}+ emitter := emittingPluginSlug(ctx)var wg sync.WaitGroupfor _, p := range d.snapshot() {if !subscribed(p.Manifest.Subscriptions.Notify, eventType) {continue}+ if p.Manifest.Slug == emitter {+ // This dispatch is the plugin's own synchronous owncast.emit+ // re-entering while its wasm call still holds p.mu; delivering back+ // into it would block on that held mutex and deadlock. A plugin does+ // not receive its own in-call emission.+ continue+ }wg.Add(1)go func(p *Loaded) {defer wg.Done()@@ -254,9 +277,29 @@ var (errPluginNoSuchExport = errors.New("plugin does not export the requested function"))+// emittingPluginSlug returns the slug of the plugin whose synchronous+// owncast.emit re-entered the dispatcher on this stack, or "" when the dispatch+// was initiated by the host (tick, chat, moderation, …). The extism go-sdk+// injects the calling plugin into the context for the duration of the emit host+// call, so callerIdentity resolves it here.+func emittingPluginSlug(ctx context.Context) string {+ if id, ok := callerIdentity(ctx); ok && id != nil {+ return id.slug+ }+ return ""+}+func callOnEvent(ctx context.Context, p *Loaded, input []byte) error {// Extism plugins are not safe for concurrent calls; serialize per-plugin.- p.mu.Lock()+ // Bound the lock acquisition by the call deadline (mirrors callOnFilter):+ // the Notify/Filter self-skip stops the common direct self-emit from+ // deadlocking, and this backstops any indirect re-entrant cycle+ // (A emits -> B -> emits -> A) at NotifyTimeout instead of a permanent hang.+ callCtx, cancel := context.WithTimeout(ctx, NotifyTimeout)+ defer cancel()+ if err := lockWithContext(callCtx, &p.mu); err != nil {+ return fmt.Errorf("on_event timed out after %s waiting for plugin mutex", NotifyTimeout)+ }defer p.mu.Unlock()// Snapshot the instance under the lock: a concurrent Loaded.Close may have// torn it down. Reading p.plugin before the lock raced with that write and@@ -268,8 +311,6 @@ func callOnEvent(ctx context.Context, p *Loaded, input []byte) error {if !pl.FunctionExists("on_event") {return errPluginNoSuchExport}- callCtx, cancel := context.WithTimeout(ctx, NotifyTimeout)- defer cancel()_, _, err := pl.CallWithContext(callCtx, "on_event", input)if err != nil {if errors.Is(err, context.DeadlineExceeded) || callCtx.Err() == context.DeadlineExceeded {diff --git a/services/plugins/shared_engine_test.go b/services/plugins/shared_engine_test.goindex 460a682543..c39a9b13bb 100644--- a/services/plugins/shared_engine_test.go+++ b/services/plugins/shared_engine_test.go@@ -7,6 +7,7 @@ import ("strings""sync""testing"+ "time")// captureEnv returns a HostEnv whose OnChat records every send, plus the slice@@ -157,6 +158,43 @@ module.exports = definePlugin({wg.Wait()}+// TestEmitSelfSubscriptionDoesNotDeadlock is the C3 regression: a plugin that+// emits an event it is itself subscribed to (notify) must not deadlock. The+// synchronous owncast.emit re-enters the dispatcher while the plugin's on_event+// call still holds its mutex; delivering the event back into the same plugin+// would block on that mutex forever (and, from the tick, freeze onTick for+// every plugin). Notify skips self-delivery on the emit chain.+func TestEmitSelfSubscriptionDoesNotDeadlock(t *testing.T) {+ ctx := context.Background()+ compiledEngines.resetForTest(ctx)+ t.Cleanup(func() { compiledEngines.resetForTest(ctx) })++ env, _, _ := captureEnv()+ // Emits "ping" on every chat message AND subscribes (notify) to "ping".+ script := `+const { definePlugin, owncast } = require("@owncast/plugin-sdk");+module.exports = definePlugin({+ onChatMessage(msg) { owncast.events.emit("ping", {}); },+ on: { ping(payload) {} },+});`+ loaded := loadShared(t, ctx, env, RuntimeJavaScript, "echoer", script, []string{PermEmitEvent})+ defer loaded.Close(ctx)++ d := NewLiveDispatcher(func() []*Loaded { return []*Loaded{loaded} })+ env.Emit = d.Dispatch // route owncast.emit through the dispatcher, as in production++ done := make(chan struct{})+ go func() {+ d.Dispatch(ctx, EventChatMessageReceived, chatPayload("alice", "hi"))+ close(done)+ }()+ select {+ case <-done:+ case <-time.After(10 * time.Second):+ t.Fatal("Dispatch deadlocked on a re-entrant self-subscribed emit")+ }+}+// TestManifestParse covers the R4 reserved-config-key guard and confirms the// manifest no longer requires (or validates) a "type" field — the runtime is// inferred from the code artifact at load time.