Skip to content

fix: do not derive max_execution_time from the context deadline - #2101

Merged
chernser merged 3 commits into
ClickHouse:mainfrom
tushar-signoz:tvats-max-execution-time-deadline
Nov 20, 2026
Merged

chernser merged 3 commits into
ClickHouse:mainfrom
tushar-signoz:tvats-max-execution-time-deadline

Conversation

@tushar-signoz

@tushar-signoz tushar-signoz commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #2100.

When the context has a deadline, the driver set max_execution_time to the deadline + 5s. This replaced a max_execution_time set on the context, the connection, the DSN or the server profile. So the server could run a query longer than the limit the user set.

The driver now does not derive max_execution_time from the context deadline. The deadline only limits how long the client waits. The server uses the max_execution_time the user or the DB admin sets.

Behaviour change:

  • Over HTTP, a query can keep running on the server after the context deadline, unless max_execution_time or cancel_http_readonly_queries_on_client_close is set. Over Native, the driver still sends a cancel when the context ends.
  • A connection-level max_execution_time (for example 60, as in the README) now applies also when the context deadline is longer. Before, the deadline + 5s replaced it.

Checklist

  • Unit and integration tests covering the common scenarios were added
    • TestQueryOptionsMaxExecutionTime: a deadline does not add max_execution_time, and does not change a value that is set (also 0).
    • tests/issues/issue_2100_test.go: Native and HTTP. With a 1 minute deadline, the server gets the same max_execution_time as without a deadline: not set, set on the context, and set on the connection.
  • docs/config-reference.mdx: the deadline note now says that the deadline only limits the client.

🤖 Generated with Claude Code


Note

Medium Risk
Behavior change: queries may continue on the server after the client context expires unless max_execution_time is set (HTTP in particular); callers who relied on deadline-derived server limits need to set max_execution_time explicitly.

Overview
Fixes #2100 by removing logic in queryOptions that auto-set ClickHouse max_execution_time from a context deadline (remaining seconds + 5), which could override limits from DSN, connection Settings, or per-query WithSettings.

Context deadlines now only cap client wait time; server query duration is controlled only by explicit max_execution_time (or other server settings). Docs in config-reference.mdx are updated to match, including clarifying that max_execution_time=0 means the server does not auto-stop the query.

Adds unit tests (TestQueryOptionsMaxExecutionTime) and integration tests (issue_2100_test.go for Native and HTTP) to ensure a 1-minute context deadline does not change the max_execution_time the server sees.

Reviewed by Cursor Bugbot for commit 8721a42. Bugbot is set up for automated code reviews on this repo. Configure here.

Fixes ClickHouse#2100

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CLAassistant

CLAassistant commented Nov 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Comment thread context.go Resolved
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

🤖 Claude review

Stops queryOptions from replacing a caller-set max_execution_time with deadline + 5s when the caller value is lower. The core change is correct and covered on both protocols for settings passed through clickhouse.Context(WithSettings(...)), but the fix does not reach the connection-level setting, and the config reference still documents the old behavior.

What is sound:

  • maxExecutionTime uses the same CustomSetting unwrap and fmt.Sprint formatting that the native encoder (lib/proto/query.go) and the HTTP request builder use, so the parsed value is the value the server sees.
  • Test2100 runs on Native and HTTP, and both error paths render code: 159 (proto.Exception.Error and HTTPError wrapping it), so the assertion is valid on both.
  • The std surface goes through the same exec/query methods, so it inherits the fix for context-level settings.

Key concerns:

  • max_execution_time set in Options.Settings (the form shown in README.md, examples/clickhouse_api/connect_settings.go, and every database/sql DSN such as ?max_execution_time=60) is still overridden. queryOptions only inspects the context settings; the native connect.settings() appends query settings after connection settings and HTTP createRequest calls query.Set, so the deadline value wins. See inline on context.go.
  • docs/config-reference.mdx line 316 says the deadline value "overrides any manually set value", which is now false for the context-level case.

Blind spots: could not run the integration test against a live server. On HTTP, exec discards the body after a 200, so the test relies on the server buffering the small Native result and returning a 500 with the exception; CI will confirm this on the matrix.

Verdict: ⚠️ Request changes

General findings

  • ⚠️ Should fix — config reference documents the old override behavior
    docs/config-reference.mdx line 316 states: "If the context has a deadline > 1s, max_execution_time is automatically set to seconds_remaining + 5. This overrides any manually set value." After this PR that sentence is false for a lower, non-zero value set with WithSettings. Update it to describe the new rule: a caller value that is a number, greater than 0 and not above seconds_remaining + 5 is kept; otherwise the deadline value is used. The Settings row on line 281 and the WithSettings row on line 324 both point readers at max_execution_time, so this is the paragraph they land on.

Inline comments are attached to the relevant lines. This summary updates in place on re-review.

Comment thread context_test.go Resolved
The context deadline only limits how long the client waits. The driver
no longer sets max_execution_time from it, so a value set on the
context, the connection, the DSN or the server profile reaches the
server unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tushar-signoz
tushar-signoz requested review from a team and polyglotAI-bot as code owners November 20, 2026 19:20
@tushar-signoz tushar-signoz changed the title fix: keep a lower max_execution_time when the context has a deadline fix: do not derive max_execution_time from the context deadline Nov 20, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@chernser chernser left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

thank you for updating the documentation!

@security-ai-gate-reviewer security-ai-gate-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Co-signing @chernser's approval of 8721a42: the AI review gate is green on this commit.

@chernser
chernser merged commit 2653ee0 into ClickHouse:main Nov 20, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context deadline overrides a lower max_execution_time set with WithSettings

4 participants