Skip to content

fix(postgresql): avoid stale running-query errors after disconnect - #450

Closed
TywinLanni wants to merge 1 commit into
jasync-sql:masterfrom
TywinLanni:master
Closed

TywinLanni wants to merge 1 commit into
jasync-sql:masterfrom
TywinLanni:master

Conversation

@TywinLanni

@TywinLanni TywinLanni commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Clear and fail the pending PostgreSQL query promise when the connection is closed.
  • Notify the connection from channelInactive via onDisconnect.
  • Add regression coverage with unit tests and a Testcontainers integration test.

Why

After a query timeout or channel close, queryPromise remained set. Reusing the same physical connection for ROLLBACK then failed with ConnectionStillRunningQueryException (race -> false) even though no query was running.

Fixes #449

Tests

  • :postgresql-async:test --tests "com.github.jasync.sql.db.postgresql.PostgreSQLConnectionLifecycleTest" --tests "com.github.jasync.sql.db.postgresql.codec.PostgreSQLConnectionHandlerTest"
  • :postgis-jasync:test --tests "com.github.aysnc.sql.db.integration.PostgisQueryTimeoutSpec"
  • Full :postgis-jasync:test passes.

The full :postgresql-async:test still has two pre-existing failures (LocalDateTime assertion and SelfSignedCertificate SSL test). Both reproduce on clean master without this change.

@oshai

oshai commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Thanks @TywinLanni for reporting #449, putting together a clear reproduction, and tracking down the stale queryPromise on disconnect!

While reviewing how this behaves end-to-end when a query times out inside a transaction without waiting for the channel close to finish, we noticed a few additional race windows across db-async-common and postgresql-async:

  • TimeoutScheduler completed the query future before marking the connection timed out and disconnected, and isConnected() stayed true while CloseMessage was flushing asynchronously, which allowed an immediate ROLLBACK or pool return to race with channel teardown.
  • inTransaction and SuspendingConnection.inTransaction unconditionally sent ROLLBACK on failure, which could mask TimeoutException or leave the transaction future uncompleted when the socket was already disconnected.

I opened #451 (with you credited as co-author) to cover the full timeout, disconnect, and transaction rollback lifecycle in one change. I suggest taking a look at #451 to review the approach and verify it covers your setup, and if it looks good you can close this PR in favor of #451.

@TywinLanni

Copy link
Copy Markdown
Contributor Author

Thanks @oshai for such a quick response — I’m closing my MR in favor of yours.

@TywinLanni TywinLanni closed this Nov 13, 2026
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.

[postgresql-async] Clear pending query promise on disconnect to prevent stale ConnectionStillRunningQueryException

2 participants