fix(daemon): guard binding dispatch loop against nil dispatcher/creator
BindingDispatcher and BindingTaskCreator are documented "optional: nil disables the dispatch loop", same as Bindings, but dispatchBindings only checked Bindings before dereferencing the other two. Any composition that wires Bindings without also wiring both handles panicked on the first dispatch cycle.
This commit is contained in:
1 parent
164886308b
commit
eaa58dfe73
2 files changed
+29
-3
No files matched your search
@@ -529,10 +529,12 @@ const bindingDispatchBatchLimit = 100
|
||||
// pair, and dispatchOneBinding treats that as a normal "another cycle
|
||||
// raced us" outcome rather than an error.
|
||||
//
|
||||
// Nil Bindings disables the loop (legacy behaviour: daemons built
|
||||
// before t2db.4 Phase E had no playbook bindings).
|
||||
// Nil Bindings, BindingDispatcher or BindingTaskCreator disables the
|
||||
// loop (legacy behaviour: daemons built before t2db.4 Phase E had no
|
||||
// playbook bindings, and each field's own doc comment promises this
|
||||
// degrade-not-panic contract independently).
|
||||
func (d *Daemon) dispatchBindings(ctx context.Context) {
|
||||
if d.Bindings == nil {
|
||||
if d.Bindings == nil || d.BindingDispatcher == nil || d.BindingTaskCreator == nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -216,6 +216,30 @@ func TestDispatchBindingsNilBindingsIsNoOp(t *testing.T) {
|
||||
d.dispatchBindings(t.Context())
|
||||
}
|
||||
|
||||
// TestDispatchBindingsNilDispatcherIsNoOp exercises the composition
|
||||
// gap the daemon field comments claim is safe: Bindings is set (so an
|
||||
// armed binding + a matching capture exist) but BindingDispatcher is
|
||||
// nil. Both fields are documented "optional: nil disables the
|
||||
// dispatch loop", so this must degrade silently rather than panic on
|
||||
// the nil-interface call to ListUndispatchedCaptures.
|
||||
func TestDispatchBindingsNilDispatcherIsNoOp(t *testing.T) {
|
||||
s := openDispatchTestStore(t)
|
||||
mappingID := seedMapping(t, s, "sentry", mapping.Field{Name: "title", Path: "title", Type: mapping.TypeString})
|
||||
seedArmedBinding(t, s, "sentry", mappingID)
|
||||
seedCapture(t, s, "sentry", true, `{"title":"hello"}`)
|
||||
|
||||
d := &Daemon{
|
||||
Cfg: config.NewHolder(config.Config{Repos: []config.Repo{{Owner: "acme", Name: "widget"}}}),
|
||||
Store: s,
|
||||
Mappings: s,
|
||||
Bindings: s,
|
||||
// BindingDispatcher and BindingTaskCreator intentionally nil.
|
||||
Log: slog.New(slog.NewTextHandler(os.Stderr, nil)),
|
||||
}
|
||||
// Should not panic.
|
||||
d.dispatchBindings(t.Context())
|
||||
}
|
||||
|
||||
// ── helpers ──────────────────────────────────────────────────────
|
||||
|
||||
// openDispatchTestStore creates a fresh in-memory SQLite store for
|
||||
|
||||
Reference in new issue
Block a user