Repository navigation
Source: a cancel that cuts a reader-level read short throws; the throwOnCancellation flag is removed - #159
Merged
Conversation
…ancellation and the one-more-row read are removed A canceled read on ForEach, IterateWhile, Select and SelectAsync ended quietly by default: the caller got a partial result that looked complete, after one more row was read and handled. Now a cancel that cuts the read short throws OperationCanceledException. A cancel that finds the read already finished (the read that ends the result returns no row) returns the complete result: nobody is made to throw for work that is done. The token is checked before each read; when it was canceled, one read is made to learn whether a row was still to come, and that row is not handled. A token that cannot be canceled keeps the plain loop. The six overloads that carried the bool throwOnCancellation parameter are removed: the flag's false default returned a silent partial result, and once a cancel that cuts a read short throws, the flag would be a lie. The kept overloads take over the loops, and the token-first Select keeps its shape without the flag until it is removed with the other token-first forms. The command-level callers that passed true are updated for the signature change only. A stream of a mapped query over a plain IDataReader now forwards its token, so a cancel between rows throws there too. Tests pin the behavior (they failed first), the overload binding is read from the IL, and the docs, a compiled sample and the migration lines say what to call instead. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ancel, one more read decides ForEachAsync (useReadAsync), IterateWhileAsync(DbDataReader, ...) and IterateWhileAsync(IDataReader, ValueTask<bool>, ...) read with ReadAsync(token), which throws on a canceled token before a provider can say there are no more rows. So a cancel that arrived while the last row was handled threw, although the result was complete. They now read through one helper: a token canceled before the read makes that one read without it. No more rows returns the complete result; another row throws OperationCanceledException (that row is not handled). No signature changes. Tests cover each of the six loop forms (cancel mid-read throws, cancel during the last row or as the ending read is made returns all rows) and IterateReaderAsync and IterateReaderWhileAsync over a plain reader (a small fake that implements IExecuteReaderAsync). One existing assertion changed: a call with an already canceled token now makes the one lookahead read before it throws (Reads is 1, not 0). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The useReadAsync:false branches of ForEachAsync and IterateWhileAsync, and IterateWhileAsyncInternal (used for a plain IDataReader), still used the old pattern: an extra row was handled after a cancel before the throw, and a canceled token threw up front. They now read through a synchronous twin of the lookahead helper: a token canceled before the read still makes the read; no more rows returns the complete result, another row throws OperationCanceledException and is not handled. Exception types and signatures are unchanged. The two cancel theories now also run these three forms (nine async loop forms in all). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
electricessence
marked this pull request as ready for review
November 16, 2026 16:53
Keeps both sides of Source/Core/PublicAPI.Unshipped.txt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… let ReadAsync cancel the rest, no lookahead The earlier rule made one more read after a cancel to learn whether the result was complete. The rule is now simpler: the token is passed down and cancels at the top of the loop, before the read. Async loops hand the token straight to ReadAsync (the provider cancels the read, also a slow one); sync loops call ThrowIfCancellationRequested before each Read() and before each NextResult() of the read-through. A read that returned no row means the read is done, and nothing after that throws for the token. There is no extra read after a cancel and no extra row handled; a cancel while the last row is handled throws at the next check. Every loop follows it: ForEach, IterateWhile, Select, SelectAsync, ForEachAsync, every IterateWhileAsync form (with and without ReadAsync, DbDataReader and plain IDataReader) and the stream of a mapped query over a plain reader (the token check moved before the read, and the read-through checks it before each NextResult). The lookahead helper is gone; the async loops over a DbDataReader are the original ReadAsync(token) lines again. Tests flip every case of a cancel during the last row to expect OperationCanceledException, keep the cases where the cancel arrives with the ending read (no throw), fix the Read counts, and add a cancel during a slow ReadAsync that takes effect without waiting for the server. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The ReadAsync loop conditions in ForEachAsync and IterateWhileAsync were over 80 columns. Each operand is on its own line; two loops that became multi-line got braces (IDE0011). No change of behaviour: the token stream of the file is unchanged apart from whitespace and those braces. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…with await using The compiled sample shown in foundation.md now uses await using for the SqliteCommand and the SqliteDataReader, as the other async samples do. The sync region is unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.
What this changes
A cancelled
ForEach,IterateWhile,SelectorSelectAsyncon a reader used to end quietly by default and return a partial result that looked complete: the caller got 10 or 11 rows of 100 and no exception. Now a cancellation that cuts a read short throwsOperationCanceledException, and a read that completed returns its complete result.The owner's rule: "we pass a cancellation down into the deepest depths and we let it do the cancellation for us ... at the top of the loop. Before you try to read ... throw if canceled ... You don't do something while things are canceled ... But if it completed, then it's like, oh, well, that's fine."
The rule as built.
ReadAsyncandNextResultAsync, and the provider does the cancelling. A cancel during a slow read takes effect without waiting for the server (a test with a stalledReadAsyncshows it).ThrowIfCancellationRequestedat the top of the loop, before eachRead()and before eachNextResult()of the read through. A plain-reader stream checks before the read, and before skipped rows.ReadorNextResultthat returned false means the read is done: nothing after that throws for the token. There is no extra read after a cancel.The flag goes. Six shipped members are removed, and the no-flag overloads take over the throwing body, so a caller who passed
trueor left the flag out keeps working. Only a caller who passedfalseexplicitly (asking for the quiet end) breaks.ForEach(reader, handler, bool throwOnCancellation, token),IterateWhile(reader, predicate, bool throwOnCancellation, token), and the twoSelectAsyncoverloads that took the flag.Selectforms:Select(reader, transform, token)(token still required, soSelect(reader, transform)stays unambiguous) and its token-first twin.falsedefault returned a silent partial result; once cancellation that cuts a read short throws, the flag would be a lie.Every path follows the rule: the reader loops,
ForEachAsyncand theIterateWhileAsyncforms (with and withoutuseReadAsync), the command-levelIterateReaderAsync/IterateReaderWhileAsyncover a plain reader, and the plain-IDataReaderfallback of the mapped stream (which also fixes the analyzer finding CA2016). Behaviour changes to know about: a cancel that cuts the read short now throws where a partial result used to come back as if complete (the obsolete readerToList,ToListAsync(useReadAsync: false)and theToChannelreader forms), andForEachAsyncandIterateWhileAsyncnow throw for a cancel during the last row's handler. The migration notes list each one.Not changed (known limitation).
AsAsyncEnumerableand its cores still end quietly on a cancel. The migration notes say so.Selectwithout the flag, and its token-first twin), nothing else.foundation.md, new sample checks, and the migration notes.Checks
🤖 Generated with Claude Code