Skip to content

Close failed connections without reconnecting during exception cleanup - #479

Open
shinzui wants to merge 1 commit into
KovalevDima:masterfrom
shinzui:fix/connection-lifecycle-cancellation
Open

shinzui wants to merge 1 commit into
KovalevDima:masterfrom
shinzui:fix/connection-lifecycle-cancellation

Conversation

@shinzui

@shinzui shinzui commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

This is the third fix, and it involves an API behavior change I'd appreciate your feedback on. While testing a connection pool with bounded readiness checks, I encountered problems with cancellation and failed-connection disposal.

withConnection reconnects inside its exception handler, including after a timeout. Cleanup can therefore enter another connection attempt and authentication. If reconnection throws, the connection-state MVar remains empty, so later operations can wait indefinitely. Failed or cancelled initialization/authentication can also leave sockets open, and I couldn't find a public close operation for pool disposal.

This proposes closing and invalidating failed handles instead of reconnecting during exception cleanup. It also releases resources when initialization fails and exports an idempotent closeConnection. Subsequent operations raise ConnectionClosed; callers must open a fresh handle to retry. The existing error-test example has been updated accordingly. closeConnection waits for active operations, so owners should stop concurrent use before disposal.

The connection-lifecycle-regressions suite passes on GHC 9.12.4, covering initialization/authentication failures, cancellation, repeated close, invalidated-handle reuse, and a throwing close callback. It uses real allocated sockets with scripted IO. The updated t004-errors executable also builds. I haven't run the full compiler matrix or live-server suite for this branch.

Would explicit reopening fit the intended API? If automatic recovery should be preserved, I'm happy to discuss an alternative that keeps reconnection out of cancellation cleanup.

Add idempotent closeConnection and release sockets when initialization or authentication fails or is cancelled.

BREAKING CHANGE: failed operations invalidate their connection. Further operations raise ConnectionClosed; callers must open a fresh handle to retry.

This branch has not been deployed

No deployments
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