Repository navigation
Every async terminal that was missing one takes a CancellationToken - #110
Merged
Merged
Conversation
…th the command's and releases the linked source Internal and unused until the terminals take a token. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ExecuteNonQueryAsync, ExecuteScalarAsync, ExecuteReturnAsync, ExecuteReaderAsync and IterateReader*Async take a CancellationToken as their last parameter. It is used together with the command's own token; either cancels. Code compiled against rc.3 needs a recompile; source stays compatible. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… allocated per call The core still guards the writer, with its own parameter name, as before. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A method group converted to a delegate (four places) becomes a lambda, as the optional token parameter makes the conversion impossible. The xUnit1051 warnings are answered with TestContext.Current.CancellationToken, or with a scoped pragma where the test is about which token a call is given. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…; say so to the analyzer Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…asured twice Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…el overloads on the small command, not the big fake reader Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…d throw on the Channel ValueTask overloads is documented and tested, and one comment is indented Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… measured and the slack Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, => on its own line, doc comments wrapped at natural breaks Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…E0011 asks Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.
The command's own Async terminals,
IterateReader*Async, and the Channel and Dataflow overloads on anIExecuteReaderignored a token the caller had in hand; only the command'sUseCancellationTokentoken applied. They now takeCancellationToken cancellationToken = defaultas the last parameter. The token is used together with the command's own: either one cancels.UseCancellationTokenis unchanged. The parameter is optional on purpose, so the CA2016 analyzer flags a caller who has a token and does not pass it.How the tokens combine
What changed
Core (15 members, plus 4 new overloads):
ExecuteNonQueryAsync,ExecuteScalarAsync(4 forms),ExecuteReturnAsync(2 forms),ExecuteReaderAsync(5 forms).IterateReaderAsyncandIterateReaderWhileAsync(3 members: oneIterateReaderAsync, twoIterateReaderWhileAsync).ExecuteReaderAsync(handler, cancellationToken)for the four handler shapes, because with the token last,ExecuteReaderAsync(handler, token)would not compile.ExecuteReaderAsync(handler, default)now binds to it;defaultis an empty token and the default behavior, so the call does the same.Channel (20):
ToChannelandToChannelAsyncon anIExecuteReader/IExecuteReaderAsync, both theValueTask<long>forms and theChannelReader-returning forms.Dataflow (10):
AsSourceBlockAsyncandToTargetBlockAsyncon anIExecuteReader.Where an optional parameter comes before the token (
behavior,options,arrayPool,map), pass the token by name (cancellationToken: token) or pass the optional ones too.cmd.AsSourceBlockAsync(token)does not bind, as with theIDbCommandoverloads.Same signature, two meanings
ExpressiveCommandBase.ExecuteReaderAsync(state, transform, behavior, cancellationToken)combines the token with the command's. The member of the same shape on theIExecuteReaderinterface keeps its contract: through the interface the token replaces the command's own, because the caller has already combined them. The interface members that have no token still use the command's own token. A test pins both.Compatibility
Func<ChannelWriter<object[]>, bool, ValueTask<long>> f = cmd.ToChannel) no longer compiles, because of the optional parameter. Use a lambda. This is a source break for that rare pattern. Four places in the tests did it and now use a lambda.defaultas the last argument binds differently where the new token overloads sit beside anArrayPooloverload.ToChannelAsync(cmd, writer, true, default)used to bind the nullable-ArrayPooloverload and now binds the token one: the same call.ToChannel(cmd, writer, false, default)used to bind the non-nullArrayPooloverload and threwArgumentNullException; it now binds the token overload and runs. A test pins both.ValueTask<long>Channel overloads (ToChannel,ToChannelAsync, plain and mapped) now throwsArgumentNullExceptionat the call, instead of returning a faulted task. A null target is still reported through the returned task.Allocation
Passing a token must not cost an allocation. A test per family (Core terminals,
IterateReader*Async, Channel) measures the bytes one call allocates on the calling thread, for a call with no token, one call token, one command token, and the same token on both, and asserts each is no more than the command token alone always allocated (slack 16 bytes). The mapped Channel overloads and Dataflow hop threads (a pipeline of their own,Task.Yield), which the per-thread counter cannot see, so they are not measured.Tests
ChannelReader-returning overload is tested with a token canceled while reading, which completes the channel as canceled. A token that is already canceled before the read starts leaves such a channel open today (it is reachable withUseCancellationTokentoo); that is not changed here.TestContext.Current.CancellationToken, or with a scoped pragma where the test is about which token a call is given.Checks
TokenTerminalTests(200 cases).!, which is fixed.=>on its own line, doc comments wrapped at natural breaks. The style analyzer (IDE0011) then asked for braces on multi-lineif/else/foreachbodies, inIExecuteReader.cs,ExecuteReaderEdgeTests.csandExecuteScalarNullTests.cs; each brace pair encloses the single statement it already governed. Every changed file was compared with its previous version: the code, with whitespace removed, and the comment text are unchanged apart from those braces.UsingAsynchas no token: it creates the connection without opening it and its action receives no token, so a token there could only stop a call that is already canceled. A parameter that does almost nothing would be misleading.If the AddParam-cost branch has not merged when this does, a later merge may conflict around the parameter-adding code in
ExpressiveCommandBase.cs; this change touches the terminals and the code next toExecuteCoreAsync.🤖 Generated with Claude Code