Skip to content

Fix MySQL connect_timeout not taking effect under the query profiler - #109592

Merged
alexey-milovidov merged 2 commits into
masterfrom
fix-mysql-connect-timeout-blackhole
Jul 7, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
fix-mysql-connect-timeout-blackhole

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Related: #109472
Related: https://jira.mariadb.org/browse/CONC-834

Problem. The MySQL connect_timeout setting (and the read/write timeouts) can be silently ignored inside ClickHouse. A connection attempt to an unresponsive MySQL server — one that drops packets instead of refusing the connection — hangs for ~135 seconds (the kernel SYN-retransmission limit) even with connect_timeout = 1:

ATTACH DATABASE m ENGINE = MySQL('192.0.2.1:3306', 'fake_db', 'user', 'password')
SETTINGS connect_timeout = 1, connection_max_tries = 1  -- takes 2m16s instead of ~1s

Any query thread touching such a server blocks for minutes while holding locks (e.g. DatabaseMySQL::mutex), and stateless test 04210_show_remote_databases_in_system_tables spent ~135 seconds per run on exactly this.

Root cause. pvio_socket_wait_io_or_timeout in mariadb-connector-c restarts poll with the full original timeout after EINTR. ClickHouse query threads receive sampling-profiler timer signals (query_profiler_real_time_period_ns, default 1 s) installed without SA_RESTART, so whenever the signal period does not exceed the timeout, the poll deadline is reset by every signal and never expires; with a blackholed host the poll returns only when the kernel exhausts SYN retransmissions. Verified with a C repro against the bundled library: no signals → fails in exactly 1.00 s; with a 100 ms periodic signal → 135.32 s and 1353 EINTRs, matching the in-server measurement. The connector and the mysqlxx option plumbing are otherwise correct.

Fix. Bump the mariadb-connector-c submodule to a fork commit (ClickHouse/mariadb-connector-c@111ec1a5, branch eintr-connect-timeout) that tracks the deadline with CLOCK_MONOTONIC and re-polls with the remaining time after EINTR, reporting ETIMEDOUT once the budget is exhausted. One fix covers all callers of wait_io_or_timeout: TCP connect, TLS handshake, and read/write waits. The same defect existed upstream; reported as CONC-834 and the suggested patch was accepted the same day (Closed/Fixed, fixVersions 3.3.20 and 3.4.10), so the fork patch can be dropped at the next connector version bump.

The regression test 04507_mysql_connect_timeout_under_profiler attaches a MySQL database pointing at a blackholed TEST-NET-1 address with connect_timeout = 1 under a 100 ms query profiler and asserts via system.query_log that the ATTACH is bounded by the timeout (fixed ≈ 2 s, unfixed ≈ 135 s). Side effect: 04210_show_remote_databases_in_system_tables drops from ~135 s to ~4 s.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed the MySQL connect_timeout, read and write timeouts not taking effect: a connection attempt to an unresponsive MySQL server could hang for more than two minutes even with connect_timeout = 1, because the sampling query profiler's periodic signals kept resetting the MySQL client's internal poll deadline.

Version info

  • Merged into: 26.7.1.637 (included in 26.7 and later)

Bump mariadb-connector-c: pvio_socket_wait_io_or_timeout restarted poll
with the full original timeout after EINTR, resetting the deadline on
every signal. ClickHouse query threads receive sampling-profiler timer
signals (query_profiler_real_time_period_ns, default 1 s) without
SA_RESTART, so with a signal period not exceeding connect_timeout the
poll never expired: a connection attempt to a blackholed host (SYN
dropped, e.g. 192.0.2.1) hung until the kernel exhausted SYN
retransmissions (~130 s) regardless of connect_timeout. The connector
now re-polls with the remaining time and reports ETIMEDOUT when the
budget is exhausted. This also bounds the TLS handshake and read/write
waits that go through the same function, and cuts test
04210_show_remote_databases_in_system_tables from ~135 s to ~4 s.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2c18d21]

Summary: ✅


AI Review

Summary

This PR updates the bundled mariadb-connector-c to stop resetting the full poll timeout after EINTR, which restores the intended connect_timeout behavior for MySQL connections under periodic query-profiler signals. I checked the current patch, the connector-side call paths, the new stateless regression test, and the prior inline discussion; I did not find an unresolved correctness, safety, or evidence gap in the current revision.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default submodule changed At least one submodule changed in this PR. labels Jul 7, 2026
@tiandiwonder tiandiwonder added the can be tested Allows running workflows for external contributors label Jul 7, 2026
The previous 60 s bound only caught the full ~135 s regression; a partial
regression (e.g. capping the EINTR retries instead of decrementing the
remaining budget) could leave the timeout broken while keeping the test
green. The fixed ATTACH takes ~1 s.

Co-Authored-By: Claude Fable 5 <[email protected]>

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

Nice finding and fix

@alexey-milovidov alexey-milovidov 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.

Classic.

@alexey-milovidov alexey-milovidov self-assigned this Jul 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 7, 2026
Merged via the queue into master with commit 46fc527 Jul 7, 2026
175 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-mysql-connect-timeout-blackhole branch July 7, 2026 22:42
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 7, 2026
pull Bot pushed a commit to sonvt1710/ClickHouse that referenced this pull request Aug 20, 2026
ReadBufferFromFileDescriptor::poll restarted the interrupted poll with
the full original timeout after EINTR, resetting the deadline on every
signal. Query threads receive periodic query-profiler timer signals
(SA_RESTART does not apply to poll), so with a signal period not
exceeding the timeout the poll never expired while the fd stayed silent.
This reaches users through the streaming-insert block wait
(input_format_max_block_wait_ms in IRowInputFormat::read): a stalled
input source could delay the partial-block flush indefinitely under the
profiler instead of flushing when the wait limit is reached.

Re-poll with the remaining time, computed in microseconds from a single
monotonic anchor: per-retry whole-millisecond accounting would truncate
a sub-millisecond interval to zero and make no progress. The unit test
drives both cadences (10 ms and 0.5 ms) with a per-thread kernel timer
(SIGEV_THREAD_ID), the same mechanism the query profiler uses.

Same defect class as the mariadb-connector-c connect_timeout fix
(ClickHouse#109592, upstream
https://jira.mariadb.org/browse/CONC-834).

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…t-timeout-blackhole

Fix MySQL connect_timeout not taking effect under the query profiler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants