Repository navigation
Source: foreign IMappedQuery<T> terminals no longer resume on the caller's context - #153
Merged
Merged
Conversation
…ntext The generic fallback terminals of MappedQueryExtensions read a foreign IMappedQuery<T> through its ReadAsync handler and called the in-box System.Linq.AsyncEnumerable methods on the rows. Those await MoveNextAsync and DisposeAsync without ConfigureAwait(false), so the call resumed on the caller's SynchronizationContext. Each fallback terminal now enumerates with await foreach over WithCancellation(token).ConfigureAwait(false): ToListAsync, ToArrayAsync, both ToDictionaryAsync, ToHashSetAsync, FirstOrDefaultAsync, SingleOrDefaultAsync, FirstAsync and SingleAsync. Results, exceptions and messages are unchanged. No public API change. ForeignQueryContextTests: for every terminal, the result matches the in-box terminal over the same rows (empty, one, many, duplicates), a cancelled token behaves as the in-box terminal does, the enumerator is disposed once, and a gated MoveNextAsync or DisposeAsync never posts to the caller's context. 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.
What this changes
For a foreign
IMappedQuery<T>(an implementation that is not the library's own), the terminals fall back to a generic path inMappedQueryExtensions.cs:query.ReadAsync(async (items, token) => await items.ToListAsync(token).ConfigureAwait(false), ct). The.ConfigureAwait(false)covers only the outer await. On net10 and net11items.ToListAsyncanditems.FirstOrDefaultAsyncare the in-boxSystem.Linq.AsyncEnumerablemethods, and their own state machines awaitMoveNextAsyncandDisposeAsyncwithout it, so the terminal resumes on the caller'sSynchronizationContext. The library's rule is that it always awaits withConfigureAwait(false)and returning to the caller's context is the caller's call. This restores the rule.Found by. A test of the fallback written for the
Selectremoval (#152): a foreignIMappedQuery<int>with a gatedMoveNextAsyncorDisposeAsync, run under a countingSynchronizationContext, expected 0 posts and got 1. It is the behaviour on master today, not something that change introduced.The fix. Each fallback terminal now calls a small private helper that enumerates with
await foreach (var item in items.WithCancellation(token).ConfigureAwait(false)). The messages and exceptions the in-box terminals give are kept.items.ConfigureAwait(false)cannot be used, because it does not type-check as theIAsyncEnumerable<T>the LINQ terminal needs.The sweep. Nine terminals had the shape:
ToListAsync,ToArrayAsync, bothToDictionaryAsync,ToHashSetAsync,FirstOrDefaultAsync,SingleOrDefaultAsync,FirstAsync,SingleAsync.ToImmutableArrayAsyncalready had the loop and is unchanged (a test covers it). Nothing else inSource/has the shape. Two small consequences:ToHashSetAsyncloses its#if NETSTANDARD2_0split and uses one helper on every target (the set is created per call), andToArrayAsyncfills the existing internal array sink, so it still copies once.ForeignQueryContextTests, 140 cases per target framework: 40 compare each terminal with the in-box LINQ terminal over the same rows (empty, one, many, duplicates), 40 check a pre-cancelled token against the in-box behaviour, 60 check that a counting context receives no post with a gatedMoveNextAsyncorDisposeAsync. On unfixed master the new test class fails 54 of 140 on net10 and on net11 (all the no-post cases; theToImmutableArrayAsynccases pass), and it passes 140 of 140 after the fix.netstandardpath (theSystem.Linq.Asyncpackage): the test project targets net10 and net11, and the helpers are the same code on every target.Checks
🤖 Generated with Claude Code