Skip to content

Fix lost wake-up of a RabbitMQ consumer waiting for messages - #130854

Open
comino wants to merge 1 commit into
ClickHouse:masterfrom
comino:rabbitmq-lost-wakeup
Open

comino wants to merge 1 commit into
ClickHouse:masterfrom
comino:rabbitmq-lost-wakeup

Conversation

@comino

@comino comino commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

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

Fixes a rare race in the RabbitMQ table engine: the consuming thread could miss the notification about a new message or about the table being stopped, and wait until the next message or the end of the flush interval. This could occasionally delay a block, DROP TABLE and server shutdown by up to one flush interval.

Description

A minor fix, found while working on #130767 (RabbitMQ event loop on its own thread) and split out so it can be reviewed on its own. It does not block #130767.

Bug. RabbitMQConsumer::waitForMessages checks the queue and starts waiting on cv while holding mutex. The delivery callback and stop() notified cv without holding mutex, so a notify could land between the reader's check and its wait and be lost. The reader then sleeps until the next message or its timeout: the flush interval, or 10 minutes (SANITY_TIMEOUT) with rabbitmq_flush_interval_ms = 0. The code is the same in all supported releases. The window is a few instructions wide, so a lost wake-up is rare.

Impact. A missed wake-up delays the reader until the next message arrives, at most until the flush interval ends: 7.5 s with the default stream_flush_interval_ms, 10 minutes with rabbitmq_flush_interval_ms = 0. DROP TABLE and server shutdown wait the same way.

sequenceDiagram
    participant R as Reader (waitForMessages)
    participant D as Delivery callback / stop()
    R->>R: lock mutex (h:81), queue is empty (h:85)
    D->>D: push message (cpp:131)
    D-->>R: notify without mutex (cpp:135, stop: cpp:41): lost, R is not waiting yet
    R->>R: wait on condition variable cv (h:84)
    Note over R: sleeps until the next message<br/>or the flush interval ends (7.5 s by default)
Loading

Lines in RabbitMQConsumer on master: h:81-85, cpp:131-135, cpp:38-41.

Fix. Notify under mutex in both places, so a notify can only happen before the reader's check or after its wait has started:

sequenceDiagram
    participant R as Reader (waitForMessages)
    participant D as Delivery callback / stop()
    R->>R: lock mutex (h:83), queue is empty (h:87)
    D->>D: push message (cpp:47), then lock mutex (cpp:51): blocks
    R->>R: wait on condition variable cv, releases mutex (h:86)
    D-->>R: notify holding mutex (cpp:52, stop: cpp:66-67)
    Note over R: wakes up at once
Loading

Lines in RabbitMQConsumer in this PR: h:83-87, cpp:45-52, cpp:38-41, cpp:64-67.

If the delivery takes the mutex first, the reader's check already sees the message and does not wait. stop() now calls the existing wakeUp(), which does exactly that. The delivery path moves into a small pushMessage helper so that a test can call it. The callback holds channel_mutex while it notifies, so mutex is now taken under channel_mutex; mutex is a leaf lock (only the queue check and atomics run under it), so this adds no lock-order risk.

Tests. Two gtests race a helper thread against waitForMessages for 500000 rounds: DeliveryWakesWaitingReader and StopWakesWaitingReader. TSan build:

  • Reverting the delivery lock fails DeliveryWakesWaitingReader in 6 of 6 runs (first lost wake-up at round 1 to 7043); reverting the stop() lock fails StopWakesWaitingReader in 6 of 6 runs (round 3 to 638). With 50000 rounds one of 12 such runs missed it, hence the larger count.
  • With the fix both pass 10 runs in a row with no TSan report.
  • CI's bugfix validation cannot judge this PR: it runs the new test on master, where pushMessage does not exist. A broker-based integration test cannot hit a window this narrow.

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

@clickhouse-gh clickhouse-gh Bot added comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). external Author is not a member of the ClickHouse organization labels Nov 11, 2026
@comino
comino force-pushed the rabbitmq-lost-wakeup branch from 4ffbbde to 87dfbb1 Compare November 12, 2026 16:32
waitForMessages checks the received queue and starts waiting while holding
the consumer mutex, but deliveries notified the condition variable without
it. A notify that fell between the check and the wait was lost, so the
reader slept until the next delivery or the end of the flush interval. This
delays a full block, a direct SELECT, DROP TABLE and shutdown. Notify under
the mutex, also in stop().

Without either lock its unit test fails in nearly every run.
@comino
comino force-pushed the rabbitmq-lost-wakeup branch from 87dfbb1 to c34bb0e Compare November 12, 2026 23:55
@comino
comino marked this pull request as ready for review November 13, 2026 00:04
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c34bb0e]

Summary: ✅

job_name test_name status info comment
Stateless tests (amd_tsan, s3 storage, targeted) SKIPPED CIDB timeout in test selection, rerun to test

AI Review

Summary

The PR prevents lost delivery and stop notifications in RabbitMQConsumer by synchronizing them with waitForMessages. The queue and channel locking remain consistent, and focused regression tests cover both notification paths. No actionable findings at commit c34bb0e102f8ef6109631ddd60fc87a501e61ef1.

Missing context / blind spots
  • ⚠️ No configured build or test binary is available locally, and CI unit-test results are pending. The tests were reviewed but not executed during this review; execution evidence is limited to the TSan results reported in the PR description. Completed CI unit-test results would close this validation gap.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 15, 2026

This branch has not been deployed

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

Labels

can be tested Allows running workflows for external contributors comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants