Skip to content

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

Closed

Description

@TywinLanni

What happened vs. what you expected

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.

jasync-sql module and version

  • jasync-postgresql (postgresql-async module), 2.2.4
  • Reproduced on current master

Database server and version

  • 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

  1. added a commit that references this issue on Nov 14, 2026
    9aeb71d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions