Skip to content

darwin fast test: spawn watchdog child via posix_spawn to avoid SIGILL on macOS 26 - #105921

Closed
leshikus wants to merge 1 commit into
ClickHouse:masterfrom
leshikus:darwin-fast-test-watchdog-posix-spawn
Closed

leshikus wants to merge 1 commit into
ClickHouse:masterfrom
leshikus:darwin-fast-test-watchdog-posix-spawn

Conversation

@leshikus

Copy link
Copy Markdown
Collaborator

Every Fast test (arm_darwin) run on macOS 26.3.1 emits an .ips crash report with an identical signature, regardless of whether the test job itself succeeds. The root cause is the new libsystem_c hardening on macOS 26 (Tahoe), which SIGILLs any forked child that performs non-async-signal-safe work between fork() and exec*().

Diagnosis

98 of 99 sampled .ips files from the 100 most recent runs share the exact same signature (bug_type 309, the missing 1 was a download error):

  • Exception: EXC_CRASH / SIGILL ("Illegal instruction: 4")
  • ASI message: libsystem_c.dylib: crashed on child side of fork pre-exec
  • Crashing process: clickhouse (forked child of clickhouse-server)
  • OS: macOS 26.3.1 (25D2128)
  • The "triggered" thread shown in the IPS is the parent's main thread sitting in SignalListener::waitForTerminationRequest — misleading. The actual SIGILL is delivered to the child while it is still in the post-fork pre-exec window.

The offending fork is BaseDaemon::setupWatchdog (src/Daemon/BaseDaemon.cpp:573). The watchdog auto-enables in CI mode (no TTY on stdin/stdout/stderr, see programs/server/Server.cpp:508), fork()s, and the child path immediately calls non-async-signal-safe code: Jemalloc::setup, OwnAsyncSplitChannel::open, updateCurrentThreadIdAfterFork, logger().information(...), then Poco::Pipe::readBytes. Because the child never calls exec*() (it instead returns from setupWatchdog and continues running the parent's already-initialized code), the entire child lifetime is "pre-exec" from libsystem_c's point of view, and the first non-AS-safe call trips the hardening check.

Other forks in the server are not affected:

  • ShellCommand::executeImpl uses vfork() and a strictly AS-safe child block (dup2, sigprocmask, execv, _exit).
  • Poco::Process::launch is not used by clickhouse-server (no references in src/).
  • mainEntryClickHouseChdig, programs/docker-init, and friends are unrelated to the server runtime.

Change

On Darwin only, replace the fork()-and-continue pattern in BaseDaemon::setupWatchdog with posix_spawn. The spawned child is a fresh clickhouse-server process that re-runs main and re-initializes everything cleanly — libsystem_c has no exposed post-fork pre-exec window from userland's view, so the hardening cannot fire. A sentinel env var CLICKHOUSE_WATCHDOG_CHILD=1 is set before the spawn so the re-exec'd child knows not to enable its own watchdog (otherwise watchdogs would nest infinitely).

Linux is unchanged — the existing fork-based watchdog path is kept verbatim, gated behind #if !defined(OS_DARWIN). The parent-side waitpid loop, signal forwarding, and systemd notification are untouched.

Why posix_spawn rather than explicit fork() + execv():

  • AS-safety by construction. There is no child block to maintain; future contributors cannot accidentally reintroduce the SIGILL by adding a non-AS-safe call.
  • Failures return an errno-style code from posix_spawn synchronously, observable in the parent. With explicit fork+exec, AS-safety mistakes manifest as silent SIGILL only visible in the resulting .ips.
  • Apple-recommended pattern for macOS spawns.

The trade-off versus the existing in-memory continuation is that respawning the child requires re-parsing argv and re-reading the config file (~1-2s extra per respawn). The watchdog respawns very rarely in steady state, so this cost is acceptable. The behavioral cost is scoped to Darwin.

Reference query used to surface the universal .ips:

SELECT check_name, check_status, task_url, report_url, check_start_time
FROM default.checks
WHERE check_name = 'Fast test (arm_darwin)' AND test_name = ''
ORDER BY check_start_time DESC
LIMIT 100

on play.clickhouse.com.

Changelog category (leave one):

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

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

macOS 26 (Tahoe) libsystem_c aborts with SIGILL any forked child that performs
non-async-signal-safe work (logger, malloc, mutex) before exec*(). The watchdog
forks but never exec()s, so every clickhouse-server start on Darwin emits a
"crashed on child side of fork pre-exec" .ips report.

On Darwin, spawn a fresh server via posix_spawn and use CLICKHOUSE_WATCHDOG_CHILD
as a sentinel so the re-exec'd process skips its own watchdog setup. Linux
keeps the existing fork-based behavior.
@clickhouse-gh

clickhouse-gh Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [58df5d5]

Summary: ❌

job_name test_name status info comment
Build (amd_darwin) FAIL
Build ClickHouse FAIL cidb
Build (arm_darwin) FAIL
Build ClickHouse FAIL cidb
Fast test (arm_darwin) DROPPED

AI Review

Summary

This PR switches the Darwin watchdog path from fork-and-continue to posix_spawn with CLICKHOUSE_WATCHDOG_CHILD to avoid macOS 26 libsystem_c post-fork hardening exceptions. The approach is directionally correct, but the current spawn target introduces a launch-path dependency that can fail for valid server invocations.

Findings

❌ Blockers

  • [src/Daemon/BaseDaemon.cpp:604] posix_spawn uses orig_argv[0] as the executable path. That requires argv[0] to be directly executable from the current working directory. If the server was started via PATH lookup (for example argv[0] = "clickhouse") or another relative launcher path, this can return ENOENT and break watchdog setup on Darwin even though the running process is valid.
    Suggested fix: use a stable absolute binary path (for example getExecutablePath) or use posix_spawnp when PATH lookup is intended.
Final Verdict

Status: ⚠️ Request changes
Minimum required actions:

  1. Make Darwin watchdog spawn independent of argv[0] working-directory semantics by using a stable executable path resolution strategy.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 27, 2026
@leshikus leshikus added the can be tested Allows running workflows for external contributors label May 27, 2026
@leshikus
leshikus marked this pull request as ready for review May 27, 2026 10:55
Comment thread src/Daemon/BaseDaemon.cpp
posix_spawnattr_setsigmask(&attr, &empty_mask);
posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK);

int rc = posix_spawn(&pid, orig_argv[0], &file_actions, &attr, orig_argv, environ);

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.

posix_spawn here uses orig_argv[0] as the executable path. Unlike the previous fork-and-continue flow, this now depends on argv[0] being directly executable from the current working directory.

If the server is started via PATH (for example argv[0] = "clickhouse") or a relative launcher path, posix_spawn can return ENOENT even though the current process is already running correctly. That makes watchdog setup fail on Darwin for valid invocation modes.

Please spawn using a stable absolute executable path (for example getExecutablePath) or use posix_spawnp if PATH lookup is intended.

@clickhouse-gh

clickhouse-gh Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.10% -0.10%
Functions 91.40% 91.40% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 81.82% (9/11) | lost baseline coverage: 3 line(s) · Uncovered code

Full report · Diff report

@leshikus

leshikus commented Jun 8, 2026 •

Copy link
Copy Markdown
Collaborator Author

Sample crash report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106340&sha=b121002f9ec1230c7618d546e1908ed0f54d534e&name_0=PR&name_1=Fast%20test%20%28arm_darwin%29%20%5B4%5D Ips file: https://s3.amazonaws.com/clickhouse-test-reports/PRs/106340/b121002f9ec1230c7618d546e1908ed0f54d534e/fast_test_arm_darwin_4/clickhouse-2026-06-08-163726.ips

The SIGILL ("Illegal instruction: 4", bug_type 309) is the macOS 26 fork-child hardening, not a bug in the reported backtrace. The faulting thread shown (parent main thread parked in waitForTerminationRequest) is misleading — it is napping in a syscall, not crashing. The only authoritative signal is the asi line:

"asi": { "libsystem_c.dylib": ["crashed on child side of fork pre-exec"] }

libsystem_c writes this string and raises SIGILL when a forked child runs non-async-signal-safe code before exec*(). BaseDaemon::setupWatchdog fork()s but never execs, so the child trips the guard. posix_spawn removes the pre-exec window entirely.

@tavplubix

Copy link
Copy Markdown
Member

The analysis doesn't look correct, and I don't think we need this change:

Let's instead try disabling JIT in tests on Mac, if it helps - disable JIT on Mac completely

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants