Skip to content

fix(connections): cancelled connect no longer aborts a later successful connection (#1358)#1367

Merged
datlechin merged 3 commits into
mainfrom
fix/1358-cancel-connection-leak
May 21, 2026
Merged

fix(connections): cancelled connect no longer aborts a later successful connection (#1358)#1367
datlechin merged 3 commits into
mainfrom
fix/1358-cancel-connection-leak

Conversation

@datlechin

@datlechin datlechin commented May 21, 2026

Copy link
Copy Markdown
Member

Fixes #1358.

Cancelling a pending connection only marked the Swift Task cancelled; the blocking PQconnectdb kept running. When the host later became reachable, the cancelled connectToSession resumed and, with no cancellation guard, mutated activeSessions[connectionId], which by then belonged to the new successful attempt: either overwriting its driver or removing the live session.

Changes

  • connectToSession checks Task.isCancelled after connect() and before the session write. This is the actual fix.
  • A cancelled attempt disconnects only its own driver and never touches the connectionId-keyed session or SSH tunnel (which avoids tearing down the retry's tunnel).
  • Genuine failures still clean up as before. Postgres connect() now uses the cancellable dispatch helper, which only short-circuits a cancel that lands before dispatch; it does not interrupt an in-flight blocking PQconnectdb, so the Task.isCancelled checks are what guarantee correctness.

Tests

CancelledConnectionCleanupTests: a cancelled attempt leaves the session intact; a genuine failure removes it and either clears currentSessionId or moves it to a remaining session. The race is timing-dependent, so these pin the extracted cleanup helper's behaviour rather than reproducing the race. All pass.

Known follow-up

Cancelling a tunnelled connection without retrying can leave the SSH tunnel up until a later attempt replaces it. Tracked separately.

@datlechin datlechin merged commit 07da2bf into main May 21, 2026
2 checks passed
@datlechin datlechin deleted the fix/1358-cancel-connection-leak branch May 21, 2026 05:56
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.

Canceling connection does not actually cancel

1 participant