Skip to content

fix(connections): stop reconnecting on TLS failures that retrying cannot fix - #3170

Merged
datlechin merged 3 commits into
mainfrom
fix/health-monitor-tls-abort
Sep 28, 2026
Merged

datlechin merged 3 commits into
mainfrom
fix/health-monitor-tls-abort

Conversation

@datlechin

Copy link
Copy Markdown
Member

Found while fixing #3166 (#3168).

Root cause

The health monitor's reconnect loop (DatabaseManager+Health.swift) stopped only on an authentication failure. Every other error returned .retry, and the loop retries forever with a 120-second ceiling. So a TLS failure that no amount of retrying can fix, such as a server that started requiring TLS, one that stopped accepting it, or a client certificate or key it rejects, was retried forever. From attempt 5 the window said "The connection stopped responding." and never named the reason. SSLHandshakeError is not a PluginDriverError, and its text matches none of the auth phrases, so it never stopped the loop.

A second defect sat underneath. ClickHouseSSLClassifier fell through from its URLError switch to a message check for "hostname", so a DNS failure (URLError.cannotFindHost, "A server with the specified hostname could not be found.") was reported as a TLS hostname mismatch. Measured with a probe. Stopping on a hostname mismatch would have made every ClickHouse DNS blip after sleep or a VPN change stop the reconnect for good.

What changed

  • ConnectionFailureClassifier.reconnectVerdict(for:) decides per SSLHandshakeError case:
    • Stop: serverRejectedPlaintext, serverRequiresPlaintext, clientCertRequired, clientKeyPassphraseRequired, clientKeyPassphraseIncorrect, clientKeyInvalid. These need an edit to the connection.
    • Keep retrying, but show the certificate error: untrustedCertificate, hostnameMismatch. A captive portal, a TLS-inspecting proxy or split-horizon DNS off the VPN produces exactly these, and the connection recovers once the network is right, so they must not stop the loop.
    • Keep retrying with the generic message: cipherMismatch, unknown (drivers use them as catch-alls for resets during a restart), and any future case.
  • A stop is fenced on the driver the attempt started with, so a late loser writes neither status nor liveness. The driver stays installed, per the "handle, not a live connection" invariant. The window shows the full SSLHandshakeError.formatted text and Reconnect.
  • A retry carries its typed failure on ReconnectOutcome.retry(_:). The monitor hands the last attempt's failure to the attempt-5 threshold, which shows it in place of "The connection stopped responding." The on-demand verification path writes it too, and no longer overwrites a reason a stopped reconnect already recorded.
  • ClickHouseSSLClassifier: once a URLError is found, its code decides (default: return nil). This is a body change only, so there is no ABI change.

Behaviour change: PostgreSQL's "no pg_hba.conf entry … no encryption" is classified as serverRejectedPlaintext, so it now stops the reconnect where it used to retry forever. That is intended: TLS off against a server that requires it is a setting only the user can change.

The auth path gives the same message as before. Its status write is now fenced on the attempted driver as well.

Verification

  • verify.sh build: PASS.
  • verify.sh test: 164/164 across 26 suites, including HealthMonitorReconnectTests, ConnectionVerificationTests, ConnectionFailureClassifierTests, the seven classifier suites in PluginSSLClassifierTests.swift, ConnectionHealthMonitorAbortTests, ReconnectDegradationTests, ReconnectCredentialRecoveryTests and the new ConnectionHealthMonitorRetryTests. Each of the 22 new or changed tests was confirmed executed in the log. The reconnect tests drive a FakeMSSQLPlugin whose connect throws a chosen error, keyed by connection id so parallel suites cannot collide.
  • verify.sh abi against the merge base: PluginKit ABI unchanged.
  • swiftlint --strict on every changed file: 0 violations. Seven pre-existing violations in two touched test helpers were fixed while there.
  • Docs style, source and link checks pass.

No UI test: the reconnect path is driven by the health monitor, which unit tests reach deterministically through the fake plugin, and a real TLS failure on reconnect cannot be staged deterministically in XCUITest.

Reviewed by the code-review skill's design critique before implementation (Codex is out of quota until 2026-09-29). The critique is what moved untrustedCertificate and hostnameMismatch from stop to retry-with-reason.

@mintlify

mintlify Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
TablePro 🟢 Ready View Preview Sep 28, 2026, 3:13 PM

💡 Tip: Enable Automations to automatically generate PRs for you.

@datlechin
datlechin merged commit 4694caf into main Sep 28, 2026
3 checks passed
@datlechin
datlechin deleted the fix/health-monitor-tls-abort branch September 28, 2026 19:13
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.

1 participant