Repository navigation
Fixes #29310: add TCP reachability preflight to database CheckAccess - #29308
Conversation
The database gate check (CheckAccess -> ping) now runs a raw TCP connect to the engine's resolved host:port before opening the driver connection. This separates a network failure (DNS, firewall, wrong port) from an auth/protocol failure: an unreachable host now fails as a network problem with an actionable diagnosis, before TLS or credentials are exercised. - core/connections/test_connection/network.py: tcp_probe + NetworkUnreachableError + NETWORK_ERRORS diagnosis pack, matched by exception type so it is driver- and platform-agnostic. - ErrorPack.including(other): immutable compose that appends another pack as a lower-precedence fallback; connector rules still match first. - Matchers.exception(*types): type/cause-chain matcher for socket errors. - ping() skips the preflight when the engine URL has no host:port (file-based URLs, connector-tunnelled engines). - MySQL folds NETWORK_ERRORS into its pack via .including(); every database connector inherits the preflight as it migrates onto the framework.
✅ PR checks passedThe linked issue has a description and all required Shipping project fields set. Thanks! |
Address review feedback on the test-connection network preflight: - NETWORK_ERRORS catch-all now matches NetworkUnreachableError (which only tcp_probe raises) instead of a bare OSError. The error pack classifies every step, not just CheckAccess, so the OSError catch-all could mislabel an unrelated OS error (a dropped pipe, a permission error) from a later step as "Cannot reach the host". Scoping it to our own type makes that impossible; unrelated errors keep their raw errorLog. - Document that ping()'s preflight assumes the driver connects directly to the URL host:port; connectors whose transport differs (HTTP gateway, load balancer, unix socket) should call run_sql() directly instead of ping(). - Add a regression test asserting an unrelated OSError is not classified as a reachability problem.
🔴 Playwright Results — 2 failure(s), 52 flaky✅ 4421 passed · ❌ 2 failed · 🟡 52 flaky · ⏭️ 39 skipped
Genuine Failures (failed on all attempts)❌
|
|
Code Review ✅ Approved 1 resolved / 1 findingsAdds a TCP reachability preflight to the database CheckAccess flow to decouple network connectivity issues from driver-level errors. Resolved the potential for misleading error classification for post-access operations by successfully implementing an immutable error pack composition. ✅ 1 resolved✅ Edge Case: TCP preflight may false-fail when driver path differs from URL host:port
OptionsDisplay: compact → Showing less information. Comment with these commands to change:
Was this helpful? React with 👍 / 👎 | Gitar |



Fixes #29310
What
Adds a raw TCP reachability preflight to the database gate check. When a database connector's
CheckAccessstep runs (ping), it now opens a plain TCP connection to the engine's resolvedhost:portbefore the driver connection, so an unreachable host fails as a network problem rather than being tangled into a driver/TLS/auth error.This separates the two failure classes a user actually needs distinguished:
How
core/connections/test_connection/network.py(new):tcp_probe(host, port, timeout)raisingNetworkUnreachableError(chaining the underlying socket error), plus aNETWORK_ERRORSdiagnosis pack. Diagnoses are keyed by exception type, not message, so they are driver- and platform-agnostic (e.g.ConnectionRefusedErrorregardless of errno 61 vs 111).ErrorPack.including(other): immutable composition that appends another pack as a lower-precedence fallback — the connector's own rules still match first. Returns a new pack; the originals are untouched.Matchers.exception(*types): type/cause-chain matcher used byNETWORK_ERRORS.ping(): runs the preflight first, and skips it cleanly when the engine URL carries nohost:port(file-based URLs like SQLite, connector-tunnelled engines).errors = MYSQL_ERRORS.including(NETWORK_ERRORS).The probe lives in the shared
ping()helper, so every database connector inherits reachability automatically as it migrates onto the new test-connection framework. The probe timeout is 20s (well under the 60s per-step budget, with headroom for slow / cross-region hosts).Scope
Database connectors only for now — the path rides the shared
ping()helper.network.pyis category-neutral, so dashboard/messaging/pipeline checks can reusetcp_probe/NETWORK_ERRORSwhen they land on the framework.Tests
test_network.py(new): probe success against a live socket, refused-port →NetworkUnreachableError, and each diagnosis (DNS / refused / timeout / generic).test_classifier.py:Matchers.exceptionchain matching,.includingfallback ordering, precedence on overlap, and immutability of the original pack.test_database_checks.py:pingskips the preflight without a host:port; unreachable host fails as a network error carrying the TCP-connect command.75 tests pass across the framework + MySQL suites; ruff lint/format and basedpyright clean.
Greptile Summary
This PR adds a TCP reachability preflight check to the database
CheckAccess(ping) step, so an unreachable host produces an actionable network error before any driver/TLS/auth logic is exercised. It introducestcp_probe,NetworkUnreachableError, and aNETWORK_ERRORSdiagnosis pack, and extends the classifier withMatchers.exception()(type-based chain matching) andErrorPack.including()(immutable fallback composition).network.py(new):tcp_probewraps everyOSErrorintoNetworkUnreachableError;NETWORK_ERRORSusesNetworkUnreachableErroras its catch-all (not bareOSError), so only preflight failures receive a "reachability" diagnosis and unrelatedOSErrorsubclasses from later steps are ignored.classifier.py:Matchers.exception(*types)andErrorPack.including(other)are added with correct immutability and precedence semantics, and the runner correctly passesfailure.cause(theNetworkUnreachableError) toclassify().NETWORK_ERRORSas a lower-precedence fallback; MySQL errno rules (e.g. 2013 for server-lost) still win over the network matchers for driver-level timeouts and connection drops.Confidence Score: 5/5
Safe to merge. The preflight is cleanly gated on host+port presence, file-based and tunnelled engines skip it correctly, and the NetworkUnreachableError catch-all is scoped tightly enough that unrelated OSError subclasses from later steps are never misdiagnosed.
The key design risk — the catch-all rule misclassifying non-network OSErrors (BrokenPipeError, PermissionError) from later steps as reachability failures — has been explicitly avoided by keying the catch-all on NetworkUnreachableError rather than OSError. The runner correctly extracts failure.cause before calling classify(), so the NetworkUnreachableError carrying the original socket error reaches the pack as intended. MySQL errno rules (2003, 2013) are ordered before the NETWORK_ERRORS fallback, so driver-level connection/timeout errors still get their sharper diagnoses. Tests cover all four diagnosis branches, immutability of ErrorPack, cause-chain matching, and the critical isolation of non-preflight OSErrors.
No files require special attention.
Important Files Changed
Sequence Diagram
%%{init: {'theme': 'neutral'}}%% sequenceDiagram participant Runner participant ping participant _preflight participant tcp_probe participant run_sql participant ErrorPack Runner->>ping: call check_access() ping->>_preflight: _preflight(client) _preflight->>_preflight: "host, port = client.url.host, client.url.port" alt host and port present _preflight->>tcp_probe: "tcp_probe(host, port, timeout=20s)" alt TCP connect succeeds tcp_probe-->>_preflight: None (ok) else OSError (DNS / refused / timeout) tcp_probe-->>_preflight: "raise NetworkUnreachableError(cause=OSError)" _preflight-->>ping: raise CheckError(NetworkUnreachableError, Evidence) ping-->>Runner: raise CheckError Runner->>ErrorPack: classify(NetworkUnreachableError) ErrorPack-->>Runner: Diagnosis end else no host:port (SQLite / tunnel) _preflight-->>ping: skip (return None) end ping->>run_sql: run_sql(client, SELECT 1, ...) alt query succeeds run_sql-->>ping: "Evidence(summary=connection established)" ping-->>Runner: Evidence else driver / auth / TLS error run_sql-->>ping: raise CheckError(cause, Evidence) ping-->>Runner: raise CheckError Runner->>ErrorPack: classify(cause) ErrorPack-->>Runner: Diagnosis (MySQL errno rules first, then NETWORK_ERRORS fallback) end%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%% sequenceDiagram participant Runner participant ping participant _preflight participant tcp_probe participant run_sql participant ErrorPack Runner->>ping: call check_access() ping->>_preflight: _preflight(client) _preflight->>_preflight: "host, port = client.url.host, client.url.port" alt host and port present _preflight->>tcp_probe: "tcp_probe(host, port, timeout=20s)" alt TCP connect succeeds tcp_probe-->>_preflight: None (ok) else OSError (DNS / refused / timeout) tcp_probe-->>_preflight: "raise NetworkUnreachableError(cause=OSError)" _preflight-->>ping: raise CheckError(NetworkUnreachableError, Evidence) ping-->>Runner: raise CheckError Runner->>ErrorPack: classify(NetworkUnreachableError) ErrorPack-->>Runner: Diagnosis end else no host:port (SQLite / tunnel) _preflight-->>ping: skip (return None) end ping->>run_sql: run_sql(client, SELECT 1, ...) alt query succeeds run_sql-->>ping: "Evidence(summary=connection established)" ping-->>Runner: Evidence else driver / auth / TLS error run_sql-->>ping: raise CheckError(cause, Evidence) ping-->>Runner: raise CheckError Runner->>ErrorPack: classify(cause) ErrorPack-->>Runner: Diagnosis (MySQL errno rules first, then NETWORK_ERRORS fallback) endReviews (12): Last reviewed commit: "Merge branch 'main' into feat/test-conne..." | Re-trigger Greptile