Skip to content

Fix possible crash due to use-after-free of memory reservation after DROP WORKLOAD - #123341

Merged
serxa merged 6 commits into
ClickHouse:masterfrom
azat:workload/fix-crash-after-drop-workload
Oct 16, 2026
Merged

serxa merged 6 commits into
ClickHouse:masterfrom
azat:workload/fix-crash-after-drop-workload

Conversation

@azat

@azat azat commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

When a workload is dropped while queries still use it, purge its queue, the same as when the workload gets its first child: its memory allocations and pending CPU/IO requests fail instead of using freed nodes, running without limits, or waiting forever.

Stack: https://pastila.nl/?00012c0f/dc6c7e9fa3edb4c6941793dc6df19e38#dcw7YYjsZKVM2DiSFWqnKA==GCM
CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119443&sha=a36c9db2bee90f53296812cc6fe8bce7c278ec56&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29

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 possible crash due to use-after-free of memory reservation, and a possible hang of CPU and IO requests, after DROP WORKLOAD. Queries still running in a dropped workload now fail.


Workflow [PR]
Sync PR [sync-upstream/pr/123341]

Version info

  • Merged into: 26.11.1.88-master (included in 26.11 and later)
  • Backported to: 26.10.2.81, 26.9.26.28, 26.8.35.25

@azat
azat requested a review from serxa October 1, 2026 21:18
@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e9e355b]

Summary: ✅


AI Review

Summary

PR #123341 purges a dropped workload's memory allocations and pending CPU/IO requests, rejects subsequent requests, and clears retained queue pointers before workload destruction. Reviewed head e9e355b3d59dda4494fa01e9144d99634170b028, including all prior discussion. No unresolved findings remain; the previous bot findings are addressed.

Missing context / blind spots
  • ⚠️ No local build was available, and sanitizer unit-test results for this head were not yet available. Completed ASan/TSan runs of the added scheduler regressions would close this runtime-validation gap. The new stateless regression, 05239_workload_drop_memory_reservation_in_use, passed in the Fast test report.
Final Verdict

Status: ✅ Approve. No new inline findings.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... labels Oct 1, 2026
@azat
azat force-pushed the workload/fix-crash-after-drop-workload branch from 22c9a6a to 4b7cc8e Compare October 1, 2026 22:09
…DROP WORKLOAD

Detach queues safely when a workload disappears and allow their existing
reservations to complete outside the hierarchy.

Changelog:
v2: Clear expired workload pointers to prevent use-after-free.
v3: Wake detached queues so reservation teardown cannot hang.
v4: Approve requests on detached queues rather than failing running queries.
v5: Fold regression-test resource cleanup and comment corrections.

Stack: https://pastila.nl/?00012c0f/dc6c7e9fa3edb4c6941793dc6df19e38#dcw7YYjsZKVM2DiSFWqnKA==GCM
CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119443&sha=a36c9db2bee90f53296812cc6fe8bce7c278ec56&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29
@azat
azat force-pushed the workload/fix-crash-after-drop-workload branch from 4b7cc8e to 2e0be2d Compare October 2, 2026 06:41
Older hierarchy versions can retain queues retired during a
leaf-to-parent transition after the workload itself is destroyed.

Add a regression that retains a classifier for a sibling workload across
the leaf-to-parent-to-leaf transition and `DROP WORKLOAD`, then releases
the classifier to destroy the retired queue.
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit e9e355b3d59dda4494fa01e9144d99634170b028 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

@serxa serxa self-assigned this Oct 8, 2026
while (decrease)
approveDecrease();
while (increase)
approveIncrease();

@serxa serxa Oct 8, 2026 •

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.

It is also inconsistent with the other way a queue goes away. CREATE WORKLOAD child IN A purges A's queue and fails every running allocation. DROP WORKLOAD A lets them run with no limit. The two paths should have the same contract. Can we just reuse purgeQueue or do something similar? Also, I believe that with a time-shared scheduler, we purge the queue in such situations.

But maybe purge was indeed wrong - because if we have no workload it usualy means "unlimited" 🤔 Anyway this should be consistent.

Note: Also, there is a natural way for an orphaned part of a tree to exist briefly. Imagine a CREATE OR REPLACE WORKLOAD query that changes the parent of a workload. In this case, we detach a subtree in one place and attach it to the other place. So any external events that can see a queue in such a state are perfectly valid and must support later attachment to the hierarchy. (this PR looks correct in this regard)

…ited

When `DROP WORKLOAD` runs while a query still uses the workload, the query's
classifier keeps the workload's queue alive. Purge that queue when the
workload node is destroyed, the same way the queue is purged when the
workload gets its first child: pending and running memory allocations and
pending CPU and IO requests fail, and new ones are rejected.

Previously a detached allocation queue approved every later increase without
any limit, and a time-shared queue was never dequeued again, so a CPU or IO
request made after the drop waited until the query ended.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@serxa

serxa commented Oct 15, 2026

Copy link
Copy Markdown
Member

I hope you don't mind if I add a few more related changes here and address my own review.

@serxa

serxa commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

On DROP WORKLOAD, the queue is now purged, the same as when the workload gets its first child, for both memory and CPU/IO. Running allocations and pending requests fail instead of being served outside the hierarchy without limits. The detached time-shared queue had the same problem: requests made after the drop were never dequeued, so with CPU preemption a query froze and an IO read would wait forever. Fixed now as well.

Query threads read the allocation queue's workload name under the queue
mutex when they are rejected after a purge, so the dropped workload node
must not write that pointer again without the mutex. Each queue clears it
in `purgeQueue` instead, and a test inserts allocations while the workload
is dropped so TSan checks the overlap.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…node

The queue's error messages, built on query threads under the queue mutex,
call the workload node's virtual methods. Purging from the node's destructor
let such a call overlap the start of the node's destruction. The drop now
purges the queue on the scheduler thread while the node is fully alive, and
the race test uses positive allocations rejected by `max_waiting_queries`,
which read the workload name before the purge as well as after it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@azat
azat deleted the workload/fix-crash-after-drop-workload branch October 22, 2026 18:13
thevar1able pushed a commit to actueleai/ClickHouse that referenced this pull request Oct 22, 2026
Since ClickHouse#123341, DROP WORKLOAD purges the dropped workload's queue: the
running query's memory reservation fails with MEMORY_RESERVATION_FAILED
("Allocation queue is about to be destructed") at its next sync, which
for sleepEachRow(1) with max_block_size 1 is the end of the current
1-second step. When that step ends between DROP WORKLOAD and KILL QUERY,
the query is already gone and KILL matches nothing. That was a silent
no-op until ClickHouse#125968 made kill_throw_if_noop default to true; now KILL
writes NOTHING_TO_KILL to stderr and the test fails with
"having stderror" (master 3eecd13, amd_asan_ubsan).

The failing purge is the intended behavior (covered by the
MemoryReservationDropWorkloadFailsRunningAllocation gtest), so the test
now tolerates a no-op KILL with kill_throw_if_noop = false, the idiom of
03824 and 04726, and its comment states the current semantics. Other
KILL errors still reach stderr, and the reservation is destroyed after
the purge in both branches.

Validated on the CI binaries of 3eecd13: amd_asan_ubsan unmodified
7/30 FAIL (all NOTHING_TO_KILL), fixed 50/50 OK with 17 runs in the
drop-failed branch; amd_debug with sleep 1.5 injected between DROP and
KILL: unmodified 10/10 FAIL, fixed 10/10 OK; plain fixed 50/50 OK.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 22, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #123341 to 26.10: Fix possible crash due to use-after-free of memory reservation after DROP WORKLOAD
PedroTadim added a commit that referenced this pull request Oct 29, 2026
…6.8-123341

Resolve the conflicts of #123341:
- This branch still has `TimeShared/FifoQueue.h` instead of `TimeShared/RequestQueue.h`, so the change to
  `RequestQueue::purgeQueue` goes to `FifoQueue::purgeQueue`.
- `WorkloadNode.h`: keep the branch's `FifoQueue` comment and add `queue_dropped`.
- Take master's `05239_workload_drop_memory_reservation_in_use.sh`, which includes the follow-up fix
  #127158 (`kill_throw_if_noop = false`).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PedroTadim added a commit that referenced this pull request Oct 29, 2026
…6.9-123341

Resolve the conflicts of #123341:
- This branch still has `TimeShared/FifoQueue.h` instead of `TimeShared/RequestQueue.h`, so the change to
  `RequestQueue::purgeQueue` goes to `FifoQueue::purgeQueue`.
- `WorkloadNode.h`: keep the branch's `FifoQueue` comment and add `queue_dropped`.
- Take master's `05239_workload_drop_memory_reservation_in_use.sh`, which includes the follow-up fix
  #127158 (`kill_throw_if_noop = false`).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse added a commit that referenced this pull request Oct 29, 2026
robot-clickhouse added a commit that referenced this pull request Oct 29, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 29, 2026
PedroTadim added a commit that referenced this pull request Oct 29, 2026
…on root

This branch allows a single root workload, so the test failed with "The second root is not allowed"
before checking anything. `A` and `B` now share the parent `all`, as in the other tests of the
backport of #123341.

Failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=128123&sha=10c57992af3077a27b7aaab8eadd798da82cfaad&name_0=BackportPR&name_1=Unit%20tests%20%28asan_ubsan%29

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PedroTadim added a commit that referenced this pull request Oct 29, 2026
…on root

This branch allows a single root workload, so the test failed with "The second root is not allowed"
before checking anything. `A` and `B` now share the parent `all`, as in the other tests of the
backport of #123341.

Failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=128123&sha=10c57992af3077a27b7aaab8eadd798da82cfaad&name_0=BackportPR&name_1=Unit%20tests%20%28asan_ubsan%29

Co-Authored-By: Claude Opus 5.5 <[email protected]>
clickhouse-gh Bot added a commit that referenced this pull request Oct 29, 2026
Backport #123341 to 26.8: Fix possible crash due to use-after-free of memory reservation after DROP WORKLOAD
clickhouse-gh Bot added a commit that referenced this pull request Oct 29, 2026
Backport #123341 to 26.9: Fix possible crash due to use-after-free of memory reservation after DROP WORKLOAD
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…on root

This branch allows a single root workload, so the test failed with "The second root is not allowed"
before checking anything. `A` and `B` now share the parent `all`, as in the other tests of the
backport of #123341.

Failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=128123&sha=10c57992af3077a27b7aaab8eadd798da82cfaad&name_0=BackportPR&name_1=Unit%20tests%20%28asan_ubsan%29

Co-Authored-By: Claude Opus 5.5 <[email protected]>
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…on root

This branch allows a single root workload, so the test failed with "The second root is not allowed"
before checking anything. `A` and `B` now share the parent `all`, as in the other tests of the
backport of #123341.

Failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=128123&sha=10c57992af3077a27b7aaab8eadd798da82cfaad&name_0=BackportPR&name_1=Unit%20tests%20%28asan_ubsan%29

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

comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport v26.7-must-backport v26.8-must-backport v26.9-must-backport v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants