Repository navigation
Fix livelock in CancellationChecker caused by timeout overflow - #96450
Conversation
Two issues were causing CancellationChecker::workerFunction() to spin instead of waiting: 1. Overflow in wait_for: When a query was registered with an extremely large timeout (e.g., ~9.2e12 ms, close to INT64_MAX), the std::condition_variable::wait_for() would overflow when converting milliseconds to nanoseconds internally (multiplying by 1,000,000). This caused wait_for to return immediately instead of waiting. 2. Stale now_ms in predicate: The predicate lambda captured now_ms by value, but this timestamp was computed before the wait. On spurious wakeups, the predicate would compare the task's endtime against the stale timestamp, incorrectly returning false even when the task had actually expired. Combined with issue #1, this caused infinite spinning. Fix: - Cap timeout to 1 year maximum to prevent chrono overflow - Use fresh timestamp inside the predicate to correctly detect expiration
|
Workflow [PR], commit [c1a941f] Summary: ❌
|
scanhex12
left a comment
There was a problem hiding this comment.
Is it hard to create some test?
Verifies that queries with extremely large max_execution_time values (close to INT64_MAX) complete normally without causing livelock.
|
Yep, this simple one should do the trick, I think. |
When a query has no timeout (max_execution_time <= 0), it is not registered with CancellationChecker. Previously, appendDoneTasks() was still called unconditionally in ProcessListEntry destructor, which would lock the mutex and search through the set for nothing. Now appendTask() returns a bool indicating whether registration occurred. This flag is stored in ProcessListEntry and used to skip the unnecessary appendDoneTasks() call and mutex lock.
| -- The timeout is internally capped to 1 year to prevent overflow in std::condition_variable::wait_for. | ||
| -- This query should complete quickly without hanging, regardless of the huge timeout value. | ||
|
|
||
| SET max_execution_time = 9223372041; -- Close to INT64_MAX / 1000000000, would overflow when converted to nanoseconds |
There was a problem hiding this comment.
I think this magic constant could be very interesting to add to the randomization of settings. Especially for settings that express time in seconds. As you can see, there is no mention of nanoseconds in the code; nevertheless, internally in std::condition_variable::wait_for(), there is a conversion due to the futex syscall. This caused confusion that "overflow can never happen here" in LLMs' and developers' minds, making this bug very hard to investigate.
I dont know where is best place for such a magic constant @PedroTadim @fm4v (I'm not sure who to ping, hope you guys point me)
|
We'll have to wait... |
Backport #96450 to 25.11: Fix livelock in CancellationChecker caused by timeout overflow
Backport #96450 to 25.12: Fix livelock in CancellationChecker caused by timeout overflow
Backport #96450 to 26.1: Fix livelock in CancellationChecker caused by timeout overflow
Backport #96450 to 25.3: Fix livelock in CancellationChecker caused by timeout overflow
Backport #96450 to 25.8: Fix livelock in CancellationChecker caused by timeout overflow
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes livelock in the cancellation checker thread for queries with high
max_execution_timevalues.Two issues were causing CancellationChecker::workerFunction() to spin instead of waiting:
Overflow in wait_for: When a query was registered with an extremely large timeout (e.g., ~9.2e12 ms, close to INT64_MAX), the std::condition_variable::wait_for() would overflow when converting milliseconds to nanoseconds internally (multiplying by 1,000,000). This caused wait_for to return immediately instead of waiting.
Stale now_ms in predicate: The predicate lambda captured now_ms by value, but this timestamp was computed before the wait. On spurious wakeups, the predicate would compare the task's endtime against the stale timestamp, incorrectly returning false even when the task had actually expired. Combined with issue p.1, this caused infinite spinning.
Fix:
max_execution_timeis not set (on query stop)Version info
26.2.1.51426.1.3.32,25.12.6.23,25.11.9.29,25.8.17.16,25.3.15.6