Repository navigation
Conversation
Transaction.pipeline returned the first error from try_join_all, which drops the futures of the queries still in flight. The server runs them anyway, so nothing was gained by not waiting, and a dropped request is about to mean an abandoned query that gets a cancel request. Collect all results with join_all and return the first error.
When the task awaiting a query is cancelled, for example by an asyncio.wait_for timeout, pyo3 drops the Rust future. tokio-postgres has already queued the request, so the server keeps running the query, which can still commit after the caller gave up on it, and the connection goes back to the pool, where the next caller waits behind that query (psqlpy-python#180). PoolConnection now holds its deadpool Object in a CancellableObject. Every request awaited through the Connection trait (query, query_typed, query_one, batch_execute, prepare) goes through cancel_on_drop: if the future is dropped before the request completes, a cancel request is sent from a background task, over a new connection that uses the pool's TLS connector, as asyncpg does. A connection that had a request abandoned is taken out of the pool with Object::take and closed when it is dropped, as psycopg_pool does with a connection returned while a query is active. That also covers a cancel request that cannot reach the server, and the silent network path from the issue. The next request on the same connection waits until the cancel request has been written. tokio-postgres returns before the server acts on it, so this narrows, but does not close, the window in which the cancel stops that next request instead. Cursor fetches and COPY are not covered.
z22092
marked this pull request as ready for review
November 23, 2026 17:16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When the task awaiting a query is cancelled (an
asyncio.wait_fortimeout,task.cancel()), pyo3 drops the Rust future, but tokio-postgres has already queued the request. The server keeps running the query and can commit it later, and the connection goes back to the pool while it is still busy, so the next caller waits behind it.Changes:
PoolConnectionholds its deadpoolObjectin a newCancellableObject(src/connection/cancel.rs). Every request awaited through theConnectiontrait (query,query_typed,query_one,batch_execute,prepare) goes throughcancel_on_drop: if the future is dropped before the request completes, a cancel request is sent from a background task.build_tlsruns once, and itsConfiguredTLSis kept onConnectionPool, onConnection(insidedb_pool) and on the connection itself.Object::takeand closed when it is released, instead of being handed to the next caller. This also covers a cancel request that cannot reach the server.Transaction.pipelinenow waits for every query before returning the first error (join_allinstead oftry_join_all). Otherwise a failing statement would abandon the queries still in flight and close the connection.docs/components/connection_pool.mddocuments the behaviour.Not covered: cursor fetches and the COPY methods still await their requests directly. The docs say so.
Known limitation:
CancelToken::cancel_queryin tokio-postgres returns once the cancel request is written (cancel_query_rawdoeswrite_all,flushandshutdown, without reading until the server closes the socket). If the abandoned query had already finished, the cancel request can still stop the next query on that connection. The docs recommend releasing the connection after a timeout. asyncpg avoids this by waiting for the server to close the cancel connection; doing the same here would need a change in tokio-postgres.Motivation and Context
Closes #180.
How has this been tested?
python/tests/test_query_cancellation.py(7 tests) and one TLS test intest_ssl_mode.py. Each one holds a row lock from another session, so a statement waits on the server. It cancels the awaiting task oncepg_stat_activityshows the statement active, then checks the server and the pool. Onmain, 6 of these 8 fail. The other two,test_completed_query_keeps_its_connectionandtest_failed_pipeline_keeps_its_connection, pass there and guard against regressions.NoTlsinstead of the pool's connector.test_ssl_mode.pyincluded: 282 passed against PostgreSQL 18.7 with SSL on, Python 3.13. Not run locally on PostgreSQL 14 to 17 or other Python versions.cargo fmtwith the CI config, and clippy with the CI flags: clean. Locally this needed-A unknown-lints, because clippy 1.93 does not knowclippy::unused_async_trait_impl, which the code already uses.ruff check,ruff format --checkandmypy python: clean.pg_stat_activity.Before (
main):After:
Types of changes
Checklist: