Skip to content

Stop clickhouse_fuzzer from self-inflicting timeouts via SIGALRM signals - #105176

Merged
yakov-olkhovskiy merged 4 commits into
masterfrom
fix-clickhouse-fuzzer-signal-races
May 20, 2026
Merged

yakov-olkhovskiy merged 4 commits into
masterfrom
fix-clickhouse-fuzzer-signal-races

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented May 17, 2026 •

Copy link
Copy Markdown
Member

On the libFuzzer CI job, clickhouse_fuzzer has been failing intermittently with timeout after 21 seconds on trivial inputs (e.g. \"7\\0\\0\") where the runner-thread stack snapshot shows it idle at cv.wait, not running a query. CIDB shows 19 such hits on PR #99740 in April plus this fresh one on PR #104869.

The wrapper in programs/local/fuzzers/clickhouse_fuzzer.cpp is fighting libFuzzer's own SIGALRM machinery. libFuzzer arms setitimer(ITIMER_REAL) with an interval of UnitTimeoutSec / 2 + 1 seconds (11 s for our 20 s timeout), so SIGALRM fires periodically — not only at the actual timeout. The wrapper had three bugs that compounded into spurious timeouts:

  1. SIGALRM was not pinned to a thread. Installed process-wide via sigaction, the kernel could deliver it to either the libFuzzer main thread or the clickhouse-local runner thread. When it landed on the runner, fuzzerSigalrmHandler pthread_kill'd SIGUSR1 to itself and raced with a concurrent invocation on the main thread; two competing sigaction restores then corrupted the handler state. The failing CI trace shows exactly that interleaving (#4 fuzzerSigalrmHandler inside the runner's stack at clickhouse_fuzzer.cpp:212 — the pthread_kill line).
  2. The slow runner-stack dump ran on every periodic SIGALRM. __sanitizer_print_stack_trace shells out to llvm-symbolizer (fork+exec, not async-signal-safe) and can take many seconds. Combined with the 3-second wait-for-stack window, it could push an otherwise-fast iteration past the 20 s limit and trigger a real timeout — a self-fulfilling failure.
  3. The wrapper restored libFuzzer's original handler before raise(SIGALRM), making itself one-shot. A periodic alarm at second ~11 consumed the wrapper, so when the genuine timeout fired at second 20 there was no longer any wrapper to dump the runner stack.

Fix all three:

  • Block SIGALRM on the runner thread at the top of runLibFuzzer (pthread_sigmask(SIG_BLOCK, …)) so the wrapper only runs on the libFuzzer main thread.
  • Track per-iteration start time via clock_gettime(CLOCK_MONOTONIC) and skip the SIGUSR1 stack-dump unless elapsed ≥ 15 s, so the periodic 11 s alarms pass through without disturbing the runner. Guard with an atomic to keep the dump single-shot per process.
  • Call libFuzzer's original handler directly via the saved sigaction.sa_sigaction / sa_handler instead of permanently restoring it. The wrapper stays installed and keeps intercepting later alarms.

CI report that surfaced this: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104869&sha=4bb6825eb6220feaca31f3d2608dae09d5de7927&name_0=PR&name_1=libFuzzer%20tests (PR #104869)

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

  • Not required (CI-only change)

🤖 Generated with Claude Code

Version info

  • Merged into: 26.5.1.913

libfuzzer arms `setitimer(ITIMER_REAL)` with an interval of
`UnitTimeoutSec / 2 + 1` seconds (11 s for our 20 s timeout), so SIGALRM
fires periodically — not only at the actual timeout. The wrapper handler
in `clickhouse_fuzzer.cpp` had three issues that together produced
spurious "timeout after 21 seconds" failures (seen on PRs #99740 and
#104869):

1. SIGALRM has no per-thread routing. The handler is installed
   process-wide via `sigaction`, so the kernel can deliver it to either
   the libfuzzer main thread or the runner thread. When it landed on the
   runner, `fuzzerSigalrmHandler` `pthread_kill`'d SIGUSR1 to itself and
   raced with a concurrent invocation on the main thread, with two
   competing `sigaction` restores corrupting handler state.
2. The slow runner-stack dump (shells out to llvm-symbolizer) ran on
   every periodic SIGALRM, even when the iteration was nowhere near its
   timeout. The dump alone takes many seconds; combined with the 3-second
   wait-for-stack window it could push an otherwise-fast iteration past
   the 20 s limit and trigger a real timeout.
3. The wrapper permanently restored libfuzzer's original handler before
   `raise(SIGALRM)`, making itself one-shot. A periodic SIGALRM at second
   ~11 consumed the wrapper, so when the genuine timeout fired at
   second 20 there was no longer any wrapper to dump the runner stack.

Fix all three:

- Block SIGALRM on the runner thread at the top of `runLibFuzzer` so the
  wrapper only ever runs on the libfuzzer main thread.
- Track per-iteration start time via `clock_gettime(CLOCK_MONOTONIC)` and
  skip the SIGUSR1 stack-dump unless elapsed ≥ 15 s, so the 11 s
  periodic alarms pass through without disturbing the runner. Guard with
  an atomic to keep the dump single-shot per process.
- Call libfuzzer's original handler directly (via the saved
  `sigaction.sa_sigaction` / `sa_handler`) instead of permanently
  restoring it. The wrapper stays installed and keeps intercepting
  later alarms.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104869&sha=4bb6825eb6220feaca31f3d2608dae09d5de7927&name_0=PR&name_1=libFuzzer%20tests
#104869

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3d82b2e]

Summary: ✅


AI Review

Summary

This PR fixes clickhouse_fuzzer signal-timeout interactions by blocking SIGALRM on the runner thread, deriving timeout gating from libFuzzer's -timeout value, and forwarding to libFuzzer's original handler without uninstalling the wrapper. I re-checked prior discussion and current code at PR head; the previously reported threshold/latch issue is addressed, and I found no new actionable correctness, safety, or performance issues.

Final Verdict

Status: ✅ Approve
No required follow-up actions.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 17, 2026
…out=N`

The wrapper's previous hardcoded `timeout_imminent_sec = 15` was wrong for
any `-timeout` other than the 20 s default:

- With `-timeout=30`, libfuzzer fires SIGALRM at ~16 s and ~32 s. The first
  alarm already satisfies `elapsed >= 15`, so `dump_started` is consumed
  before any real timeout; the second alarm at 32 s (the actual timeout)
  then skips the runner-stack dump.
- For `-timeout < 15`, the dump is never attempted.

Match libfuzzer's own condition instead:

- Parse `-timeout=N` from argv in `LLVMFuzzerInitialize` into a file-scope
  `unit_timeout_sec` atomic (default 1200, libfuzzer's default).
- Trigger the dump when `elapsed_sec >= unit_timeout_sec`, which is exactly
  when libfuzzer's `AlarmCallback` declares a timeout, so the periodic
  `UnitTimeoutSec / 2 + 1` second ticks pass through untouched at every
  `-timeout=N` value.
- Move `dump_started` from a function-scope `static` to file scope and
  reset it at the top of each `LLVMFuzzerTestOneInput` call, so a stray
  late SIGALRM in one iteration cannot suppress the dump on a real timeout
  in a later iteration.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, the only test-job failure on this PR is 02180_group_by_lowcardinality in Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel):

This is unrelated to the change in this PR (which only touches programs/local/fuzzers/clickhouse_fuzzer.cpp, the libFuzzer SIGALRM wrapper).

CIDB shows this test failing with the same result differs with reference signature on 148 distinct PRs and master in the last 30 days, so it is a long-standing master-wide flaky. Could you investigate and provide a fix in a separate PR — adding a deterministic ORDER BY to the test is likely sufficient.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov — already fixed and merged this morning, but this PR (#105176) predates the merge so it doesn't yet carry the fix.

Timeline:

tests/queries/0_stateless/02180_group_by_lowcardinality.reference at this PR's HEAD still contains:

{"val":"1563.8","avg(toUInt32(val))":null}
{"val":"891.4","avg(toUInt32(val))":null}
…

Master HEAD has {"count()":10}.

CIDB confirms master is fully stable post-merge — every failure since 08:08:57 UTC is on an unrebased PR:

Hour (UTC, 2026-05-18) master failures PR failures
02:00 (pre-merge) 5 19
04:00 (pre-merge) 0 9
08:00 (merge: 08:08:57) 0 5
09:00 0 3
10:00 0 1

(9+ hours, 0 master failures.) The 6 PRs that hit the failure post-merge — #101354, #101039, #100377, #99774, #104513, #104606, and this PR — are all branched off pre-#105194 master.

Recommended action: merge master into this PR. No separate fix-PR is needed — #105194 already does what your directive asked for (and was your own merge, FYI). Will retitle/close this directive task on my side once you confirm.

(Cron session: cron:clickhouse-ci-task-worker:20260518-164500)

Two comment corrections:

1. The `dump_started` comment said "concurrent SIGALRM deliveries can't
   run the dump in parallel" — but since SA_NODEFER is not set, the kernel
   blocks SIGALRM while the handler executes, so there is no true
   concurrency. The guard actually prevents multiple *sequential*
   re-invocations (from pending signals delivered after the handler returns)
   from repeating the dump within one iteration.

2. The comment describing libfuzzer's timeout condition said
   `elapsed > UnitTimeoutSec` but the actual code in FuzzerLoop.cpp uses
   `Seconds >= (size_t)Options.UnitTimeoutSec`. The wrapper's code already
   correctly uses `>=`; only the comment was wrong.

Co-authored-by: Yakov Olkhovskiy <[email protected]>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ alexey-milovidov
❌ cursoragent
You have signed the CLA already but the status is still pending? Let us recheck it.

@yakov-olkhovskiy yakov-olkhovskiy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@yakov-olkhovskiy
yakov-olkhovskiy added this pull request to the merge queue May 20, 2026
Merged via the queue into master with commit 0fa5c65 May 20, 2026
327 of 328 checks passed
@yakov-olkhovskiy
yakov-olkhovskiy deleted the fix-clickhouse-fuzzer-signal-races branch May 20, 2026 20:34
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label May 20, 2026
DavidHe-2008 pushed a commit to DavidHe-2008/ClickHouse that referenced this pull request Jun 1, 2026
…uzzer-signal-races

Stop clickhouse_fuzzer from self-inflicting timeouts via SIGALRM signals
zeekay added a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Two comment corrections:

1. The `dump_started` comment said "concurrent SIGALRM deliveries can't
   run the dump in parallel" — but since SA_NODEFER is not set, the kernel
   blocks SIGALRM while the handler executes, so there is no true
   concurrency. The guard actually prevents multiple *sequential*
   re-invocations (from pending signals delivered after the handler returns)
   from repeating the dump within one iteration.

2. The comment describing libfuzzer's timeout condition said
   `elapsed > UnitTimeoutSec` but the actual code in FuzzerLoop.cpp uses
   `Seconds >= (size_t)Options.UnitTimeoutSec`. The wrapper's code already
   correctly uses `>=`; only the comment was wrong.

Reference: ClickHouse#105176

Co-authored-by: Yakov Olkhovskiy <[email protected]>
syoshida20 pushed a commit to syoshida20/ClickHouse that referenced this pull request Nov 1, 2026
…a timeout

The nightly `clickhouse_fuzzer` run on master reported `libFuzzer: timeout
after 28 seconds` on a 228-byte input that runs in milliseconds:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=9f79cc172306dbf177534ef194cafc034a598a33&name_0=NightlyFuzzers&name_1=libFuzzer%20tests

`fuzzerSigalrmHandler` decided that an alarm was the timeout with its own
check: whole seconds since the start of the last `LLVMFuzzerTestOneInput`,
a start that was never cleared when the callback returned. libFuzzer's
`AlarmCallback` ignores an alarm that lands on another thread (every
ClickHouse thread except the runner has SIGALRM unblocked) or arrives while
no callback is running (for example the leak check after a seed). In such
an alarm the wrapper sent SIGUSR1 to the idle runner, which then spent tens
of seconds symbolizing its stack while libFuzzer carried on, and the next
callback waited for it until libFuzzer timed that one out. Whole-second
rounding could also trigger the dump up to a second early.

Dump only on libFuzzer's own fatal condition: on the fuzzing thread, inside
`LLVMFuzzerTestOneInput` (the start is now cleared on return), and with
`-timeout` elapsed by `CLOCK_REALTIME` in nanoseconds, the clock libFuzzer
uses. The wrapper's start is taken after libFuzzer's `UnitStartTime`, so a
dump implies that libFuzzer exits at the same alarm.

Validated with an x86 ASan libFuzzer build with the CI flags, `-timeout=2`,
seeds that grow a `Memory` table (long leak checks) alternating with the CI
input: the base binary reported a false timeout in 3 of 3 runs, the fix ran
all 40 seeds in 8 of 8. A genuine timeout still exits with code 70, and a
process-directed SIGUSR1 still stops fuzzing gracefully.

Related: ClickHouse#105176
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

6 participants