Skip to content

Fix server abort when a distributed-plan worker status check fails to re-enqueue - #107575

Merged
davenger merged 2 commits into
ClickHouse:masterfrom
groeneai:fix/distributed-tasktracker-noexcept-7594
Jun 16, 2026
Merged

davenger merged 2 commits into
ClickHouse:masterfrom
groeneai:fix/distributed-tasktracker-noexcept-7594

Conversation

@groeneai

@groeneai groeneai commented Jun 16, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a server abort (std::terminate, signal 6) during teardown of a distributed-plan query (make_distributed_plan = 1) when a worker status-check failed to re-schedule the next check (for example CANNOT_SCHEDULE_TASK on shutdown or MEMORY_LIMIT_EXCEEDED). The query now fails cleanly and the server keeps running.

Description

Found by CI (Stress test, AST/BuzzHouse fuzzers) as a recurring Received signal 6 /
std::terminate in ~DistributedQueryPlanExecutorRemote. CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107565&sha=8696805ace5dc74831f1ee4ac4645fbe5d43d71d&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29

Root cause: the remote executor's TaskTracker polls worker task status on a thread
pool. Each worker lambda, after checkStatusFunc, re-enqueues the next check via
enqueueGetStatus() -> ThreadPool::scheduleOrThrow(). That tail ran outside the lambda's
try/catch. scheduleOrThrow() can throw CANNOT_SCHEDULE_TASK during shutdown, and the
lambda can hit MEMORY_LIMIT_EXCEEDED under fault injection. The exception escaped the
lambda, was stored as the pool's first_exception, and was rethrown by thread_pool.wait()
in ~TaskTracker. An exception leaving a destructor calls std::terminate, aborting the
server during pipeline teardown:

std::terminate
  ~TaskTracker -> thread_pool.wait()
  ~DistributedQueryPlanExecutorRemote
  ~ReadFromDistributedPlanSource

Fix (two layers):

  1. Route the re-enqueue through the same failure path as the status check: the lambda tail
    is wrapped in try/catch and funnelled into a new recordFailure() helper that stores
    first_exception and requests cancellation, so the query fails cleanly via
    checkCancelled() instead of escaping the thread.
  2. ~TaskTracker is made exception-safe as a last line of defense: thread_pool.wait()
    still runs (it joins worker threads while logger/lock/context are alive), but a rethrown
    exception is now logged and swallowed instead of terminating.

Reproduction and validation: a REGULAR failpoint
distributed_plan_status_check_reenqueue_fault is added at the re-enqueue site. Built both
ways and verified locally with a single-node self-worker setup:

  • Without the fix: the failpoint deterministically aborts the server with the exact
    std::terminate -> ~TaskTracker -> ~DistributedQueryPlanExecutorRemote stack above.
  • With the fix: 50/50 fault-injected distributed queries fail cleanly (CANNOT_SCHEDULE_TASK)
    and the server stays alive.

New stateless test 04340_distributed_plan_status_reenqueue_fault enables the failpoint,
runs distributed queries, and asserts the server is still serving queries afterwards.

No related open issue found (the broader multi-stage distributed family is tracked under
the re-land PR #107146).

Version info

  • Merged into: 26.6.1.892

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. With a single-node self-worker setup and the new REGULAR failpoint enabled, a remote distributed query (make_distributed_plan=1, distributed_plan_execute_locally=0) deterministically aborts the unfixed server: SYSTEM ENABLE FAILPOINT distributed_plan_status_check_reenqueue_fault then SELECT x, count() FROM t GROUP BY x SETTINGS make_distributed_plan=1, enable_parallel_replicas=0, distributed_plan_execute_locally=0, ....
b Root cause explained? The TaskTracker worker lambda re-enqueues the next status check (enqueueGetStatus() -> ThreadPool::scheduleOrThrow()) outside its try/catch. scheduleOrThrow can throw CANNOT_SCHEDULE_TASK on shutdown (or the lambda hits MEMORY_LIMIT_EXCEEDED); the exception escapes the lambda, is stored as the pool's first_exception, and is rethrown by thread_pool.wait() in ~TaskTracker. An exception leaving a destructor calls std::terminate -> signal 6, during ~DistributedQueryPlanExecutorRemote teardown.
c Fix matches root cause? Yes. (1) The re-enqueue tail is moved inside a try/catch that funnels the failure into first_exception (via recordFailure()) so it surfaces as a clean query error through checkCancelled() rather than escaping the worker thread. (2) ~TaskTracker swallows-and-logs any rethrow from thread_pool.wait() so no exception can ever escape the destructor. Both target the exact mechanism in (b).
d Test intent preserved / new tests added? New stateless test 04340_distributed_plan_status_reenqueue_fault.sh added: enables the failpoint, runs distributed queries, asserts the server is still alive. New REGULAR failpoint distributed_plan_status_check_reenqueue_fault added at the re-enqueue site to drive it. No existing test weakened.
e Both directions demonstrated? Yes, on Build-ID-verified binaries. Unfixed (Build ID 51fa7851): server aborts with std::terminate -> ~TaskTracker (thread_pool.wait()) -> ~DistributedQueryPlanExecutorRemote, matching the CI stack. Fixed (Build ID dbbdbc2b): 50/50 fault-injected distributed queries fail cleanly (CANNOT_SCHEDULE_TASK) and the server keeps serving.
f Fix is general, not a narrow patch? Yes. The LocalExecutor path uses futures (promise.set_exception), not TaskTracker, so it already routes failures safely; only the remote TaskTracker had the escaping tail. The fix addresses the root (an exception-unsafe re-enqueue + a throwing destructor), not a single symptom: it covers any throwing source at the lambda tail (shutdown, memory limit), and the destructor guard protects against any future rethrow path into wait(). The existing inner checkStatusFunc catch and the new tail catch now share one recordFailure() helper.

Session id: cron:clickhouse-worker-slot-3:20260615-232600

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @davenger @KochetovNicolai — could you review? This fixes a std::terminate server abort in ~DistributedQueryPlanExecutorRemote (TaskTracker re-enqueue throwing out of the worker lambda, then rethrown by thread_pool.wait() in the destructor) that CI keeps hitting on the re-landed multi-stage distributed feature (#107146). It does not touch the feature's logic — just makes the status-poll re-enqueue and the ~TaskTracker destructor exception-safe, with a failpoint-driven regression test.

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

clickhouse-gh Bot commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [962f687]

Summary: ✅


AI Review

Summary

This PR makes DistributedQueryPlanExecutorRemote::TaskTracker route status-check and re-enqueue failures through recordFailure, and guards ~TaskTracker so ThreadPool::wait cannot let an exception escape during teardown. The current code matches the described failure mode, and the timestamp-scoped system.text_log assertion addresses the prior vacuous-pass concern in the regression test.

PR Metadata
  • 💡 The selected category is Bug Fix (user-visible misbehavior in an official stable release), but the entry describes a server abort via std::terminate, which fits the template category Critical Bug Fix (crash, data loss, RBAC) better.
  • Suggested replacement category: Critical Bug Fix (crash, data loss, RBAC).
  • Suggested replacement entry: Fixes a server abort during teardown of a distributed-plan query with make_distributed_plan = 1 when worker status-check re-enqueue fails, for example with CANNOT_SCHEDULE_TASK during shutdown or MEMORY_LIMIT_EXCEEDED. The query now fails cleanly and the server keeps running.
Final Verdict

Status: ✅ Approve

No code-review changes are required. The metadata nit above is optional cleanup for template precision.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 16, 2026
@groeneai
groeneai force-pushed the fix/distributed-tasktracker-noexcept-7594 branch 4 times, most recently from c1563f3 to f982c6c Compare June 16, 2026 05:15
… re-enqueue

The remote distributed-plan executor (DistributedQueryPlanExecutorRemote) tracks
worker tasks with a TaskTracker that polls their status on a thread pool. Each
status-check worker lambda, after running checkStatusFunc, re-enqueues the next
check via enqueueGetStatus() -> ThreadPool::scheduleOrThrow(). That tail ran
outside the lambda's try/catch.

scheduleOrThrow() can throw CANNOT_SCHEDULE_TASK during shutdown, and the lambda
can also hit MEMORY_LIMIT_EXCEEDED (e.g. under serverfuzz fault injection). Such
an exception escaped the worker lambda, was stored as the pool's first_exception,
and then rethrown by thread_pool.wait() in ~TaskTracker. An exception leaving a
destructor calls std::terminate, aborting the server (Received signal 6) during
pipeline teardown:

    std::terminate
      ~TaskTracker -> thread_pool.wait()
      ~DistributedQueryPlanExecutorRemote
      ~ReadFromDistributedPlanSource

Fix:
- Route the re-enqueue through the same failure path as the status check: wrap
  the lambda tail in try/catch and funnel both into a new recordFailure() helper
  that stores first_exception and requests cancellation, so the query fails
  cleanly via checkCancelled() instead of escaping the thread.
- Make ~TaskTracker exception-safe as a last line of defense: thread_pool.wait()
  must still run (it joins worker threads while logger/lock/context are alive),
  but a rethrown exception is now logged and swallowed instead of terminating.

A REGULAR failpoint distributed_plan_status_check_reenqueue_fault is added at the
re-enqueue site to make the crash reproducible on demand. The new stateless test
(04340) enables it and runs distributed queries; it then asserts the fault path
actually ran by checking system.text_log for the TaskTracker::recordFailure entry
(emitted on the pool thread on every re-enqueue), and verifies the server is still
alive. The recordFailure log is used rather than the client-visible error because
whether the query surfaces the stored exception to the client is racy, while the
log is deterministic, so the test does not become flaky under randomized settings.
The assertion is scoped to the current run by capturing a microsecond timestamp
before enabling the failpoint and filtering text_log on event_time_microseconds,
so a recordFailure row left by an earlier run on the same server (the flaky check
reruns the test in place) cannot make a later run pass without firing the failpoint.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai
groeneai force-pushed the fix/distributed-tasktracker-noexcept-7594 branch from f982c6c to 7f4c024 Compare June 16, 2026 06:22
@davenger

Copy link
Copy Markdown
Member

@groeneai resolve the conflicts with master

@groeneai

Copy link
Copy Markdown
Collaborator Author

@davenger thanks for merging master in (962f687) — branch is conflict-free now (mergeable=MERGEABLE). Confirmed the 2-layer fix and the 04340 failpoint test survived the merge intact (3-dot diff vs master = the same 4 files, no stray markers). CI is re-running on the new head.

@clickhouse-gh

clickhouse-gh Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.20% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.40% 77.30% -0.10%

Changed lines: Changed C/C++ lines covered by tests: 25/27 (92.59%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@davenger
davenger added this pull request to the merge queue Jun 16, 2026
Merged via the queue into ClickHouse:master with commit 7b9fa0e Jun 16, 2026
166 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 16, 2026
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-bugfix Pull request with bugfix, not backported by default 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.

5 participants