Skip to content

Use an 8 MiB thread stack on Darwin to avoid stack overflow in JIT codegen - #107033

Merged
Algunenano merged 3 commits into
ClickHouse:masterfrom
Algunenano:darwin-thread-stack-size
Jun 11, 2026
Merged

Algunenano merged 3 commits into
ClickHouse:masterfrom
Algunenano:darwin-thread-stack-size

Conversation

@Algunenano

@Algunenano Algunenano commented Jun 10, 2026 •

Copy link
Copy Markdown
Member

On macOS (Darwin), secondary threads get only a 512 KiB stack by default, whereas on Linux the default follows RLIMIT_STACK (usually 8 MiB). That is too small for some deep call stacks: in particular, recursive scalar-subquery analysis that triggers LLVM JIT codegen. JIT codegen is stack-hungry and does not cooperate with checkStackSize, so it walks straight off the end into the guard page and the server dies (only visible on arm_darwin).

The crash surfaces in .ips reports with SIGILL and the asi note libsystem_c.dylib: crashed on child side of fork pre-exec. That note is a red herring: it is a generic annotation macOS attaches to any crash in a process that has forked without exec (the watchdog child), not a cause. The crash reproduces with the watchdog disabled too. The real fault is a stack overflow inside AArch64AsmPrinter::emitEndOfAsmFile during CHJIT compilation.

Fix: on Darwin, create our worker threads with an 8 MiB stack, matching the typical Linux default, at the two thread-creation mechanisms that matter:

  • the global thread pool, via a small std::thread-compatible wrapper (std::thread offers no way to set the stack size);
  • the Poco server_pool used for connection handlers.

This also lets us drop the Darwin-specific, more-frequent checkStackSize in the parser added in #102645, since the larger stack restores Linux-equivalent headroom.

Linux behavior is unchanged.

CI 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

Related: #103235
Related: #105921
Closes #107175

Changelog category (leave one):

  • Improvement

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

Use a larger (8 MiB) thread stack on macOS to match the Linux default, fixing rare server crashes from stack overflow during JIT compilation.

Improvement since Darwin has never been production ready

Version info

  • Merged into: 26.6.1.637

@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dae1e25]

Summary: ✅


AI Review

Summary

This PR changes Darwin worker-thread creation so GlobalThreadPool uses ThreadWithStackSize and the Poco server pool requests an 8 MiB stack, then adds a stateless regression query that exercises recursive scalar-subquery analysis with JIT codegen. I did not find a code-level correctness issue in the current diff.

PR Metadata
  • The selected changelog category Improvement does not match the stated user impact: the PR fixes a server crash caused by stack overflow during JIT compilation. Use Critical Bug Fix (crash, data loss, RBAC) instead. The changelog entry itself is specific and user-readable.
Missing context / blind spots
  • ⚠️ Full CI is still pending. The Praktika PR report currently has no failed tests, and Style check passed, but Fast test, Build (arm_tidy), and the aggregate PR check were still pending when reviewed. The platform-specific gap closes once the arm_darwin fast test covers 04324_jit_in_scalar_subquery_stack.
Findings
  • 💡 Nits
    • [PR description] Changelog category should be Critical Bug Fix (crash, data loss, RBAC) rather than Improvement, because the PR promises to fix rare server crashes from stack overflow during JIT codegen.
    • Suggested fix: replace the category with:
      ### Changelog category (leave one):
      - Critical Bug Fix (crash, data loss, RBAC)
Final Verdict

Status: ✅ Approve

No code changes required from this review; the changelog category should be corrected before merge.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 10, 2026
Comment thread src/Common/ThreadWithStackSize.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.70% +0.10%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.30% +0.10%

Changed lines: Changed C/C++ lines covered by tests: 41/41 (100.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 3 line(s) · Uncovered code

Full report · Diff report

@Algunenano
Algunenano enabled auto-merge June 11, 2026 10:20
@Algunenano
Algunenano added this pull request to the merge queue Jun 11, 2026
Merged via the queue into ClickHouse:master with commit 4c8f6de Jun 11, 2026
326 of 328 checks passed
@Algunenano
Algunenano deleted the darwin-thread-stack-size branch June 11, 2026 10:38
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 11, 2026
alexey-milovidov added a commit that referenced this pull request Jun 13, 2026
…in `Server::main`

Master now sizes connection-pool thread stacks with
`DEFAULT_THREAD_STACK_SIZE` (8 MiB on Darwin, where the OS default of
512 KiB is too small for JIT codegen) — see #107033. The listener pool
in `clickhouse-local` mirrors the pool configuration of `Server::main`,
so apply the same stack size here.

Co-Authored-By: Claude Fable 5 <[email protected]>
wilkermichael pushed a commit to Shopify/ClickHouse that referenced this pull request Nov 3, 2026
For `LocalConnection` (interactive `clickhouse-local`) the whole suggestion
query runs in-process in `Suggest::loading_thread`: analyzer, planner, and the
regexp JIT that `extractAll(word, '[\\w_]{2,}')` triggers during header
evaluation. The thread was a plain `std::thread`, which macOS gives a 512 KiB
stack, and LLVM codegen does not cooperate with `checkStackSize`, so it ran
into the guard page (`EXC_BAD_ACCESS "Thread stack size exceeded"` in thread
`Suggest`). This killed the `clickhouse-local` under test in
`Fast test (arm_darwin)` for `05024_local_listener_survives_handler_exception`
(curl rc 56) and `04892_client_dialect_command` (`spawn id not open`) on
PRs ClickHouse#95355, ClickHouse#88234, ClickHouse#117004 and ClickHouse#117636: 26 of 26 macOS crash reports of those
jobs show this stack.

ClickHouse#107033 gave the global thread pool and the server pools
`DEFAULT_THREAD_STACK_SIZE` via `ThreadWithStackSize`, and 3510173 did
the same for the `clickhouse-local` listener pool; the suggestion loader was
missed. Use `ThreadWithStackSize` for it on Darwin; other platforms keep
`std::thread`, whose default stack follows `RLIMIT_STACK`.

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-improvement Pull request with some product improvements 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.

Intermittent SIGILL on macOS arm fast test

2 participants