Skip to content

CI: kill leftover clickhouse server processes before the first server start - #110122

Merged
alexey-milovidov merged 6 commits into
masterfrom
fix/ci-kill-leftover-server-processes
Jul 13, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
fix/ci-kill-leftover-server-processes

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 11, 2026 •

Copy link
Copy Markdown
Member

Related: #105249

CI runners are reused across jobs, and a job that is cancelled or times out can leave its clickhouse-server running or slowly shutting down. Such a leftover keeps the server ports, so the next job's server fails every listen with Address already in use and wait_ready times out with an opaque Connection refused, failing the whole job at the Start ClickHouse Server step.

Observed in the "Stateless tests (amd_debug, flaky check)" job of #105249: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105249&sha=0fc7d64da40e5f01b107434f039acaede261af12&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29 — the very first server start of the job got Address already in use on every configured port (9181, 9009, 8123, 8443, 9000, ...), so a full server instance from a previous job was still alive on the runner. The job then failed before running a single test.

This adds a cleanup to ClickHouseProc.start for replica 0 only, next to the analogous dmesg --clear cross-job hygiene ("to avoid false OOM detection from previous CI jobs on the same host"). All server processes are stopped before every replica-0 start (stop_server precedes the mid-job restart in bugfix validation), so any process matching clickhouse[- ]server at that point is stale by definition and is killed, with the ps snapshot logged to job.log and a workflow warning added for visibility. The cleanup never runs locally (is_local_run), where the machine may host a developer's own server that must not be touched. The performance comparison job uses its own CHServer class with two concurrent servers and is not affected.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Version info

  • Merged into: 26.7.1.875 (included in 26.7 and later)

… start

Runners are reused across jobs, and a job that is cancelled or times out
can leave its `clickhouse-server` running or slowly shutting down. Such a
leftover keeps the server ports, so the next job's server fails every
listen with `Address already in use` and `wait_ready` times out with an
opaque `Connection refused`, failing the whole job at the `Start
ClickHouse Server` step.

Observed in the "Stateless tests (amd_debug, flaky check)" job:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105249&sha=0fc7d64da40e5f01b107434f039acaede261af12&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29
on #105249 - the first
server start of the job got `Address already in use` on every port
(9181, 9009, 8123, 9000, ...), so a full server instance from a previous
job was still alive on the runner.

The cleanup runs in `ClickHouseProc.start` for replica 0 only, next to
the analogous `dmesg --clear` cross-job hygiene. All server processes
are stopped before every replica-0 start (`stop_server` precedes the
mid-job restart in bugfix validation), so anything matching at that
point is stale by definition. The cleanup never runs locally, where the
machine may host a developer's own server.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [131ed3c]

Summary: ✅


AI Review

Summary

This PR factors a pre-start kill_leftover_server_processes helper across several CI server launchers and updates the relevant job digests so helper changes reschedule those jobs. Verdict: request changes, because the cleanup still runs in the wrong PID namespace for the host-network Docker jobs that motivated the fix, and the only concrete incident cited for the change was later shown not to be a leaked-process case.

Findings

❌ Blockers

  • [ci/jobs/scripts/server_cleanup.py:27-34] The new cleanup promises to clear stale clickhouse-server processes from previous CI jobs, but for the Docker jobs that actually share host ports it still executes inside a fresh container PID namespace. ci/praktika/runner.py:458 uses plain docker run --init ... with no --pid=host, while Fast test, Bugfix validation, ClickBench, and Collect ClickHouse Profiles still opt into --network=host in ci/defs/job_configs.py, and the nested AST/BuzzHouse fuzzer path still does the same in ci/jobs/ast_fuzzer_job.py:123-133 / ci/jobs/scripts/fuzzer/run-fuzzer.sh:129-135. A leaked previous container can therefore keep 9000 / 8123 / 9181 bound on the host while the fresh job's in-container pgrep / pkill sees nothing, so the original Address already in use failure mode remains.
    Suggested fix: move the cleanup to the host side before docker run (or otherwise operate on the host/container namespace that owns the conflicting ports) instead of trying to kill from inside the new job container.

⚠️ Majors

  • [ci/jobs/scripts/server_cleanup.py:5-19] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/110122#discussion_r3565898060] The PR still lacks evidence that this new pkill -9 path fixes a real incident. The only report cited in the PR body (Stateless tests (amd_debug, flaky check) on PR Accessing tables as files, query construction and out-of-band modification in HTTP interface #105249) was confirmed in-thread to be a slow debug-build readiness timeout, not a stale process holding the ports, and there is still no same-namespace repro or alternate report for the helper's actual scope.
    Suggested fix: either point the PR at a real leaked-process report that this mechanism can reach, or add a focused repro that leaves a stale clickhouse-server in the same PID namespace and shows the next start succeeding.
Tests
  • ⚠️ Provide the smallest proof that this mechanism fixes its intended failure mode: either a CI report where the conflicting clickhouse-server is visible from the new job's namespace, or a focused repro that leaves such a stale process behind and shows the next start failing before this patch and succeeding after it.
Final Verdict

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 11, 2026
Comment thread ci/jobs/scripts/clickhouse_proc.py Outdated
if replica_num == 0:
# Clear dmesg to avoid false OOM detection from previous CI jobs on the same host
Shell.check("dmesg --clear", verbose=True)
self._kill_leftover_server_processes()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only applies the stale-runner cleanup to ClickHouseProc.start, but the same Start ClickHouse invariant still has sibling entrypoints that launch clickhouse-server on fixed shared ports without any cleanup: start_light (used by ci/jobs/clickbench.py, ci/jobs/vector_search_stress_job.py, and ci/jobs/vector_search_stress_tests.py) and the duplicate ClickHouseBinary.start helpers in ci/jobs/sqltest_job.py, ci/jobs/sqllogic_test.py, and ci/jobs/sqlstorm_test.py. A reused runner with a leaked server will still fail those jobs with the same Address already in use / wait_ready pattern, so the fix is only partial. Please move this into a shared pre-start helper and call it from every CI server-start path.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 30b0f2044cc: factored the cleanup into a shared ci/jobs/scripts/server_cleanup.py helper (kill_leftover_server_processes) and call it right before the first server start from every host entry point — ClickHouseProc.start_light (functional/fast/fuzzer/clickbench/vector-search jobs) in addition to ClickHouseProc.start (replica 0), plus the duplicate ClickHouseBinary.start helpers in sqltest_job.py, sqllogic_test.py, and sqlstorm_test.py. The Info().is_local_run guard is preserved everywhere.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The coverage is still incomplete. Quick functional tests starts clickhouse server directly on the host in ci/jobs/clickhouse_light.py:119-127 (ci/defs/job_configs.py:624-634), and Collect ClickHouse Profiles starts servers in ci/jobs/collect_clickhouse_profiles.py:463-474 and ci/jobs/collect_clickhouse_profiles.py:283-305 while its job uses clickhouse/binary-builder+--network=host (ci/defs/job_configs.py:23-27, ci/defs/job_configs.py:1514-1518). A leaked server from a cancelled or timed-out previous job can still make those jobs fail with the same Address already in use / readiness-timeout pattern, so the "every host entry point" part is not true yet.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current cleanup still misses the original failure mode for the Docker-based jobs. kill_leftover_server_processes only does pgrep/pkill inside the current process namespace (ci/jobs/scripts/server_cleanup.py:26-36), but Praktika starts these jobs with plain docker run and never adds --pid=host (ci/praktika/runner.py:458). That means a cancelled previous job can leave a host-network container still running, holding ports like 9000/8123, and the fresh container for functional_tests / fast_test / clickbench / collect_clickhouse_profiles (ci/defs/job_configs.py:101, ci/defs/job_configs.py:209, ci/defs/job_configs.py:1329, ci/defs/job_configs.py:1518) still cannot see or kill that stale clickhouse-server, so Address already in use remains reproducible.

There is also still an uncovered host-network start path in the AST/BuzzHouse fuzzer flow: the job launches a nested docker run --network=host in ci/jobs/ast_fuzzer_job.py:123-133, and that container starts clickhouse-server in ci/jobs/scripts/fuzzer/run-fuzzer.sh:129-135. The cleanup needs to happen at the host / Docker-container level before the new job starts, not only via pgrep inside the new job's namespace.

alexey-milovidov and others added 2 commits July 11, 2026 22:55
Address the AI review (⚠️ Request changes) on PR #110122: the
stale-process cleanup was wired only into `ClickHouseProc.start`, so the
other CI entry points that launch `clickhouse-server` on the host still
hit the same reused-runner port conflict (`Address already in use` ->
`wait_ready` timeout with an opaque `Connection refused`).

Factor `kill_leftover_server_processes` into a shared
`ci/jobs/scripts/server_cleanup.py` helper and call it right before the
first server start from every such entry point:
- `ClickHouseProc.start` (replica 0) and `ClickHouseProc.start_light`,
  covering the functional, fast, fuzzer, clickbench and vector-search
  jobs;
- the duplicate `ClickHouseBinary.start` helpers in `sqltest_job.py`,
  `sqllogic_test.py` and `sqlstorm_test.py`.

The helper keeps the `Info().is_local_run` guard so it never touches a
developer's own server on a shared machine.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The AI review found two more entry points that start `clickhouse-server`
on fixed shared runner ports without the pre-start cleanup: `Quick
functional tests` (`clickhouse_light.py`) and `Collect ClickHouse
Profiles` (`collect_clickhouse_profiles.py`, both its temporary dataset
server and the profiled server). A server leaked by a cancelled or
timed-out previous job can still make these jobs fail with `Address
already in use` and a readiness timeout before their first server start.

Call `kill_leftover_server_processes` right before the first server start
in each:
- `ClickHouseSetup.start_server` (gated on the first start so a later
  restart does not kill the job's own server),
- `main` in `collect_clickhouse_profiles.py` (a single call before the
  sequential PGO/BOLT dataset and profiled server starts),
- `ClickHouseService.__enter__` (the `CI Tests` job), found by auditing
  all remaining server-launch sites.

The performance-comparison job (`performance_tests.py`) is intentionally
left untouched: it runs two concurrent servers (Reference and Patched),
so a blanket pre-start kill would kill one while the other is running.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed the AI review "Request changes" in 23bed640f71.

Extended the pre-start kill_leftover_server_processes cleanup to the remaining host entry points that launch clickhouse-server on fixed shared ports:

  • ClickHouseSetup.start_server in ci/jobs/clickhouse_light.py (Quick functional tests) — gated on the first start (self.proc is None) so a later restart does not kill the job's own server.
  • main in ci/jobs/collect_clickhouse_profiles.py (Collect ClickHouse Profiles) — a single call before the stages, covering both the temporary dataset server (configure_datasets) and the profiled server (start_server) across the PGO and BOLT passes, which start sequentially.

While there, I audited every other server-launch site so this converges:

  • ClickHouseService.__enter__ in ci/jobs/scripts/clickhouse_service.py (CI Tests) — same single-server-per-job pattern, so I added the cleanup there too.
  • performance_tests.py (CHServer) is intentionally not touched: it runs two concurrent servers (Reference and Patched), so a blanket pre-start kill would kill one while the other is running.
  • Replicas 1/2 in ClickHouseProc.start are additional servers of the same job, so the cleanup stays scoped to replica_num == 0.

Verified with py_compile and ruff check (clean) on all changed files; I did not execute the kill path locally.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

On the AI "❌ Blockers" verdict — I dug into the execution model and the finding is correct: as currently written the cleanup cannot reach the cross-job leftover it targets. Evidence:

  • Praktika runs {job.command} inside the job container (ci/praktika/runner.py:458: docker run --init ... {docker} {job.command}), so kill_leftover_server_processes executes in-container.
  • --pid=host appears nowhere in ci/, and --init gives each container its own PID namespace. So the helper's pgrep/pkill only see the job's own fresh container. A leftover lives in a previous, different container (unique --name + --rm) whose PID namespace the fresh container cannot see — so at the fresh container's start pgrep matches nothing and the cleanup is a no-op for exactly the case it was meant to handle.
  • --network=host (real host-port conflict) applies to fast_test (job_configs.py:209), clickbench (:1329) and collect_clickhouse_profiles (BINARY_DOCKER_COMMAND, :23/:1518); functional_tests/sqltest/sqllogic/sqlstorm/CI Tests keep their own net namespace.

So the mechanism only works for a job that runs directly on the host (host PID namespace, where pkill sees processes inside orphaned containers too) — not for the containerized jobs above.

The two ways to actually fix it are both CI-architecture decisions, so I'm leaving them to you rather than changing them unilaterally:

  1. Host-level cleanup before docker run (in runner.py or a pre-job hook, mirroring the existing post_hooks=[docker_volume_clean_up_hook.py] pattern): docker rm -f the orphaned clickhouse/* container, or host-pkill. Correct target, but it edits core praktika (mirrored via CH Inc sync) and must be provably safe against any concurrently-running job on a shared runner.
  2. --pid=host on the affected job containers so the existing pkill can see the leaked process. One flag, but it weakens isolation and — combined with pkill -9 -f 'clickhouse[- ]server' — would kill a concurrent job's server if a runner ever runs more than one job at once, so it hinges on the runner concurrency model.

Which one is right depends on how these runners actually leak (an orphaned still-running container vs a bare host process) and whether a runner is single-job — I can't determine either from the code. Happy to implement whichever you pick.

My last commit (23bed640f71) is orthogonal to this: it just extends the same mechanism to the remaining entry points the previous review round asked for, and is harmless either way (a no-op in-container). If you choose the host-level approach it can be dropped.

…r-server-processes

# Conflicts:
#	ci/jobs/collect_clickhouse_profiles.py
Comment thread ci/jobs/scripts/clickhouse_proc.py Resolved
Address the AI review "Request changes" Major: `ClickBench` starts the
server through `ClickHouseProc.start_light`, whose cleanup now lives in
`clickhouse_proc.py` and the new `server_cleanup.py`, but the job's
`digest_config` listed neither. Praktika's `Job.Config.is_affected_by`
therefore returned `False` for both `ClickBench` jobs on this PR, so the
changed start path was never exercised, and any future change confined to
the shared helper would bypass the job again.

Add `server_cleanup.py` (and, for `ClickBench`, `clickhouse_proc.py`) to
the `digest_config` of every job that reaches `kill_leftover_server_processes`
at runtime and has a restrictive `include_paths`: `ClickBench`, `Fast test`
(shared `fast_test_digest_config`, also feeding the Darwin variant),
stateless functional tests, `Quick functional tests`, `SQL test`,
`SQL logic test`, `SQL storm test`, and `Collect ClickHouse Profiles`.
`CI Tests` already includes the whole `./ci` tree, so it needs no change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment on lines +8 to +12
Runners are reused across jobs, and a job that is cancelled or times out
can leave its clickhouse-server running or slowly shutting down. Such a
leftover keeps the server ports, so the next job's server fails every
listen with `Address already in use` and its `wait_ready` times out with
an opaque `Connection refused`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only concrete report cited for this failure mode (Stateless tests (amd_debug, flaky check) on PR #105249) is not actually a leaked-process case. Its clickhouse-server.log shows one fresh server process starting normally, then receiving SIGTERM exactly when the 60s wait_ready budget expires; the earlier Address already in use messages are the expected dual-stack [::]/0.0.0.0 warnings from tests/config/config.d/listen.xml plus listen_try=true, not another server already holding the ports.

So this helper is still unproven against the example in the PR description, and if that flaky-check timeout is the bug we care about, this change will not fix it. Can we either point to a real leaked-process report or add a focused repro that leaves a stale clickhouse-server in the same namespace and shows the next start succeeding?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified against the actual log for that run (.../stateless_tests_amd_debug_flaky_check/clickhouse-server.log) — this is correct, there was no leaked server:

  • One process only (pid 633, forked 21:17:08.427).
  • Every Address already in use is a [0.0.0.0]:<port> bind immediately followed by a successful [::]:<port> bind of the same port — the expected IPv4-after-IPv6 dual-stack duplicate under listen_try=true. E.g. Listen [0.0.0.0]:9000 failed at 21:18:11.621 -> Listening for native protocol (tcp): [::]:9000 at 21:18:12.930.
  • The server bound [::]:9000 ~64.5s after fork, just past the 60s wait_ready budget; SIGTERM (Received signal 15) had already arrived at 21:18:11.450 when the harness gave up.

So this is a slow debug-build readiness timeout, not a stale clickhouse-server holding ports — the pre-start pkill would not have changed the outcome, and (per the other thread) it cannot reach a cross-job leftover in a separate PID namespace anyway. This report does not justify the change; the real signal here is the readiness budget for slow debug/sanitizer builds. I will not add a same-namespace repro, since it would not reflect the actual failure.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Jul 13, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 13, 2026
Merged via the queue into master with commit d89bbe1 Jul 13, 2026
176 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/ci-kill-leftover-server-processes branch July 13, 2026 21:47
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 13, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Address the AI review (⚠️ Request changes) on PR ClickHouse#110122: the
stale-process cleanup was wired only into `ClickHouseProc.start`, so the
other CI entry points that launch `clickhouse-server` on the host still
hit the same reused-runner port conflict (`Address already in use` ->
`wait_ready` timeout with an opaque `Connection refused`).

Factor `kill_leftover_server_processes` into a shared
`ci/jobs/scripts/server_cleanup.py` helper and call it right before the
first server start from every such entry point:
- `ClickHouseProc.start` (replica 0) and `ClickHouseProc.start_light`,
  covering the functional, fast, fuzzer, clickbench and vector-search
  jobs;
- the duplicate `ClickHouseBinary.start` helpers in `sqltest_job.py`,
  `sqllogic_test.py` and `sqlstorm_test.py`.

The helper keeps the `Info().is_local_run` guard so it never touches a
developer's own server on a shared machine.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…over-server-processes

CI: kill leftover clickhouse server processes before the first server start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants