Skip to content

Commit 0dbd304

Browse files
AgentWrapperclaude
andcommitted
fix(backend): drain CDC/lifecycle goroutines without deadlocking on non-signal exit
lcStack.Stop()/cdcPipe.Stop() block on done channels that close only when ctx is cancelled, but the deferred stop() that cancels ctx ran last (LIFO) — so any non-signal exit (e.g. a listener bind error) hung the daemon forever. Cancel ctx first, then drain, explicitly after srv.Run instead of via defer. Also refresh the startup comments that still described the removed outbox/JSONL/janitor flow. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 0a69b84 commit 0dbd304

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

‎backend/main.go‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,38 +47,34 @@ func run() error {
4747
return err
4848
}
4949

50-
// Open the durable store and bring up the CDC substrate (outbox publisher,
51-
// JSONL consumer + broadcaster, outbox janitor). The LCM/Session Manager and
52-
// the HTTP API routes that drive and read this store are owned by the daemon
53-
// lane and are wired there once their collaborators (Notifier, AgentMessenger,
54-
// and the runtime/agent/workspace plugins) have production implementations;
55-
// here we stand up the persistence + change-delivery foundation they build on.
50+
// Open the durable store and bring up the CDC substrate: the DB triggers
51+
// capture changes into change_log, the poller tails it, and the broadcaster
52+
// fans events out to the SSE transport. The LCM/Session Manager and the HTTP
53+
// API routes that drive and read this store are owned by the daemon lane and
54+
// are wired there once their collaborators (Notifier, AgentMessenger, and the
55+
// runtime/agent/workspace plugins) have production implementations; here we
56+
// stand up the persistence + change-delivery foundation they build on.
5657
store, err := sqlite.Open(cfg.DataDir)
5758
if err != nil {
5859
return fmt.Errorf("open store: %w", err)
5960
}
6061
defer store.Close()
6162

6263
// signal.NotifyContext cancels ctx on SIGINT/SIGTERM, which drives the
63-
// graceful shutdown inside Server.Run.
64+
// graceful shutdown inside Server.Run and stops the background goroutines.
6465
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
6566
defer stop()
6667

6768
cdcPipe, err := startCDC(ctx, store, log)
6869
if err != nil {
6970
return err
7071
}
71-
defer func() {
72-
if err := cdcPipe.Stop(); err != nil {
73-
log.Error("cdc pipeline shutdown", "err", err)
74-
}
75-
}()
7672

7773
// Bring up the Lifecycle Manager (sole store writer) and the reaper (OBSERVE
78-
// timer). This makes the write path live end-to-end: LCM.Upsert -> store ->
79-
// outbox -> CDC JSONL -> broadcaster. The collaborators it needs that don't
80-
// yet have production implementations (Notifier, AgentMessenger, runtime
81-
// registry) are stubbed in lifecycle_wiring.go with TODO markers.
74+
// timer). This makes the write path live end-to-end: LCM write -> store -> DB
75+
// trigger -> change_log -> poller -> broadcaster. The collaborators it needs
76+
// that don't yet have production implementations (Notifier, AgentMessenger,
77+
// runtime registry) are stubbed in lifecycle_wiring.go with TODO markers.
8278
//
8379
// NOT wired here yet — both await collaborators the daemon lane owns:
8480
// - Session Manager: session.New needs Runtime/Agent/Workspace plugins to
@@ -90,11 +86,21 @@ func run() error {
9086
// the SM work since the routes call into it.
9187
lcStack, err := startLifecycle(ctx, store, log)
9288
if err != nil {
93-
return fmt.Errorf("start lifecycle: %w", err)
89+
return err
9490
}
95-
defer lcStack.Stop()
9691

97-
return srv.Run(ctx)
92+
runErr := srv.Run(ctx)
93+
94+
// Shut the background goroutines down in order: cancel the context FIRST so
95+
// their loops exit, then wait for them to drain. Doing this explicitly (not
96+
// via defer) avoids the LIFO trap where a Stop() that blocks on ctx-cancel
97+
// runs before the cancel — which would hang any non-signal exit path.
98+
stop()
99+
lcStack.Stop()
100+
if err := cdcPipe.Stop(); err != nil {
101+
log.Error("cdc pipeline shutdown", "err", err)
102+
}
103+
return runErr
98104
}
99105

100106
// newLogger returns the daemon's slog logger. It writes to stderr so the

0 commit comments

Comments
 (0)