Repository navigation
Await the consumer channel close before deleting RabbitMQ queues on DROP TABLE - #116050
Conversation
…ROP TABLE StorageRabbitMQ::shutdown() sent channel.close on every consumer channel and then immediately issued queue.delete with AMQP::ifunused on a separate, freshly created channel. AMQP orders method completion per channel only, and RabbitMQ dispatches each channel to its own process, so the broker could evaluate the deletion while the consumer was still registered on the other channel. A classic queue then answers what the flag asks for, PRECONDITION_FAILED - queue '...' in use, which was logged before giving up. The queues are declared durable and deliberately not autodelete, so they leaked permanently together with any unconsumed messages. Losing the race depends on broker scheduling, so it showed up rarely and mostly on slow configurations. RabbitMQProducer::finish already awaited its own channel close while the consumer discarded the Deferred that close() returns, and that asymmetry was the bug. The consumer now observes its close, and shutdown() waits for those answers on a dropped table before asking for the deletion. rabbit_channel deregisters a channel's consumers while handling its channel.close and before answering it, through a synchronous notify_down_all, so that answer is the ordering the deletion needs. The deletion itself is unchanged, still a single ifunused attempt, so a queue shared with another table through rabbitmq_queue_base is still refused and still survives. The bound is small rather than the 30s used elsewhere in this file, because a channel whose last method awaits a reply that never arrives queues its own close, and such a table would otherwise pay the full timeout on every DROP TABLE; expiring restores the previous behaviour of not waiting. startBlockingLoopWithTimeout also refreshes the event loop's cached time before arming its timer. uv_timer_start derives the deadline from that cached value, which libuv only updates while the loop runs, so between two blocking waits it could be far enough in the past that the timer was already overdue and the wait returned at once. That silently shortened every bounded wait here; it went unnoticed with a 30s bound and made a 1s one useless. Two limitations are out of scope. Quorum and stream queues still leak, because the broker does not implement if-unused for them at all, so no client-side barrier can help. A table that has already nacked a message also still leaks, which is an AMQP-CPP defect tracked separately.
The regression test asserted that the consumer channel close is logged before the queue deletion, but that ordering can also arise from the order two replies happen to be drained in, so it did not pin the wait that establishes it. It now also requires the wait's own bounded loop to have started before the deletion, matched on a four-digit bound so the unrelated 30000ms waits cannot satisfy it. Removing only the wait's use of the timed loop, while leaving the wait itself in place so every other assertion still passes, trips the new assertion on 5 of 5 runs; the test is green on 10 of 10. Refreshing the event loop's cached time before arming the timer had no arm at all, yet dropping that line makes every bounded wait expire on its first iteration. Both existing tests in gtest_rabbitmq_handler.cpp arm the timer on a freshly created loop, so neither can observe a stale cache. The new one idles the loop for two seconds first and asserts a lower bound on how long a 500ms wait actually took: 400ms with the refresh, 0ms without it, on 5 of 5 runs. The wait is measured rather than stopped by a second timer, because such a timer is armed against the same stale clock and is therefore overdue as well: the correct build then returns immediately and the assertion fails on it. Only a lower bound is asserted, so a loaded runner overshooting the timeout cannot make the test fail. Co-Authored-By: Claude Opus 5 <[email protected]>
StorageRabbitMQ::shutdown() discarded its is_drop argument and gated the queue deletion only on the drop_table flag. checkTableCanBeDropped() sets that flag before the statement it guards can fail, and nothing ever clears it, so a table left fully alive by a failed TRUNCATE stayed marked: a later DETACH TABLE, or a server shutdown, then deleted its durable queues along with every unconsumed message in them. TRUNCATE reaches this state with no settings and no race. RabbitMQ does not implement truncate, so InterpreterDropQuery calls checkTableCanBeDropped() and only then hits the IStorage default, which throws NOT_IMPLEMENTED. A failing dependency check on DROP latches the flag the same way. Conjoining the two conditions is a strict narrowing. Replacing drop_table with is_drop would not be: TemporaryTableHolder's destructor and DatabaseReplicated's recovery drop both pass is_drop = true without ever latching the flag, so that form would add deletions on two paths that do not have them today. Before this change the deletion on that path raced the still-registered consumer and was usually refused, which is the defect the surrounding commit fixes, so the two have to land together.
Internal second-model review (click to expand)Reviewed cold against the resulting tree over four rounds, then adjudicated against the recorded
Also verified independently, from the pinned broker's own sources rather than from the plan: the Stability, superseding the count in the description's validation block: all three tests, the two Session id: cron:clickhouse-review-slot-8:20260824-045600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260824-022400 |
|
Workflow [PR], commit [43c1503] Summary: ✅
AI ReviewSummaryThis PR fixes the RabbitMQ shutdown ordering around Final VerdictStatus: ✅ Approve LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 53/62 (85.48%) · Uncovered code |
|
cc @kssenii @antaljanosbenjamin, could you review this? DROP TABLE sent channel.close on the consumer channels and immediately issued queue.delete with ifunused on a separate channel, and AMQP orders method completion per channel only, so the broker could still see a registered consumer and refuse the deletion, leaking the durable queue; this waits for the broker to answer those closes first, on a dropped table only, and also requires shutdown its own drop argument so a failed TRUNCATE latching the internal drop flag no longer costs a later DETACH its queues. |
|
cc @pamarcos nice for you when you return! |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units12 translation units recompiled, 30 s compile time in total, 12 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.94 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
720a9a0
Related: #114269
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a broker-side queue leak when dropping a
RabbitMQtable.DROP TABLEasked the broker to delete the table's queues without waiting for its own consumer channels to close, so the broker could still see a registered consumer and refuse the deletion withPRECONDITION_FAILED. The queues are declared durable and are never auto-deleted, so they survived permanently together with any unconsumed messages.Description
StorageRabbitMQ::shutdown()sentchannel.closeon every consumer channel and then immediately issuedqueue.deletewithAMQP::ifunusedon a separate, freshly created channel. AMQP orders method completion per channel only, so the broker could evaluate the deletion while the consumer was still registered on the other channel. A classic queue then answers exactly what the flag asks for,PRECONDITION_FAILED - queue '...' in use, which was logged before giving up. Losing that race depends on broker scheduling, so it is rare.shutdown()now waits, on a dropped table only, for the broker to answer those closes before asking for the deletion.rabbit_channelderegisters a channel's consumers while handling itschannel.close, before answering it, so that answer is the ordering the deletion needs. The deletion is unchanged: still a singleifunusedattempt, so a queue shared throughrabbitmq_queue_basestill survives.The cleanup now also requires
shutdown()'s own drop argument, so a failedTRUNCATElatching the internal drop flag no longer costs a laterDETACHits queues.The bound is small rather than the 30s used elsewhere: a channel awaiting a reply that never arrives queues its own close, so such a table would otherwise pay the full timeout on every
DROP TABLE. Expiring restores the previous behaviour.startBlockingLoopWithTimeoutalso refreshes the loop's cached time before arming its timer:uv_timer_startderives the deadline from that value, which libuv only updates while the loop runs, so between two waits the timer can already be overdue and the wait returns at once. The four pre-existing 30s waits could end early the same way.Two limitations are out of scope. Quorum and stream queues still leak, because the broker does not implement
if-unusedfor them at all, so no client-side barrier helps. A table that has already nacked also still leaks, tracked separately as an AMQP-CPP defect.Validation (deterministic forcing, base vs fixed binary)
The losing interleaving was forced deterministically by suspending the consumer channel's broker-side process until its
channel.closearrived, then releasing it after 300ms.rabbitmq_queue_baseDETACH TABLE, added waitsrabbitmq_num_consumers = 2test_storage_rabbitmq/was run on both binaries: 58 passed on the fixed one, and the twofailures (
test_rabbitmq_select[1],test_rabbitmq_broker_log_is_collected) fail identically onthe base binary and are unrelated to this change. The new test fails on the base binary and passes
on the fixed one; it plus
test_rabbitmq_drop_table_properlyran 50 times each, 100/100 passed.Workflow [PR]
Sync PR [sync-upstream/pr/116050]
Version info
26.10.1.258-master(included in26.10and later)