Validate config at startup to prevent ticker panic#30
Merged
Conversation
An empty or non-positive poll_interval_seconds (or updates_check_minutes) reached time.NewTicker unchecked and panicked, crashing the service in a restart loop from a single config typo. Other broken values (empty listen_addr, unknown log_level, inverted thresholds) were silently misapplied. Add Config.Validate() and call it at the end of Load() (after flag overrides, but skipped for -version) so the daemon fails fast with a descriptive error. As defense in depth, clamp non-positive tick intervals to 1s with a warning in Collector.Run so no future caller can trigger the panic. Closes #25 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016FQNxpLCapteFxih5iPHy4
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
internal/config/config.goperformed no validation after merging defaults,YAML, and flags, so broken values reached the runtime unchecked. Most
seriously,
poll_interval_seconds: 0(or negative) produced a0-durationtime.NewTickerinCollector.Run, which panics and crashes the wholeservice — a single typo in
/etc/pimonitor/config.yamltook the daemon downin a
Restart=on-failureloop. Other broken values were silently misapplied(empty
listen_addr→ port 80, unknownlog_level→ info, negativehistory_window_minutes→ broken sparklines, inverted thresholds).This change makes the daemon fail fast at startup with a clear message:
Config.Validate()checkingpoll_interval_seconds > 0,updates_check_minutes > 0,updates_stale_threshold_minutes >= 0,history_window_minutes > 0, non-emptylisten_addr,log_level∈{debug, info, warn, error}, and all thresholds
>= 0with eachwarn <=its
critcounterpart. Returns one descriptive error per first violation.cfg.Validate()at the end ofLoad()(after flag overrides). A-versionrequest short-circuits before validation sopimonitor -versionstill works against an otherwise invalid config.
run()already wraps theerror as
load config: ....in
Collector.Runso no future caller can trigger the ticker panic.Verified by building the binary and running it against
poll_interval_seconds: 0:No panic, no stack trace.
Related Issue
Closes #25
Checklist
go test ./...passes locally) —table-driven tests in
internal/config/config_test.gocover eachrejected field, valid edge cases (warn == crit, zero stale threshold),
that
Default()passesValidate(), thatLoadrejectspoll_interval_seconds: 0, and that-versionbypasses validation.go test ./... -racepasses.go vet ./...andgolangci-lint runare clean (0 issues)docs/API.md),configuration (
README.md,packaging/pimonitor.example.yaml), orinstallation/packaging (
packaging/install.sh, systemd units) — n/a,no config keys, defaults, or API shapes changed
/api/v1/...response shapes, or a new APIversion was introduced instead