You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[postgresql-async] Clear pending query promise on disconnect to prevent stale ConnectionStillRunningQueryException #449
What happened: after a query timeout inside a transaction, PostgreSQLConnection.disconnect() closes the channel but leaves the pending queryPromise set. isQuerying() remains true. If a pool returns the same physical connection for cleanup, ROLLBACK fails with ConnectionStillRunningQueryException (race -> false), even though the connection is already closed.
What I expected: disconnect, query timeout, and channel close should fail and clear the pending query promise. The next use must not report that a query is still running. A closed connection should surface as a connection-closed error, and the pool should discard it.
Postgres 14 (postgis/postgis:14-3.2 via Testcontainers)
Postgres 18.1 in my root case
Java and Kotlin version
Java 21 in the original report
Java 17 for the local regression run
Kotlin 1.8.21
Minimal reproduction
val configuration =Configuration(
username ="postgres",
host ="localhost",
port =5432,
password ="postgres",
database ="postgres",
queryTimeout =Duration.ofSeconds(1)
)
val connection =PostgreSQLConnection(configuration)
connection.connect().get(5, TimeUnit.SECONDS)
connection.sendQuery(
""" CREATE OR REPLACE FUNCTION slow_jasync_function() RETURNS void AS $$ BEGIN PERFORM pg_sleep(5); END; $$ LANGUAGE plpgsql""".trimIndent()
).get(5, TimeUnit.SECONDS)
connection.sendQuery("BEGIN").get(5, TimeUnit.SECONDS)
val timedOutQuery = connection.sendQuery("SELECT slow_jasync_function()")
try {
timedOutQuery.get(10, TimeUnit.SECONDS)
} catch (e:ExecutionException) {
// TimeoutException is expected here.
}
// Wait until the timeout-driven disconnect closes the channel.// Before the fix: isQuerying() is still true and the next line throws// ConnectionStillRunningQueryException(race -> false).
check(!connection.isQuerying())
connection.sendQuery("ROLLBACK")
Root cause and fix
PostgreSQLConnection.disconnect() did not call failQueryPromise, and PostgreSQLConnectionHandler.channelInactive() only logged the disconnect. The fix clears and fails the pending query promise in disconnect(), adds onDisconnect() to the connection delegate, and calls it from channelInactive().
Activity
fix: clear query state on disconnect/timeout and guard tx rollback (j…