Skip to content

Cherry pick #96450 to 25.3: Fix livelock in CancellationChecker caused by timeout overflow - #96527

Merged
robot-ch-test-poll4 merged 12 commits into
backport/25.3/96450from
cherrypick/25.3/96450
Feb 10, 2026
Merged

robot-ch-test-poll4 merged 12 commits into
backport/25.3/96450from
cherrypick/25.3/96450

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #96450

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

serxa and others added 7 commits February 9, 2026 14:15
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
Verifies that queries with extremely large max_execution_time values
(close to INT64_MAX) complete normally without causing livelock.
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.
…velock

Fix livelock in CancellationChecker caused by timeout overflow
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Feb 10, 2026
@serxa
serxa marked this pull request as draft February 10, 2026 16:11
@serxa
serxa requested a review from Copilot February 10, 2026 16:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Backport of a fix intended to prevent livelock/hangs in CancellationChecker when extremely large timeouts overflow chrono conversions.

Changes:

  • Add a stateless test covering extremely large max_execution_time values.
  • Cap tracked query timeout to 1 year (ms) to avoid wait_for overflow.
  • Adjust workerFunction() wait predicate to use a fresh timestamp on re-evaluation.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/queries/0_stateless/03903_cancellation_checker_large_timeout.sql Adds coverage to ensure huge timeouts don’t hang the server.
tests/queries/0_stateless/03903_cancellation_checker_large_timeout.reference Expected output for the new stateless test.
src/Interpreters/CancellationChecker.h Minor comment punctuation change.
src/Interpreters/CancellationChecker.cpp Implements timeout capping and updates waiting logic to avoid overflow/spin.

@serxa
serxa marked this pull request as ready for review February 10, 2026 16:29
@robot-ch-test-poll4
robot-ch-test-poll4 merged this pull request into backport/25.3/96450 Feb 10, 2026
@robot-ch-test-poll4
robot-ch-test-poll4 deleted the cherrypick/25.3/96450 branch February 10, 2026 16:35
@clickhouse-gh clickhouse-gh Bot added the ready-for-backport PR is eligible for backporting (merged 7+ days ago, not reverted) label Mar 27, 2026
@maxknv maxknv removed the ready-for-backport PR is eligible for backporting (merged 7+ days ago, not reverted) label Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants