Skip to content

Fix livelock in CancellationChecker caused by timeout overflow - #96450

Merged
serxa merged 6 commits into
masterfrom
fix-cancellation-checker-livelock
Feb 10, 2026
Merged

serxa merged 6 commits into
masterfrom
fix-cancellation-checker-livelock

Conversation

@serxa

@serxa serxa commented Feb 9, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC) or LOGICAL_ERROR

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_time values.

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 p.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
  • Do not take CancellationChecker mutex if max_execution_time is not set (on query stop)

Version info

  • Merged into: 26.2.1.514
  • Backported to: 26.1.3.32, 25.12.6.23, 25.11.9.29, 25.8.17.16, 25.3.15.6

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
@clickhouse-gh

clickhouse-gh Bot commented Feb 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c1a941f]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_asan, db disk, old analyzer, 5/6) failure
test_acme_tls/test_multi_node.py::test_coordinated_acme_authorization FAIL cidb IGNORED
test_acme_tls/test_single_node.py::test_acme_authorization FAIL cidb IGNORED
Integration tests (amd_binary, 5/5) failure
test_acme_tls/test_single_node.py::test_acme_authorization FAIL cidb IGNORED
test_acme_tls/test_multi_node.py::test_coordinated_acme_authorization FAIL cidb IGNORED
Integration tests (arm_binary, distributed plan, 1/4) failure
test_acme_tls/test_single_node.py::test_acme_authorization FAIL cidb IGNORED
test_acme_tls/test_multi_node.py::test_coordinated_acme_authorization FAIL cidb IGNORED
Integration tests (amd_tsan, 5/6) failure
test_acme_tls/test_single_node.py::test_acme_authorization FAIL cidb IGNORED
test_acme_tls/test_multi_node.py::test_coordinated_acme_authorization FAIL cidb IGNORED

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Feb 9, 2026
@scanhex12 scanhex12 self-assigned this Feb 9, 2026

@scanhex12 scanhex12 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@serxa

serxa commented Feb 9, 2026

Copy link
Copy Markdown
Member Author

Yep, this simple one should do the trick, I think.

serxa added 3 commits February 9, 2026 15:32
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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@serxa

serxa commented Feb 9, 2026

Copy link
Copy Markdown
Member Author

We'll have to wait...

Error: fatal: unable to access 'https://github.com/ClickHouse/ClickHouse/': The requested URL returned error: 500

@serxa
serxa enabled auto-merge February 10, 2026 11:09
@serxa
serxa added this pull request to the merge queue Feb 10, 2026
Merged via the queue into master with commit 47523eb Feb 10, 2026
126 of 130 checks passed
@serxa
serxa deleted the fix-cancellation-checker-livelock branch February 10, 2026 11:25
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Feb 10, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 10, 2026
robot-clickhouse added a commit that referenced this pull request Feb 10, 2026
robot-clickhouse added a commit that referenced this pull request Feb 10, 2026
robot-clickhouse added a commit that referenced this pull request Feb 10, 2026
serxa added a commit that referenced this pull request Feb 10, 2026
Backport #96450 to 25.11: Fix livelock in CancellationChecker caused by timeout overflow
robot-clickhouse added a commit that referenced this pull request Feb 10, 2026
robot-clickhouse added a commit that referenced this pull request Feb 10, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Feb 10, 2026
serxa added a commit that referenced this pull request Feb 11, 2026
Backport #96450 to 25.12: Fix livelock in CancellationChecker caused by timeout overflow
serxa added a commit that referenced this pull request Feb 11, 2026
Backport #96450 to 26.1: Fix livelock in CancellationChecker caused by timeout overflow
serxa added a commit that referenced this pull request Feb 11, 2026
Backport #96450 to 25.3: Fix livelock in CancellationChecker caused by timeout overflow
serxa added a commit that referenced this pull request Feb 11, 2026
Backport #96450 to 25.8: Fix livelock in CancellationChecker caused by timeout overflow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants