Repository navigation
Make the cleanup DROP of a failed CREATE ... AS SELECT killable - #113504
PedroTadim merged 1 commit into
Conversation
A plain `CREATE TABLE ... AS SELECT` on an Atomic database is executed by populating a temporary table and publishing it with a RENAME. If the populate fails, an internal cleanup DROP removes the temporary table. That DROP runs on a context built from the global context, which copies the caller's settings but not the caller's process list element. With `database_atomic_wait_for_drop_and_detach_synchronously` enabled the copied settings make the DROP synchronous, so it waits in `DatabaseCatalog::waitTableFinallyDropped`. The only exit from that loop is the `throw_if_cancelled` callback, which `InterpreterDropQuery::waitForTableToBeActuallyDroppedOrDetached` derives from `getProcessListElementSafe()`. With no element the callback is a no-op, so the wait, and therefore the whole CREATE, ignores both KILL QUERY and `max_execution_time` for as long as the background drop queue keeps the table. It showed up as a Stress test (arm_release) hung check: an internal query sat in that wait for 655 s with `is_cancelled = 1` and `max_execution_time = 10`, so cancellation had fired and had no effect. The sibling factory used by CREATE OR REPLACE copies the caller's context and therefore already carries the element, which is why only the plain-create path was affected. Pass the element through so the wait the internal context can already enter is also a wait it can leave. `getProcessListElementSafe` is used rather than `getProcessListElement` because this factory also runs on the failure path, where throwing on an expired weak pointer would replace the real error; returning an empty pointer instead degrades to today's behaviour for a genuinely internal operation that has no user query. The regression test enters the wait deterministically by parking the background drop thread at the existing `database_catalog_drop_finally_before_id_erase` failpoint, which pauses after `dropTableFinally()` and before the UUID is erased from `tables_marked_dropped_ids`, so the waiter's predicate still holds and it cannot leave the loop on its own. Before this change the query stays in the wait and only releasing the failpoint ends it; after it, the query returns 1.3 s after the KILL with the failpoint still parked. The cleanup DROP's exception is logged and swallowed while the populate's own error is rethrown, so the client sees the same error on both sides and the test asserts the cancellation from `system.text_log` instead of from the client's exit code. The same factory's other consumer, the lost-race DROP taken when a concurrent query creates the target name during the populate, is left unchanged here: it passes its context as a temporary while `InterpreterDropQuery` keeps only a weak pointer, so it aborts with `LOGICAL_ERROR: Context has expired` before reaching any wait. That reproduces on unmodified master with no failpoint and needs its own fix.
Internal second-model review (1 dropped self-finding, 1 noted nit, 0 blockers)Two independent reviews ran against this diff before it was opened: a cold code review that did not
Checks that came back clean, each re-derived from source rather than taken from the implementation Freshly re-checked before opening: no competing pull request. Seventeen open candidates were found by |
Pre-PR validation gate (a-i)
Mutation arm: reverting only the added line and rebuilding reproduced the pristine base build id exactly, and the base arm above measured that binary as FAIL. Re-applying returned the build id exactly to the shipped value, so the shipped tree is byte-for-byte the measured tree. Neighbours: 61 tests matching the drop-queue / |
|
cc @evillique @davenger, could you review this? The internal context used to clean up a failed |
|
Workflow [PR], commit [98170b2] Summary: ✅
AI ReviewSummaryThis PR threads the caller's Findings
Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code |
| internal_context->makeQueryContext(); | ||
| internal_context->setSettings(current_context->getSettingsRef()); | ||
| /// The settings copied above can make the internal DROPs below wait; the element is the only way out. | ||
| internal_context->setProcessListElement(current_context->getProcessListElementSafe()); |
There was a problem hiding this comment.
I think this only fixes one instance of the invariant you rely on here. make_internal_context now carries the caller's QueryStatus, but InterpreterDropQuery::executeDropQuery still builds a full-access drop_context from global_context, copies the caller's settings, and never calls setProcessListElement (src/Interpreters/InterpreterDropQuery.cpp:890). ALTER WINDOW VIEW reaches that helper with sync = true (src/Storages/WindowView/StorageWindowView.cpp:500), so if the background drop queue is blocked its inner-table DROP still waits in waitTableFinallyDropped with no way for KILL QUERY or max_execution_time to break it. Please thread current_context->getProcessListElementSafe() through that helper as well, or centralize these internal full-access DROP contexts so the cancellation behavior stays consistent.
There was a problem hiding this comment.
Both citations check out against current master, and the mechanism is real: executeDropQuery builds drop_context from global_context at InterpreterDropQuery.cpp:890, that file has zero setProcessListElement calls, and drop_query->sync = sync at :883 makes the inner DROP wait regardless of ignore_sync_setting (which clears the setting, not the already-set flag).
The carrier is wider than ALTER WINDOW VIEW. A plain DROP TABLE <mv> SYNC reaches the same wait with no experimental settings, and so does CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE when the populate throws, which recurses out of the function this PR changes:
waitTableFinallyDropped <- waitForTableToBeActuallyDroppedOrDetached <- executeToTable
<- executeDropQuery <-- element lost here
<- StorageMaterializedView::dropInnerTableIfAny <- DatabaseAtomic::dropTable
<- executeToTableImpl <- executeToTable
<- InterpreterCreateQuery::doCreateOrReplaceTable
So I implemented your suggestion (a), built it, and it worked as intended: both shapes became killable, and a mutant carrying only this PR's existing line reddened exactly the new assertion. Measured on the pristine base and on this PR's head both shapes hang identically, so the defect is pre-existing rather than introduced here.
I then reverted it, because propagating the element there converts the hang into a corrupt catalog. DatabaseAtomic::dropTable:199-204 calls dropInnerTableIfAny before dropTableImpl, so the new cancellation throw aborts the parent's own removal. With the line, DROP TABLE mv SYNC returns after the KILL but leaves the view attached with its inner table already gone, so SELECT FROM mv gives Code: 60 and the view is only recoverable by dropping it again; the CREATE OR REPLACE case leaves a visible _tmp_replace_* materialized view behind. Without the line the parent is removed correctly. Deterministic on repeated runs.
The reasoning that a cancellation check can only add a way to stop holds for the wait but not for its caller: the throw unwinds a caller that was relying on the wait to finish. Fixing this site therefore needs the ordering in DatabaseAtomic::dropTable repaired first, across the four storages that own inner tables. That is your option (b), and it is a wider change than this one-line context propagation, so it belongs in its own PR rather than here.
Nothing in this PR's description is affected: its changelog entry claims only the plain CREATE ... AS SELECT path, which is what it fixes.
CI finish ledger - 98170b2Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
No other check failed on this head: 174 check-runs, 0 incomplete, 17 skipped by Session id: cron:our-pr-ci-monitor:20260805-210000 [Corrected 2026-08-05: the owner task already existed and is in progress, so the wording is "moved to pending", not "created". Same owner, no change of disposition.] |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a
CREATE TABLE ... AS SELECTwhose populatingSELECTfailed becoming unkillable whendatabase_atomic_wait_for_drop_and_detach_synchronouslyis enabled: the internal cleanupDROPof the temporary table waited for the background drop queue and ignored bothKILL QUERYandmax_execution_time.Description
A plain
CREATE TABLE ... AS SELECTon an Atomic database populates a temporary table and publishes it with aRENAME; if the populate fails, an internalDROPcleans it up. ThatDROPruns on a context built from the global context, which copies the caller's settings but not its process list element.With
database_atomic_wait_for_drop_and_detach_synchronously = 1the copied settings make thatDROPsynchronous, so it waits inDatabaseCatalog::waitTableFinallyDropped. The only way out is thethrow_if_cancelledcallback, whichInterpreterDropQuery::waitForTableToBeActuallyDroppedOrDetachedderives from the context's process list element. Without one it is a no-op, so the wait -- and the wholeCREATE-- ignoresKILL QUERYandmax_execution_timefor as long as the drop queue is busy.Observed as a
Stress test (arm_release)hung check: an internal query sat in that wait for 655 s withis_cancelled = 1andmax_execution_time = 10. The sibling factory used byCREATE OR REPLACEcopies the caller's context, so its cleanupDROPwas already killable; this restores that property for the plain-create path.Validated with a deterministic fixture: a failpoint parks the background drop thread where the waiter's predicate still holds, so the wait is always entered. Before the change the query stays in the wait and only releasing the failpoint ends it; after it, it returns 1.3 s after the
KILL. Reverting only the added line reddens the new test. 50/50 randomized runs pass; 61 neighbouring drop-queue tests show no regression.The factory's other caller (
CREATE TABLE IF NOT EXISTS ... AS SELECTlosing a race for the target name) has a separate pre-existing bug, left for its own change: it passes the context as a temporary whileInterpreterDropQuerykeeps only a weak pointer, so the server aborts withLOGICAL_ERROR: Context has expired. That reproduces on unmodified master with no failpoint.