Fix race between async insert completion and query_log entry#96958
Merged
alexey-milovidov merged 3 commits intomasterfrom Feb 16, 2026
Merged
Fix race between async insert completion and query_log entry#96958alexey-milovidov merged 3 commits intomasterfrom
alexey-milovidov merged 3 commits intomasterfrom
Conversation
In `AsynchronousInsertQueue::processData`, the `finish_entries` lambda was calling `entry->finish()` (which sets the promise and wakes up the client waiting with `wait_for_async_insert=1`) before `logQueryFinish` (which writes the `QueryFinish` entry to `query_log`). This created a race window: the client could wake up, execute `SYSTEM FLUSH LOGS`, and query `system.query_log` before the `QueryFinish` entry was even added to the log buffer. Under TSan or CPU contention, this window was wide enough to trigger reliably. Fix by reordering: write log entries first, then notify the waiting clients. This is consistent with the error path, where `logQueryException` already runs before `finishWithException`. Note: this is complementary to #96957, which addresses a similar race in the HTTP/curl path at the test level. That PR does not cover this case because test `02790` uses the native client, not curl, and the race window is in `AsynchronousInsertQueue`, not in the HTTP response path. Closes #80813 Co-Authored-By: Claude Opus 4.6 <[email protected]>
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AsynchronousInsertQueue::processDatawhereentry->finish()(which wakes up the client) was called beforelogQueryFinish(which writes theQueryFinishentry toquery_log)wait_for_async_insert=1could wake up and executeSYSTEM FLUSH LOGS+SELECT FROM system.query_logbefore theQueryFinishentry was added to the log bufferlogQueryExceptionalready runs beforefinishWithExceptionRelation to #96957: that PR addresses a similar race in the HTTP/curl path at the test level (retry loops). It does not cover this case because test
02790uses the native client (not curl), and the race window is inAsynchronousInsertQueue, not in the HTTP response path. The two fixes are complementary.Test plan
02790_async_queries_in_query_logpasses consistently (5/5 local runs)Closes #80813
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Documentation entry for user-facing changes
🤖 Generated with Claude Code