Skip to content

Keep --env overrides across watch and server config reloads - #1625

Merged
Slach merged 7 commits into
masterfrom
fix-watch-env-override
Oct 21, 2026
Merged

Slach merged 7 commits into
masterfrom
fix-watch-env-override

Conversation

@Slach

@Slach Slach commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Problem

--env KEY=VALUE overrides were applied only inside GetConfigFromCli and immediately restored. watch (interval and --schedule) reloads the config with plain LoadConfig at the start of every iteration, so even the first backup ran without the overrides — e.g. watch --env REMOTE_STORAGE=ftp failed with remote_storage shall not be "none", and backups_to_keep_remote from --env was ignored. server --env ... was never applied to the server's own config at all (startup, /restart, SIGHUP, API handlers). Found while writing the TestFlows test for #1535.

Change

  • GetConfigFromCli applies --env to the process environment once and keeps it for the life of the process; later reloads see the overrides naturally. No temporary set/restore window on reload (which would race with other goroutines reading the environment).
  • Exception: commands re-run in-process by the server (--command-id != -1, e.g. /backup/actions) restore the environment after loading, so a per-request --env doesn't stick in the server.
  • server loads its config once via GetConfigFromCli before waiting for ClickHouse: an invalid config or --env value now exits instead of retrying forever; the 5s retry only waits for the ClickHouse connection.
  • Server-run commands (/backup/actions, resume after restart) inherit the server's --env; a command's own --env still wins.
  • disable_environment_override is respected; for duplicate keys the last value wins.

Tests

  • Unit: TestGetConfigFromCliKeepsEnvOverride (overrides survive later LoadConfig reloads, server-run restore, disable_environment_override); go test -race ./pkg/config/... ./pkg/backup/... ./pkg/server/... pass.
  • Integration (local): new TestWatchEnvOverride (runs watch --env S3_PATH=... in background, polls for ≥2 backups under the --env path, none under the config path) and TestServerAPI pass.
  • Reviewed with a multi-model review (rejudge); findings applied.

🤖 Generated with Claude Code

Slach and others added 3 commits October 18, 2026 22:50
--env is applied only inside GetConfigFromCli (override process env,
LoadConfig, restore env). The watch loop, scheduled watch ticks and
the API server reload config with plain LoadConfig, so every watch
iteration (even the first) silently dropped --env, e.g.
--env=REMOTE_STORAGE=ftp fell back to the config file value, and
`server --env` was never applied to the server config at all.

Extract LoadConfigFromCli (returns error instead of log.Fatal, restores
env even on load error, serialized by a mutex because overrides go
through the process-global environment while API handlers reload
concurrently) and use it in all reload sites. disable_environment_override
semantics are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Follow-up to review of the --env reload fix:
- /backup/actions and resume-after-restart re-enter cliApp with only
  -c and --command-id, so they ran with a different config than
  api.config once server --env was applied; prepend server --env so
  the command's own --env still wins.
- server startup retried config load errors forever, now a bad --env
  value would hang startup silently; invalid config is fatal like in
  every other command, only the ClickHouse connection is awaited.
- duplicated --env key recorded the first override as the "old" value,
  so restore left it in the long-lived server/watch process env.
- reloads logged override K=V at info on every API call / iteration and
  left the log level reset to info after a load error; log once, then
  debug, and restore the previous level on error.
- ReloadConfig error log now shows the resolved config path.

Tests: TestLoadConfigFromCli snapshots ambient env, covers duplicated
keys and log level; new TestServerKeepsEnvOverride pins ReloadConfig
and cliArgs; TestWatchEnvOverride polls a background watch with a
bounded deadline instead of a fixed timeout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Re-applying --env on each reload needs a temporary os.Setenv/restore
window, and kelseyhightower/envconfig has no custom lookup to avoid it,
so goroutines reading the environment concurrently could see or miss
the overrides. Keep --env in the process environment after
GetConfigFromCli instead: watch/server reloads go back to plain
LoadConfig and see it, and /backup/actions plus resume-after-restart
re-entries inherit server --env without extra args.

Re-entered commands (--command-id set) still restore the environment,
so --env of a single /backup/actions command can't leak into the
long-lived server process. server loads config once via
GetConfigFromCli, so a bad config or --env is fatal instead of retried
forever. This drops LoadConfigFromCli, its mutex, log-once and cliArgs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Slach Slach added this to the 2.8.2 milestone Oct 19, 2026
@coveralls

coveralls commented Oct 19, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 39483697392

Coverage at 70.32% (no base build to compare)

Details

  • Coverage remained the same as the base build.
  • Patch coverage: 12 of 12 lines across 2 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 29508
Covered Lines: 20750
Line Coverage: 70.32%
Coverage Strength: 34378.04 hits per line

💛 - Coveralls

@Slach
Slach merged commit e2afb88 into master Oct 21, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants