refactor(api): extract Run-scoped background goroutine lifecycle into BackgroundComponents - #407
Merged
Merged
Conversation
… BackgroundComponents Introduce internal/api/background.go (BackgroundComponents) as the single ordered Start/Stop seam for Run-scoped long-lived goroutines, beginning with the reflector-stats SSE publisher (runReflectorStatsPublisher). Shutdown now stops the publisher and waits for it to exit before tearing down the reflector executor it reads, closing a latent use-during-teardown race. Construction-scoped cleanup goroutines (rate limiters, CSRF/auth managers) deliberately stay in Shutdown: they outlive Run and must stop even when Run was never called (the test suite constructs servers and calls Shutdown directly). ADR-0005 records the lifecycle-holder decision. ADR-0006 records that stem's only at-rest encryption (license activation) is device-fingerprint-keyed and fully decoupled from the JWT signing secret, so seed ADR-0015 DEK separation is N/A by construction. Verified: go build ./..., go vet ./..., golangci-lint (0 issues), full internal/api -race suite (ok 320s), non-api tests.
krisarmstrong
enabled auto-merge (squash)
June 8, 2026 04:11
Contributor
License Compliance ReportAll dependencies pass license compliance checksGo Dependencies
npm DependenciesSee full report in workflow artifacts Allowed Licenses: MIT, Apache-2.0, BSD-*, ISC, CC0-1.0, MPL-2.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Introduces
internal/api/background.go— aBackgroundComponentsholder that is the single orderedStart(ctx)/Stop()seam for Run-scoped long-lived goroutines, beginning with the reflector-stats SSE publisher. Mirrors seed'sinternal/api/background.goshape (harmonization, not a code copy).This is the composition-root + DEK item from the stem/niac remediation plan (Phase 4).
What changed
background.go:BackgroundComponentsowns the publisher goroutine, a derived cancellable context, and aWaitGroup.Stop()cancels and waits for exit (nil-safe + idempotent).sse_publishers.go:startReflectorStatsPublisher→ blockingrunReflectorStatsPublisher(ctx); the holder owns the goroutine.server.go:Runconstructs + starts the holder;Shutdownstops it first — before tearing down the reflector executor the publisher reads, closing a latent use-during-teardown gap (Shutdown previously never waited for the publisher).Shutdown— they outliveRunand must stop even whenRunwas never called (tests construct servers and callShutdowndirectly).Decisions (ADRs)
credential.keyis added.Verification
go build ./...,go vet ./...cleangolangci-lint run ./internal/api/— 0 issuesinternal/api-racesuite — ok (320s)background_internal_test.go: start/stop, stop-before-start, idempotent stop, ctx-cancel-stops (all-race)