Skip to content

fix(server): StartHTTPServer shutdown does not wait for background DB init #664

Closed

Description

@cristim

Found by the independent re-review of #646 at 574caf4.

StartHTTPServer (internal/server/http.go:162-186) starts the background initializer with app.startBackgroundInit(sigCtx) (http.go:113) but never waits for it. On SIGTERM it runs server.Shutdown, returns nil, and cmd/server/main.go:52 runs defer app.Close() while ensureDB may still be running.

Effects (not fatal; Close still runs):

  • app.Close() (internal/server/app.go:949) reads app.DB without a lock while ensureDB may be writing it. That is a data race, and a pool created just after Close read nil is never closed.
  • runMigrationsBounded takes a pool, not a context, so SIGTERM mid-migration is not interrupted; the process exit kills it mid-run.

Fix sketch: make startBackgroundInit return a channel closed when initializeUntilReady exits, and have StartHTTPServer wait on it after server.Shutdown, inside the same 30s shutdownCtx. A draft with a test (TestRunUntilReadyClosesDoneAfterInitializerExits) passes locally: ~/.claude/agent-comms/messages/draft-20261008-646-f4-shutdown-wait.patch.

Deferred out of #646 by the fleet lead; to be done as a follow-up PR after #646 merges.

Activity

  1. added a commit that references this issue on Oct 9, 2026
    16a475b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions