Repository navigation
Fix possible crash due to use-after-free of memory reservation after DROP WORKLOAD - #123341
Conversation
|
Workflow [PR], commit [e9e355b] Summary: ✅
AI ReviewSummaryPR #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 Missing context / blind spots
Final VerdictStatus: ✅ Approve. No new inline findings. |
22c9a6a to
4b7cc8e
Compare
…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
4b7cc8e to
2e0be2d
Compare
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.
Build profile diff (arm_release)Commit See the job log for details. |
| while (decrease) | ||
| approveDecrease(); | ||
| while (increase) | ||
| approveIncrease(); |
There was a problem hiding this comment.
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]>
|
I hope you don't mind if I add a few more related changes here and address my own review. |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
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]>
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]>
Backport #123341 to 26.10: Fix possible crash due to use-after-free of memory reservation after DROP WORKLOAD
…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]>
…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]>
… memory reservation after DROP WORKLOAD
… memory reservation after DROP WORKLOAD
…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]>
…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]>
Backport #123341 to 26.8: Fix possible crash due to use-after-free of memory reservation after DROP WORKLOAD
Backport #123341 to 26.9: Fix possible crash due to use-after-free of memory reservation after DROP WORKLOAD
… memory reservation after DROP WORKLOAD
…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]>
… memory reservation after DROP WORKLOAD
…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]>
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):
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
26.11.1.88-master(included in26.11and later)26.10.2.81,26.9.26.28,26.8.35.25