Skip to content

Allow allocations in NOEXCEPT_SCOPE catch handler - #110174

Merged
azat merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai-fix-asyncloader-spawn-noexcept
Jul 14, 2026
Merged

azat merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai-fix-asyncloader-spawn-noexcept

Conversation

@groeneai

@groeneai groeneai commented Jul 12, 2026 •

Copy link
Copy Markdown
Collaborator

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):

...

Description

Run the NOEXCEPT_SCOPE catch handler under ALLOW_ALLOCATIONS_IN_SCOPE so the real exception is logged before terminate.

CI report (STID 2573-2cd4): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110144&sha=7c516a74c93c6b69606ae441c4a8792afca0b281&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29

Version info

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

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. New gtest NoexceptScopeDeathTest.RealExceptionLoggedUnderDenyAllocations deterministically reproduces the exact pattern: DENY_ALLOCATIONS_IN_SCOPE around a NOEXCEPT_SCOPE whose body does ALLOW_ALLOCATIONS_IN_SCOPE then throws. On the unfixed build it aborts with the identical CI stack (tryLogCurrentException -> CurrentMemoryTracker::allocImpl -> Memory tracker: allocations not allowed).
b Root cause explained? NOEXCEPT_SCOPE_IMPL's catch(...) always allocates (tryLogCurrentException formats a String). When nested in a DENY scope, the body's ALLOW_ALLOCATIONS_IN_SCOPE SCOPE_EXIT restores the outer DENY flag as the try block unwinds, so by the time catch runs the allocation trips the debug assertion — masking the real exception (CANNOT_SCHEDULE_TASK from scheduleOrThrowOnError failing to start a thread).
c Fix matches root cause? Yes. The catch handler now runs under ALLOW_ALLOCATIONS_IN_SCOPE, directly addressing the always-allocating diagnostic path. No band-aid, no bound widening.
d Test intent preserved / new tests added? New regression test added (gtest_noexcept_scope_deny_allocations.cpp), gated on MEMORY_TRACKER_DEBUG_CHECKS. No existing test relaxed.
e Both directions demonstrated? Yes. Unfixed build: test FAILS (aborts on "allocations not allowed", marker exception never logged). Fixed build: test PASSES (real exception logged before std::terminate()). All 24 AsyncLoader tests + MemoryTracker* tests still pass.
f Fix is general across code paths? Yes — the fix is in the shared NOEXCEPT_SCOPE_IMPL macro, so it covers ALL ~10 nested NOEXCEPT_SCOPE call sites (AsyncLoader, ThreadPool, DatabaseCatalog, MergeTreeTransaction, StorageReplicatedMergeTree, MergeTreeBackgroundExecutor, etc.), not just AsyncLoader::spawn. The catch handler is the single choke point where the diagnostic allocation happens.
g Fix generalizes across inputs? Yes. The bug is independent of the thrown exception type/content: any exception reaching the catch handler triggers the allocation. The fix applies unconditionally in the catch block regardless of which body threw or why.
h Backward compatible? Yes. DENY_ALLOCATIONS_IN_SCOPE / ALLOW_ALLOCATIONS_IN_SCOPE are static_assert(true) no-ops in release builds, so this is a debug/sanitizer/CI-only diagnostics fix. No release behavior, on-disk/wire format, or setting default changes.
i Invariants and contracts preserved? Yes. NOEXCEPT_SCOPE's contract is unchanged: it still logs the current exception and std::terminate()s. The only change is that the log now succeeds instead of aborting prematurely — the terminate-on-exception guarantee holds on all paths. LockMemoryExceptionInThread (MEMORY_LIMIT_EXCEEDED suppression) is untouched.

Session id: cron:clickhouse-worker-slot-1:20260712-173700

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @serxa @azat — could you review? NOEXCEPT_SCOPE's catch handler always allocates (tryLogCurrentException), but when it's nested in a DENY_ALLOCATIONS_IN_SCOPE (e.g. AsyncLoader::spawn wrapping scheduleOrThrowOnError) the body's inner ALLOW_ALLOCATIONS_IN_SCOPE unwinds and restores DENY before the handler runs, so the log allocation aborts with a misleading Memory tracker: allocations not allowed in debug/sanitizer builds and hides the real exception. Fix runs the handler under ALLOW_ALLOCATIONS_IN_SCOPE.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3ba5eef]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_tsan) FAIL
Logical error: Block structure mismatch in A stream: different columns: (STID: 0993-28b7) FAIL cidb, issue

AI Review

Summary

This PR changes NOEXCEPT_SCOPE so its catch handler temporarily lifts DENY_ALLOCATIONS_IN_SCOPE before calling tryLogCurrentException, which should let debug and sanitizer builds log the original exception instead of aborting on Memory tracker: allocations not allowed. The implementation itself looks reasonable, but the current revision still lacks focused regression proof for the exact unwind path it claims to fix, so I would not approve it yet.

Findings

⚠️ Majors

  • [src/Common/noexcept_scope.h:20] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/110174#discussion_r3569969919] The promised invariant is that NOEXCEPT_SCOPE now logs the real exception even when the body temporarily uses ALLOW_ALLOCATIONS_IN_SCOPE inside an outer DENY_ALLOCATIONS_IN_SCOPE. The current PR still has no focused proof for that unwind pattern. Existing CI only shows that this header change compiles and survives broad smoke checks; it would not catch a later regression that drops ALLOW_ALLOCATIONS_IN_SCOPE from the catch path or reintroduces an allocation before the guard. Suggested fix: restore a small MEMORY_TRACKER_DEBUG_CHECKS-gated death test that nests DENY_ALLOCATIONS_IN_SCOPE -> NOEXCEPT_SCOPE -> inner ALLOW_ALLOCATIONS_IN_SCOPE, throws, and asserts that the original exception is logged before std::terminate().
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: add a focused regression test for the DENY_ALLOCATIONS_IN_SCOPE unwind path that this PR claims to fix.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 7f829f4

Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stress test (amd_debug) / Block structure mismatch in A stream: different number of columns (STID 0993-386d) flaky (trunk JoinStep duplicate-column, 124 hits / 99 unrelated PRs / 5 master, 30d; not on this PR's noexcept_scope.h path) #109114 (ours, open)

This PR touches only src/Common/noexcept_scope.h + a gtest; it cannot cause a server-side JoinStep block-structure mismatch. Confirmed not PR-caused.

Session id: cron:our-pr-ci-monitor:20260713-020000

@azat azat self-assigned this Jul 13, 2026

@azat azat 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.

Ok, but:

  • remove the useless comment, it can be at max git commit if any
  • remove the test

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jul 13, 2026
Per @azat review on ClickHouse#110174: the comment belongs at most in the commit
message, and the added test is removed.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Done: dropped the comment and removed the test.

@azat

azat commented Jul 13, 2026

Copy link
Copy Markdown
Member

Squash

@azat

azat commented Jul 13, 2026

Copy link
Copy Markdown
Member

And trim the PR description as well, it contains lots of words, while it is enough to include few words + CI report link

@groeneai

Copy link
Copy Markdown
Collaborator Author

Trimmed the description to a one-liner + the CI report link.

@azat

azat commented Jul 13, 2026

Copy link
Copy Markdown
Member

You forgot to squash commits

@groeneai
groeneai force-pushed the groeneai-fix-asyncloader-spawn-noexcept branch from 0c33cae to fdc6ecb Compare July 13, 2026 10:25
@groeneai

Copy link
Copy Markdown
Collaborator Author

Squashed into one commit.

@azat
azat enabled auto-merge July 13, 2026 10:27
Comment thread src/Common/noexcept_scope.h Resolved
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — fdc6ecb

Every failure below has an owner. None is caused by this PR (a 2-line src/Common/noexcept_scope.h change); all are pre-existing trunk flakies/bugs tracked separately.

Check / test Reason Owner / fixing PR
Stateless (amd_debug / amd_asan_ubsan / amd_msan WasmEdge / amd_tsan s3 / arm_binary, sequential) / 02346_text_index_bug108519_qcc_skip_index flaky (materialize_statistics_on_insert randomization; 52 master + 30+ unrelated PRs, all 2026-07-13) #110260 (ours, merged 15:05Z) — clears on next master merge
Integration tests (amd_tsan) / test_replicated_database::test_replicated_table_structure_alter flaky (35 master + 173 unrelated PRs, 7d) a fix task is investigating at full effort (fixing-PR link to follow here)
Stateless (amd_tsan, s3 storage) / Scraping system tables infra (minio_audit_logs dump 600s timeout on s3 configs) #110099 (ours, open)
Stress test (amd_debug) / Block structure mismatch in stream (STID 0993-386d) logical-error (chronic JoinStep duplicate-column trunk family) #109114 (ours, open)
CH Inc sync — CH Inc sync (private, not actionable)

Session id: cron:our-pr-ci-monitor:20260713-160000

@azat

azat commented Jul 13, 2026

Copy link
Copy Markdown
Member

Rebase, most of the failures has been fixed.

When NOEXCEPT_SCOPE is nested inside a DENY_ALLOCATIONS_IN_SCOPE and its body opens a local ALLOW_ALLOCATIONS_IN_SCOPE (e.g. AsyncLoader::spawn wrapping ThreadPool::scheduleOrThrowOnError), a throw from the body unwinds the body's ALLOW scope and restores the outer DENY before the catch handler runs. The catch handler always allocates (tryLogCurrentException formats the message), so under the restored DENY it aborted in debug/sanitizer builds with a misleading LOGICAL_ERROR, hiding the real exception. Run the catch handler under ALLOW_ALLOCATIONS_IN_SCOPE so the real exception is logged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
auto-merge was automatically disabled July 13, 2026 16:48

Head branch was pushed to by a user without write access

@groeneai
groeneai force-pushed the groeneai-fix-asyncloader-spawn-noexcept branch from fdc6ecb to 3ba5eef Compare July 13, 2026 16:48
@groeneai

Copy link
Copy Markdown
Collaborator Author

Rebased onto current master.

@azat
azat enabled auto-merge July 13, 2026 16:52
@clickhouse-gh

clickhouse-gh Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.80% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.00% +0.00%

Changed lines: Changed C/C++ lines covered: 3/6 (50.00%) · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 3ba5eef

CI fully finished on the rebased head (Finish Workflow SUCCESS; @azat APPROVED). Every failure below has an owner. Not PR-caused: this PR is src/Common/noexcept_scope.h +2/-0 (allow allocations in the NOEXCEPT_SCOPE catch handler); it cannot cause an AST-fuzzer block-structure mismatch.

Check / test Reason Owner / fixing PR
AST fuzzer (amd_tsan) / Block structure mismatch in stream (STID 0993-28b7) chronic JoinStep duplicate-column trunk family #109114 (ours, open)
CH Inc sync — CH Inc sync (private, not actionable)

Session id: cron:our-pr-ci-monitor:20260713-230000

@azat
azat added this pull request to the merge queue Jul 14, 2026
Merged via the queue into ClickHouse:master with commit 0840bc6 Jul 14, 2026
174 of 176 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 14, 2026
alexey-milovidov added a commit that referenced this pull request Jul 16, 2026
Pulls in #110174 (merged 2026-07-14), which fixes the `AsyncLoader::spawn`
`std::terminate` crash (STID: 2028-288e) hit by the AST fuzzer job on this PR's
own CI, among ~1400 other commits.
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Aug 15, 2026
AsyncLoader::spawn wrapped scheduleOrThrowOnError in NOEXCEPT_SCOPE, whose
handler is unconditionally std::terminate(). A transient CANNOT_SCHEDULE_TASK
from a saturated global thread pool therefore killed the server during table
loading, seen 4 times across 4 unrelated PRs and 4 build flavours in 30 days.

A spawn must succeed only when nothing else will drain the pool's ready queue,
so spawn() now tells three states apart: a worker that entered worker() and is
not inside wait() drains the queue now; a worker inside wait() on another
pool's job resumes draining once that job runs; neither of those means nobody
ever will. The first two use the non-throwing trySchedule, roll back the
Pool::workers increment on failure, warn and return false. Only the last keeps
the fatal path, byte for byte, because the queued jobs would be stranded and
schedule() marks that region non-exception-safe.

Two counters are added because Pool::workers also counts a worker still queued
in the global pool, which drains nothing, and one blocked in wait(), which
cannot take its own pool's jobs. A saturated pool is remembered so the failing
thread creation happens once per episode rather than once per queued job,
counted by the new AsyncLoaderSpawnFailures event; entering wait() clears it.

The regression tests assert which branch was taken rather than only that the
process survived, and resolve the ProfileEvent by name so that the file also
links against a tree where the event does not exist. That keeps Bugfix
validation able to reach a verdict: the before-binary links, runs, and aborts
in spawn() as the bug requires. A SQL test cannot reach the line, since on
master the whole call sits under blockFaultInjections().

Related: ClickHouse#110174, which removed the mask that hid this exception.
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…loader-spawn-noexcept

Allow allocations in NOEXCEPT_SCOPE catch handler
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 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.

4 participants