Skip to content

Fix "already attached to a group" abort when a callback-runner task is dropped synchronously - #109111

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-callback-runner-drop-attached-group
Jul 7, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-callback-runner-drop-attached-group

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Related: #107383

~CallbackRunnerTask (in threadPoolCallbackRunnerUnsafe) releases a dropped task's callback under the task's thread group. A task can be dropped in two ways:

  • Drained by a pool worker when the pool shuts down — a thread with no group attached.
  • When scheduleOrThrowOnError throws synchronously (the pool is shutting down or its queue is full), the just-created job lambda that solely owns the task is destroyed during stack unwinding on the scheduling thread, which is typically running a query and already attached to its own thread group.

The drop path constructed ThreadGroupSwitcher with the default allow_existing_group=false, so in the second case it raised the Thread () is already attached to a group LOGICAL_ERROR. Constructing a LOGICAL_ERROR aborts the server in builds that treat logical errors as assertions (stress/sanitizer), so ThreadGroupSwitcher's own try/catch could not prevent the abort.

Observed in the arm_release stress test on master during MergeTreeDeduplicationLog::shutdown → WriteBufferFromS3 finalize → TaskTracker::add, where the persistent S3 write scheduler had captured an earlier query's thread group.

Fix: pass allow_existing_group=true on the drop path so ThreadGroupSwitcher saves and restores the scheduling thread's group instead of asserting. Added a regression test that forces a synchronous schedule failure (via CannotAllocateThreadFaultInjector) while attached to a different group and verifies the callback is released under the task's group without aborting.

This is a regression introduced by #107383.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=85408e96f5c6339850577d9702ec5717665868a5&name_0=MasterCI&name_1=Stress%20test%20%28arm_release%29

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

Fixed a spurious Thread is already attached to a group logical error (a server abort in assertion-enabled builds) when a threadPoolCallbackRunnerUnsafe task is dropped synchronously on a thread that is already attached to another thread group.

🤖 Generated with Claude Code

…s dropped synchronously

`~CallbackRunnerTask` releases the dropped task's callback under the task's thread
group. A task can be dropped in two ways: drained by a pool worker (a thread with
no group attached), or -- when `scheduleOrThrowOnError` throws synchronously (the
pool is shutting down or its queue is full) -- destroyed during stack unwinding on
the scheduling thread, which is typically running a query and already attached to
its own thread group.

The drop path constructed `ThreadGroupSwitcher` with the default
`allow_existing_group=false`, so in the second case it raised the "Thread is already
attached to a group" `LOGICAL_ERROR`. Constructing a `LOGICAL_ERROR` aborts the
server in builds that treat logical errors as assertions (stress/sanitizer builds),
so `ThreadGroupSwitcher`'s own try/catch could not prevent the abort.

Observed in the `arm_release` stress test during `MergeTreeDeduplicationLog::shutdown`
-> `WriteBufferFromS3` finalize -> `TaskTracker::add`, where the persistent S3 write
scheduler had captured an earlier query's thread group.

Pass `allow_existing_group=true` on the drop path so `ThreadGroupSwitcher` saves and
restores the scheduling thread's group instead of asserting.

Added a regression test that forces a synchronous schedule failure (via
`CannotAllocateThreadFaultInjector`) while attached to a different group and verifies
the callback is released under the task's group without aborting.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=85408e96f5c6339850577d9702ec5717665868a5&name_0=MasterCI&name_1=Stress%20test%20%28arm_release%29

Caused by: #107383

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

clickhouse-gh Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9307102]

Summary: ✅


AI Review

Summary

This PR fixes the threadPoolCallbackRunnerUnsafe drop path for the case where scheduleOrThrowOnError fails synchronously on a thread that is already attached to another thread group, and it adds a focused regression gtest for that scenario. I did not find a correctness or lifecycle issue in the current implementation: switching with ThreadGroupSwitcher(..., /*allow_existing_group=*/ true) preserves the existing ordering guarantee that callback captures are released under the task's group before the promise is satisfied, while also restoring the caller's original group afterwards.

Missing context / blind spots
  • ⚠️ I could not run the new ThreadPoolCallbackRunner gtest locally because this checkout has no existing build directory or test binary to reuse.
  • ⚠️ The Praktika PR report for commit 9307102e084cec016dceb7fce37e3171c17f2cb2 was still empty at review time, so there was no CI evidence yet beyond source inspection.
Final Verdict

✅ No blocking findings from source review.

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

clickhouse-gh Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.50% 85.50% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.70% 77.60% -0.10%

Changed lines: Changed C/C++ lines covered: 37/37 (100.00%) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay.

@alexey-milovidov alexey-milovidov self-assigned this Jul 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 7, 2026
Merged via the queue into master with commit 5259c45 Jul 7, 2026
176 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-callback-runner-drop-attached-group branch July 7, 2026 21:23
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 7, 2026
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.

3 participants