Make local server stop <name> idempotent (#255)#260
Merged
Conversation
Stopping a server that exists on disk but is already stopped now exits 0
("Server 'x' is already stopped") instead of erroring with
ServerNotRunning. This matches docker/systemctl semantics and the
existing idempotent behavior of `stop-all`, so scripts no longer need to
guard against an already-stopped server.
An unknown server name (no data dir in the project) still returns
ServerNotFound (exit 1), so typos are caught. `--global` stop and the
Postgres stop command are unchanged.
The stop decision is extracted into a pure `classify_stop` helper and
unit-tested for all three outcomes; `ServerStopOutput` gains an
`already_stopped` flag surfaced in both human and `--json` output.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Closes #255.
Problem
chctl local server stop <name>exits non-zero (ServerNotRunning) when the named server isn't running, forcing scripts to guard against the already-stopped case.stop-allis already idempotent (exits 0 with "No running servers"), so the two were inconsistent.Approach
Rather than add the
--none-okflag from the issue,stopis now idempotent by default — the declarative paradigm used bydocker stop/systemctl stop:stop <running>stop <stopped-but-exists>stop <unknown-name>The unknown-name case still errors (
ServerNotFound) so typos surface — we distinguish it from already-stopped via the on-disk data dir (project-scoped, same checkremoveuses).This is a deliberate behavior change, agreed appropriate pre-1.0 and consistent with
stop-all.Changes
classify_stop(running, exists_on_disk)pure helper encodes the three outcomes; the handler stays thin. Unit-tested for all three cases.ServerStopOutputgainsalready_stopped: bool, surfaced in human output ("Server 'x' is already stopped") and--json.--globalstop and Postgresstopare unchanged (out of scope; global is cross-project maintenance where the project-scoped data-dir check doesn't apply).stopagent-help text document the idempotent behavior.Testing
cargo build,cargo test -p clickhousectl(392 tests),cargo clippy --all-targetsall clean.classify_stop_*(3 outcomes),server_stop_already_stopped(display + JSON).already_stopped: true.🤖 Generated with Claude Code
Note
Low Risk
Localized CLI behavior change for project-scoped ClickHouse stop only; running stops and unknown-name errors are unchanged aside from the already-stopped success path.
Overview
Project-scoped
clickhousectl local server stop <name>now treats an existing but stopped server as success (exit 0) instead of failing with a not-running error, aligning withstop-alland typicaldocker stop/systemctl stopbehavior.The handler uses
classify_stop(running, data_dir_exists)to choose kill, idempotent noop, orServerNotFoundfor unknown names.ServerStopOutputaddsalready_stoppedso CLI text says the server is already stopped and--jsoncan tell a real stop from a noop. README andserver stopagent help document this.--globalstop and Postgresstopare unchanged.Reviewed by Cursor Bugbot for commit 842ddb8. Bugbot is set up for automated code reviews on this repo. Configure here.