refactor: propagate context.Context through call chains #155

Merged
lerko merged 1 commits from refactor/context-propagation into main 2026-06-27 23:56:11 +00:00
Owner

Closes backlog item #19 — the last deferred security audit finding (context propagation, item 3.10).

Changes

Thread context.Context from callers instead of creating context.Background() at every call site:

  • Engine: stores lifecycle ctx (set in Start()) for triggerAlert goroutines. Init methods (InitHistory, InitLogs, InitAlertHealth) accept ctx. GetStateChanges/GetStateChangesSince/TestAlert accept ctx.
  • TUI Model: carries ctx field, captured into Cmd closures for all store operations (writes, preference saves, tab data loads, detail/history/SLA queries).
  • main.go: CLI commands (apply, export, migrate-secrets) create root ctx and thread through openStore, seed functions. runServe creates one root ctx used for store init, engine lifecycle, and TUI.
  • keyCache: refresh/IsAllowed accept ctx, threaded from SSH auth handler.

Two intentional context.Background() remain:

  1. Engine constructor default — overridden by Start(ctx)
  2. drainWrites — parent ctx already cancelled at shutdown time

Test plan

  • go build ./... — clean
  • go test ./... — all pass
  • golangci-lint run ./... — 0 issues
  • No stray context.Background() outside root entry points and the two documented exceptions
Closes backlog item #19 — the last deferred security audit finding (context propagation, item 3.10). ## Changes Thread `context.Context` from callers instead of creating `context.Background()` at every call site: - **Engine**: stores lifecycle ctx (set in `Start()`) for `triggerAlert` goroutines. Init methods (`InitHistory`, `InitLogs`, `InitAlertHealth`) accept ctx. `GetStateChanges`/`GetStateChangesSince`/`TestAlert` accept ctx. - **TUI Model**: carries ctx field, captured into Cmd closures for all store operations (writes, preference saves, tab data loads, detail/history/SLA queries). - **main.go**: CLI commands (`apply`, `export`, `migrate-secrets`) create root ctx and thread through `openStore`, seed functions. `runServe` creates one root ctx used for store init, engine lifecycle, and TUI. - **keyCache**: `refresh`/`IsAllowed` accept ctx, threaded from SSH auth handler. Two intentional `context.Background()` remain: 1. Engine constructor default — overridden by `Start(ctx)` 2. `drainWrites` — parent ctx already cancelled at shutdown time ## Test plan - [x] `go build ./...` — clean - [x] `go test ./...` — all pass - [x] `golangci-lint run ./...` — 0 issues - [x] No stray `context.Background()` outside root entry points and the two documented exceptions
lerko added 1 commit 2026-06-27 23:44:00 +00:00
refactor: propagate context.Context through call chains
CI / test (pull_request) Successful in 1m56s
CI / lint (pull_request) Successful in 1m17s
CI / vulncheck (pull_request) Successful in 56s
a5fd3aec90
Thread context.Context from callers instead of creating
context.Background() at every call site. Engine stores its
lifecycle ctx for use by triggerAlert goroutines. TUI Model
carries ctx for store operations in Cmd closures. CLI commands
create a root ctx and thread it through openStore, seed
functions, and init methods.

Only two intentional context.Background() remain in non-root
positions: engine constructor default (overridden by Start)
and drainWrites (parent already cancelled at shutdown).
lerko merged commit a5fd3aec90 into main 2026-06-27 23:56:11 +00:00
lerko deleted branch refactor/context-propagation 2026-06-27 23:56:11 +00:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: lerkolabs/uptop#155