Repository navigation
Don't cancel the command after QueryUnbufferedAsync completes normally - #2245
Open
aaronwilliams-tesouro wants to merge 1 commit into
Open
aaronwilliams-tesouro wants to merge 1 commit into
aaronwilliams-tesouro wants to merge 1 commit into
Conversation
QueryUnbufferedAsync's finally block calls DbCommand.Cancel() whenever the reader is still open, and on the success path it always is. Npgsql sends that cancel over a new connection, so every completed query pays for an extra connection to cancel a command that has already finished. Close the reader and clear it after reading to the end, as the sync Query and QueryRow paths already do. Do the same on the early exit for statements with no columns; sync QueryImpl still cancels there. Cancel() now only runs when the caller stops early or an error is thrown. Fixes DapperLib#2188
aaronwilliams-tesouro
marked this pull request as ready for review
October 20, 2026 16:32
This branch has not been deployed
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.
Fixes #2188.
Problem
QueryUnbufferedAsynccallsDbCommand.Cancel()in itsfinallywhenever!reader.IsClosed. On the success path the reader is still open at that point, so every query that is read to the end gets cancelled. Npgsql sends that cancel over a new connection, so each completed query costs an extra connection, plus server work to cancel a command that has already finished. #2188 reports similar behaviour with MySqlConnector.Fix
Close the reader and set it to
nullbefore thefinallyruns. The read-to-end exit now mirrors the syncQueryImplandQueryRowImpl, comment included. The early exit for statements that return no columns gets the same treatment. SyncQueryImplstill cancels on that exit and could get the same fix in a follow-up.Cancel()still runs when the caller stops enumerating early or an exception is thrown, which is the case #2121 added it for.command.OnCompleted()now runs after the reader is closed, as it does on the sync path. Providers fill in output parameters once the reader closes, so they're now populated when that callback runs.#2207 tried a drained flag and was closed by its author without review. This PR mirrors the sync code instead and adds tests.
Tests
The tests use SQLite: it needs no server and still reproduces the bug. They count
Cancel()calls through a pass-throughDbConnection/DbCommandwrapper:QueryUnbufferedAsync_ReadToEnd_DoesNotCancelQueryUnbufferedAsync_NoColumns_DoesNotCancelQueryUnbufferedAsync_StopEarly_CancelsRelated: #2220 removes
Cancel()from the sync single-row cleanup.I agree to the terms in NonCLA.md.