Skip to content

Harden ThreadStatus::attachToGroupImpl against partial attachment failures - #106462

Merged
Felixoid merged 8 commits into
masterfrom
fix-desctructor-cleanup
Jun 22, 2026
Merged

Felixoid merged 8 commits into
masterfrom
fix-desctructor-cleanup

Conversation

@Felixoid

@Felixoid Felixoid commented Jun 4, 2026 •

Copy link
Copy Markdown
Member

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

Fix a case where a worker thread could stay attached to a stale thread group after CurrentThread::attachToGroup failed part way through, causing later tasks on the same thread to fail with Thread is already attached to a group.

@Felixoid
Felixoid marked this pull request as draft June 4, 2026 12:15
@clickhouse-gh

clickhouse-gh Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [af50059]

Summary: ✅


AI Review

Summary

This PR hardens ThreadStatus::attachToGroupImpl and ThreadGroupSwitcher so failed group attachment rolls back the partially attached state and restores the caller's previous group for allow_existing_group paths. The current code covers the previously open bot findings: the regression test now runs in a dedicated thread, and the constructor catch detaches the attempted group before restoring the previous one. I found no remaining correctness issues in the current head.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 4, 2026
@Felixoid Felixoid changed the title Regression test for the thread switch logical error Fix thread switching logical error Jun 4, 2026
@Felixoid
Felixoid force-pushed the fix-desctructor-cleanup branch from 4091c1a to ce16c10 Compare June 4, 2026 13:01
@watson-ch

watson-ch Bot commented Jun 4, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/1682

Labels added:

  • v26.4-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@Felixoid
Felixoid force-pushed the fix-desctructor-cleanup branch 2 times, most recently from 3622132 to d22c401 Compare June 4, 2026 15:24
@Felixoid
Felixoid marked this pull request as ready for review June 4, 2026 15:24
Comment thread src/Common/ThreadGroupSwitcher.cpp Resolved
Comment thread src/Common/ThreadGroupSwitcher.cpp Resolved
@Felixoid
Felixoid force-pushed the fix-desctructor-cleanup branch from 45a39b1 to ded2658 Compare June 4, 2026 16:13
Comment thread src/Interpreters/ThreadStatusExt.cpp Resolved
@Felixoid
Felixoid force-pushed the fix-desctructor-cleanup branch from cd5bb7e to d188200 Compare June 4, 2026 16:35
Comment thread src/Interpreters/ThreadStatusExt.cpp Resolved
@Felixoid
Felixoid force-pushed the fix-desctructor-cleanup branch from d188200 to 7d52561 Compare June 4, 2026 17:00
Comment thread src/Interpreters/ThreadStatusExt.cpp Resolved
Comment thread src/Common/ThreadGroupSwitcher.cpp Resolved
@mstetsyuk mstetsyuk self-assigned this Jun 5, 2026
Comment thread src/Interpreters/ThreadStatusExt.cpp Resolved
Comment thread src/Common/ThreadGroupSwitcher.cpp Resolved
@watson-ch

watson-ch Bot commented Jun 8, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/1682

Labels added:

  • v25.12-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@Felixoid
Felixoid force-pushed the fix-desctructor-cleanup branch from 65e2736 to 86f0d55 Compare June 10, 2026 17:31
@Felixoid Felixoid changed the title Fix thread switching logical error Harden ThreadStatus::attachToGroupImpl against partial attachment failures Jun 10, 2026

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

Can we simplify the tests? Ideally, consolidate them into a single test and avoid covering very specific invariants.

Comment thread src/Common/ThreadGroupSwitcher.cpp Resolved
Comment thread src/Interpreters/ThreadStatusExt.cpp Resolved
@Felixoid
Felixoid force-pushed the fix-desctructor-cleanup branch from c3bb702 to f842fa3 Compare June 16, 2026 07:45
@Felixoid
Felixoid requested a review from mstetsyuk June 16, 2026 07:45
Comment thread src/Interpreters/ThreadStatusExt.cpp Resolved
Comment thread src/Common/ThreadGroupSwitcher.cpp Resolved
@Felixoid
Felixoid force-pushed the fix-desctructor-cleanup branch from 7165005 to e1e8c86 Compare June 18, 2026 11:47
@Felixoid Felixoid changed the title Harden ThreadStatus::attachToGroupImpl against partial attachment failures Harden ThreadStatus::attachToGroupImpl against partial attachment failures Jun 18, 2026
Comment thread src/Common/ThreadGroupSwitcher.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 18, 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.50% 77.50% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 70/71 (98.59%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@Felixoid
Felixoid requested a review from mstetsyuk June 19, 2026 12:50
@Felixoid

Copy link
Copy Markdown
Member Author

I hope, it's in a decent state now.

@Felixoid
Felixoid added this pull request to the merge queue Jun 22, 2026
Merged via the queue into master with commit 27f3ff5 Jun 22, 2026
166 checks passed
@Felixoid
Felixoid deleted the fix-desctructor-cleanup branch June 22, 2026 11:15
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 22, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Felixoid @mstetsyuk — a couple of suggestions to make this PR easier for reviewers and the changelog reader.

Description (currently missing):

  • Why: The body is the unmodified PR template (only the link placeholders and changelog category). It does not explain what partial-attachment failure looks like, when it triggers (a throw in applyGlobalSettings/applyQuerySettings/setThreadName), or why leaving the thread half-attached is harmful (later LOGICAL_ERROR in ~ThreadGroupSwitcher nesting checks).

  • Suggested addition:

    Explain: when attachToGroupImpl threw after linkThread/parent setup (e.g. settings application failed), the thread was left linked to the group with partially-set parents and no rollback; a subsequent ThreadGroupSwitcher could then hit the nesting-invariant LOGICAL_ERROR in its destructor. The fix wraps the post-link work in try/catch and calls detachFromGroup() on failure, and adds symmetric recovery in ThreadGroupSwitcher's catch (detach the target group, restore the previous group). Two ONCE fail points plus a gtest exercise both the in-attach failure and the post-attach failure from detached and attached starting states.

Ignore this if the current title/body is intentional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants