Skip to content

fix: clear query state on disconnect/timeout and guard tx rollback - #451

Merged
oshai merged 3 commits into
masterfrom
fix/issue-449-timeout-disconnect-rollback
Nov 13, 2026
Merged

oshai merged 3 commits into
masterfrom
fix/issue-449-timeout-disconnect-rollback

Conversation

@oshai

@oshai oshai commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes stale running-query errors (ConnectionStillRunningQueryException), async-disconnect race windows, and transaction rollback hangs/error-masking after query timeouts or peer disconnects:

  • TimeoutSchedulerImpl (db-async-common): Support an optional claimTimeout hook so PostgreSQLConnection and MySQLConnection atomically claim queryPromiseReference, set isTimeout = true, and mark the connection disconnected before completing the query CompletableFuture with TimeoutException. Callers woken by TimeoutException never observe isQuerying() == true, isTimeout() == false, or isConnected() == true.
  • PostgreSQLConnection / PostgreSQLConnectionHandler / PostgreSQLConnectionDelegate (postgresql-async):
    • Track an atomic disconnected flag in PostgreSQLConnectionHandler set immediately on disconnect() and channelInactive so isConnected() becomes false synchronously before CloseMessage finishes flushing, preventing subsequent queries (ROLLBACK) or pool validation from passing on an actively closing socket, and complete disconnectionPromise when already inactive.
    • Clean up and fail pending queryPromiseReference, closeStatementReference, and handshake connectionFuture on disconnect() and channelInactive (onDisconnect()), without logging false ERROR messages on already-completed futures.
    • Guard releasePreparedStatement when !isConnected().
  • MySQLConnection (mysql-async): Consult @Volatile channelClosed in isConnected(), complete disconnectionPromise when closing an unconnected instance, and claim queryPromiseReference atomically on query timeout.
  • ConcreteConnectionBase & SuspendingConnection (db-async-common):
    • In inTransaction, when the transaction body fails: skip ROLLBACK if !isConnected() || isTimeout() (server rolls back uncommitted transactions on disconnect), disconnect if isQuerying() is still true (e.g. caller-side withTimeout / orTimeout), and catch synchronous/asynchronous rollback errors as suppressed exceptions so the returned future never hangs and the original TimeoutException is never masked.
    • In releaseIfNeeded, avoid calling releasePreparedStatement on disconnected, timed-out, or still-querying connections when the prepared statement future failed.

Related issue: Fixes #449 (reported in #421; supersedes #450 — thanks to @TywinLanni for reporting and investigating)

Checklist

  • Added or updated tests covering the change
  • ./gradlew clean build passes locally

Claim the active query promise atomically and mark connections
disconnected before completing timed-out query futures so woken
callers never observe stale running-query or connected states.

Clean up pending PostgreSQL query, close-statement, and handshake
futures on disconnect and channelInactive, and guard inTransaction
and releaseIfNeeded against rolling back or releasing over timed-out,
disconnected, or still-querying connections.

Fixes #449
Co-authored-by: TywinLanni <sasha291096@gmail.com>
@TywinLanni

Copy link
Copy Markdown
Contributor

I checked the code locally against my case and got the expected behavior

Keep PostgreSQLConnectionDelegate unchanged by passing an explicit
onDisconnect callback to PostgreSQLConnectionHandler, release all
DataRowMessage ByteBuf slices when currentQuery is cleared across
threads, and unwrap CompletionException before attaching suppressed
rollback errors.
Extract rollbackAfterFailure, commitAfterSuccess, and shared
CompletionException unwrapping/suppression helpers to flatten
ConcreteConnectionBase.inTransaction and SuspendingConnection.
@oshai
oshai merged commit 9aeb71d into master Nov 13, 2026
2 checks passed
@oshai
oshai deleted the fix/issue-449-timeout-disconnect-rollback branch November 13, 2026 20:45
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