Skip to content

Fix orphan .sh test processes when clickhouse-test or fast_test.py is killed - #101636

Closed
maxknv wants to merge 5 commits into
masterfrom
fix/clickhouse-test-orphan-processes
Closed

maxknv wants to merge 5 commits into
masterfrom
fix/clickhouse-test-orphan-processes

Conversation

@maxknv

@maxknv maxknv commented Apr 2, 2026 •

Copy link
Copy Markdown
Member

Motivation

When running clickhouse-test on macOS without Docker, .sh test subprocesses could survive indefinitely after clickhouse-test or fast_test.py was killed unexpectedly (SIGKILL, OOM).

The root cause: each .sh test was spawned with start_new_session=True, placing it in a completely isolated session. If clickhouse-test was killed before its signal handlers ran, there was no kernel mechanism to reap those children — they were re-parented to launchd and kept running. In Docker this was invisible because the container PID namespace killed everything on exit. On macOS without Docker the orphans accumulated across runs.

Solution: session-based containment

clickhouse-test now calls os.setsid() instead of os.setpgid(0, 0) at startup, making itself a new session leader (session ID == its PID). Each .sh test is spawned with preexec_fn=os.setpgrp instead of start_new_session=True, giving each test its own process group (so os.killpg still works for per-test cleanup) while keeping it in clickhouse-test's session.

Session membership survives re-parenting. Even after clickhouse-test exits and its children are adopted by launchd, their session ID does not change — so ps -A -o pid,sess can find them on both Linux and macOS.

run_test() in clickhouse_proc.py kills the entire session in a finally block after process.wait() returns, covering the case where clickhouse-test itself was SIGKILL'd before its own cleanup ran.

Post-hook guard for fast_test.py being killed: run_test() writes the session ID to ci/tmp/clickhouse_test_session_id immediately after Popen(), before any blocking call. A new post-hook (ci/jobs/scripts/job_hooks/clickhouse_test_cleanup_hook.py) registered on darwin_fast_test_jobs reads that file and kills the session after fast_test.py exits, regardless of how it died. The file is deleted in the finally block on any normal exit, so the hook is a no-op in the common case.

Cleanup layers

Layer Trigger Kills
clickhouse-test cleanup_child_processes SIGTERM/SIGINT/SIGHUP to the runner .sh test process groups
run_test() finally block Any exit of clickhouse-test (incl. SIGKILL) Entire session via ps -o sess
Post-hook Any exit of fast_test.py (incl. SIGKILL) Same session, read from session ID file

Changelog category (leave one):

  • CI Fix or Improvement

… killed

When clickhouse-test was killed with SIGKILL (OOM, external kill), all .sh test
subprocesses it had spawned with start_new_session=True became orphans: they were
re-parented to init/launchd and continued running indefinitely. In Docker this
was invisible because the container boundary killed everything on exit. On macOS
without Docker the orphans accumulated across test runs.

Solution: use session-based containment.

clickhouse-test now calls os.setsid() instead of os.setpgid(0, 0), making itself
a new session leader (session ID == its PID). .sh tests are spawned with
preexec_fn=os.setpgrp instead of start_new_session=True, giving each test its own
process group for per-test os.killpg kills while keeping it in clickhouse-test's
session. Session membership survives re-parenting, so survivors can always be
found with ps -A -o pid,sess even after clickhouse-test exits.

run_test() in clickhouse_proc.py kills the entire session in a finally block after
process.wait() returns, covering the case where clickhouse-test itself was killed.

For the case where fast_test.py is killed before the finally block runs, run_test()
writes the session ID to ci/tmp/clickhouse_test_session_id immediately after
Popen() (before any blocking call). A new post-hook registered on
darwin_fast_test_jobs reads that file and kills the session after fast_test.py
exits, regardless of how it died.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
@maxknv
maxknv marked this pull request as draft April 2, 2026 15:54
@clickhouse-gh

clickhouse-gh Bot commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [815d652]

Summary: ❌

job_name test_name status info comment
LLVM Coverage failure
Generate LLVM Coverage Diff Report failure cidb

AI Review

Summary

This PR introduces session-based cleanup for clickhouse-test subprocesses on macOS, adds a Darwin post-hook fallback, and documents the process model. The direction is correct, but there are blocking correctness/safety issues in process-session handling and stale session-file trust that can terminate unrelated host processes. I also found a documentation/code mismatch in the new process-management doc.

Findings

❌ Blockers

  • [tests/clickhouse-test:5546-5548] os.setsid is attempted unconditionally and falls back to os.setpgid(0, 0) on error. In common foreground-shell execution (pid == pgid), setsid fails with PermissionError, and the fallback does not create a new session. This breaks the core invariant used by session cleanup and can leave orphan processes unmanaged.
    • Suggested fix: guard by process-group leadership (e.g. only call os.setsid when os.getpid() != os.getpgrp()), or reliably re-exec/launch in a non-group-leader context.
  • [ci/jobs/scripts/job_hooks/clickhouse_test_cleanup_hook.py:29-60] The hook trusts ci/tmp/clickhouse_test_session_id without run-identity validation. A stale file + PID/session ID reuse can terminate unrelated processes on the host.
    • Suggested fix: persist and validate tuple metadata (e.g. session leader PID + start time + job/run id) before signaling; ignore stale/non-matching records.

⚠️ Majors

  • [tests/clickhouse-test-process-management.md:88] The doc says cleanup uses ps -A -o pid,sess, but implementation intentionally avoids sess on macOS and uses os.getsid per PID.
    • Suggested fix: update docs to match actual implementation (ps -A -o pid= + os.getsid).
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ⚠️ Stale session-file trust can terminate unrelated host sessions on shared runners.
Compilation time ✅
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Fix setsid startup logic in tests/clickhouse-test so session creation invariant is guaranteed.
    • Add stale-file/session-identity validation before post-hook session termination.
    • Correct process-management documentation to match actual cleanup implementation.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Apr 2, 2026
maxknv and others added 2 commits April 2, 2026 22:49
1. clickhouse-test: guard os.setsid() with a session leader check.
   When run_test() uses start_new_session=True, the child is already a
   session leader and os.setsid() raises PermissionError.  The session
   ID already equals the PID in that case, so skipping the call is safe.

2. _kill_session / cleanup hook: replace ps -o sess with os.getsid() loop.
   On macOS, ps -o sess prints the controlling terminal device number,
   not the session ID, so it returns 0 for all detached processes.
   Iterating all PIDs from ps -A -o pid= and calling os.getsid() directly
   works correctly on both Linux and macOS.

Verified with a unit test that confirms:
- runner session ID == its PID after setsid() / start_new_session
- .sh test (preexec_fn=os.setpgrp) is in the same session, different pgid
- os.getsid() scan finds both processes
- 0 processes remain after _kill_session

Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
Comment thread tests/clickhouse-test Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

alexey-milovidov and others added 2 commits April 6, 2026 17:50
The previous guard `os.getpid() != os.getsid(0)` only checked for
session leadership, but `setsid` also fails when the caller is a
process-group leader (pid == pgid), which is the normal case in a
foreground shell job.  Replace the guard with try/except and fall
back to `setpgid(0,0)` when `setsid` fails.

#101636 (comment)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@maxknv
maxknv marked this pull request as ready for review April 7, 2026 10:25
@maxknv
maxknv marked this pull request as draft April 8, 2026 09:16
@maxknv

maxknv commented Apr 9, 2026

Copy link
Copy Markdown
Member Author

addressed by @leshikus

@maxknv maxknv closed this Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants