Skip to content

fix: skip Cancel in QueryUnbufferedAsync when reader is drained (#2188)#2207

Open
leno23 wants to merge 1 commit into
DapperLib:mainfrom
leno23:fix/query-unbuffered-skip-cancel-when-drained-2188
Open

fix: skip Cancel in QueryUnbufferedAsync when reader is drained (#2188)#2207
leno23 wants to merge 1 commit into
DapperLib:mainfrom
leno23:fix/query-unbuffered-skip-cancel-when-drained-2188

Conversation

@leno23
Copy link
Copy Markdown

@leno23 leno23 commented May 17, 2026

Summary

  • Track whether QueryUnbufferedAsync finished reading all rows and result sets (readerDrained).
  • Only call cmd.Cancel() in the finally block when the reader was not fully drained and is still open.

This avoids unnecessary command cancellation on providers such as Npgsql where DbDataReader.IsClosed remains false after the result set is fully consumed, which was causing extra connection churn and server-side cancel overhead.

Test plan

  • Existing TestBasicStringUsageUnbufferedAsync / cancellation tests (CI)
  • With Npgsql: run QueryUnbufferedAsync to completion and confirm no CANCEL is issued in PostgreSQL logs

CI not run locally (no .NET SDK in contributor environment).

Fixes #2188

Made with Cursor

Npgsql keeps IsClosed false after the result set is fully consumed, which
caused an unnecessary cmd.Cancel() on every completed unbuffered query.

Only cancel when enumeration did not finish draining the reader.

Fixes DapperLib#2188
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.

QueryUnbufferedAsync cancels command unnecessarily for Npgsql, causing performance degradation

1 participant