Repository navigation
serve: wait for the agent calls' handlers before the store closes - #258
avi-papineni wants to merge 2 commits into
Conversation
TestServeShutdownMarksStreamsOffline runs the control plane as `innerwall serve` builds it, connects four agents, stops it with their sync streams open, and requires that by the time serve returns every workload reads offline with a last-seen instant from the shutdown and the log carries no failed offline mark. The store is given two connections for four streams, so the marks queue for a connection as the streams end. serve takes the log's writer as a parameter so the test can read it; runServe passes standard error as before. This commit adds the test alone and is expected to fail: serve returns, and closes the store, before the stream handlers have written their marks (#113). The next commit fixes the order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Claude <noreply@anthropic.com>
On a signal, serve ran GracefulStop on its own goroutine and waited up to ten seconds for it. Sync streams never end on their own, so that wait always ran out and serve called Stop, which ends every call but returns without waiting for their handlers. serve then returned, and its deferred store close ran while the sync handlers were writing their workloads' offline marks on their way out; a mark that reached for a connection after the close failed with "closed pool". The mark itself already uses a detached context with its own five-second bound, so the order was the whole cause. GracefulStop, still running after Stop, returns only once every handler has returned, its deferred exit path included. serve now waits on it again after Stop, for at most five seconds, before returning; a handler still running then is logged once at warning level and its writes fail as before. Stop runs on its own goroutine, since it takes the lock that GracefulStop holds while waiting for handlers. A stop with agents connected is bounded at fifteen seconds instead of ten; a second signal still changes nothing. The serve reference states the order and the bound, and the changelog records the change. Fixes #113. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Claude <noreply@anthropic.com>
|
No ADR contradictions found (diff-anchored review). ScopeThe diff touches only The change reorders the control plane's shutdown sequence: after ADRs checked
Other checks
|
Today's shutdown sequence, and why the offline mark failed
cmd/innerwall/serve.go, on the signal context ending (signal.NotifyContextinmain.go; a second signal is swallowed by it):stopCtxis taken;grpcServer.GracefulStop()runs on its own goroutine andhttpServer.Shutdown(stopCtx)drains the operator listener (Closeif it runs out).GracefulStopalways runs to the deadline andgrpcServer.Stop()is called.Stopcloses every transport and cancels every call, but returns once the connections are gone, without waiting for the handlers (grpc-go waits for handlers only inGracefulStopor withWaitForHandlers).servereturns, and itsdefer st.Close()closes the pool.Meanwhile each sync handler, woken by its cancelled stream, runs its deferred exit path in
internal/gateway/sync.go: it leaves the session registry and writesSYNC_STATE_OFFLINEthroughSetSyncState. That write already uses a detached context (context.Background()withofflineWriteTimeout, five seconds), so a cancelled context is not the problem; it races the pool close in step 4, and a write that reaches for a connection after the close fails withclosed pool("marking workload offline"). The workload then reads as it did while connected until its agent next connects.The new sequence and its bound
Steps 1–2 are unchanged. After step 2,
Stopruns on its own goroutine andservewaits again on theGracefulStopgoroutine. Still running, that goroutine returns only after every handler has returned, deferred exit path included (grpc-go's own handler count, taken before each handler starts). It waits at mosthandlerExitGrace, five seconds, the same as the offline write's own bound. Only then doesservereturn and close the store. A handler still running at the end of that wait is logged once at warning level (agent calls still running at the end of shutdown; closing the store, so what they still write fails), and its writes fail as they do today.Stopis on its own goroutine because, once every connection is gone,GracefulStopwaits for handlers while holding the server lock thatStoptakes, so a stuck handler cannot hold shutdown past the bound.Other writers checked: flow reports (
ReportFlows), enrollment and renewal are gRPC handlers and are covered by the same wait. Operator requests get the existing ten-secondShutdown. The retention job and the policy announcement listener run on the serve context and write nothing once it has ended: each returns silently onctx.Err().Test
TestServeShutdownMarksStreamsOffline(cmd/innerwall/serve_shutdown_integration_test.go) runsserveas the binary builds it, with its log captured. It connects four real agent sync daemons and waits until each workload reads synced with its snapshot instant recorded. Then it stopsservewith the streams open. Onceservehas returned, every workload must read offline with a last-seen instant between the stop and the return, and the log must carry neithermarking workload offlinenorclosed pool(it must carrycontrol plane serving, so an empty capture cannot pass). The store is givenpool_max_conns=2for four streams, so the marks queue for a connection as the streams end, the moment a pool closed under them refuses them. Every wait is on a condition with a deadline.servenow takes the log's writer as a parameter, andrunServepasses standard error as before.The first commit adds the test alone, so its CI run shows the test failing without the fix. The second commit is the fix.
docs/reference/innerwall-cli.md(serve) now states the order and the bound. There is also a CHANGELOG entry.Verified by the project's CI only; nothing was built or run locally.
Fixes #113.
Generated by Claude Code