Skip to content

Source: foreign IMappedQuery<T> terminals no longer resume on the caller's context - #153

Merged
electricessence merged 2 commits into
masterfrom
fix/foreign-query-configureawait
Nov 16, 2026
Merged

electricessence merged 2 commits into
masterfrom
fix/foreign-query-configureawait

Conversation

@electricessence

Copy link
Copy Markdown
Collaborator

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 in MappedQueryExtensions.cs: query.ReadAsync(async (items, token) => await items.ToListAsync(token).ConfigureAwait(false), ct). The .ConfigureAwait(false) covers only the outer await. On net10 and net11 items.ToListAsync and items.FirstOrDefaultAsync are the in-box System.Linq.AsyncEnumerable methods, and their own state machines await MoveNextAsync and DisposeAsync without it, so the terminal resumes on the caller's SynchronizationContext. The library's rule is that it always awaits with ConfigureAwait(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 Select removal (#152): a foreign IMappedQuery<int> with a gated MoveNextAsync or DisposeAsync, run under a counting SynchronizationContext, 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 the IAsyncEnumerable<T> the LINQ terminal needs.

The sweep. Nine terminals had the shape: ToListAsync, ToArrayAsync, both ToDictionaryAsync, ToHashSetAsync, FirstOrDefaultAsync, SingleOrDefaultAsync, FirstAsync, SingleAsync. ToImmutableArrayAsync already had the loop and is unchanged (a test covers it). Nothing else in Source/ has the shape. Two small consequences: ToHashSetAsync loses its #if NETSTANDARD2_0 split and uses one helper on every target (the set is created per call), and ToArrayAsync fills the existing internal array sink, so it still copies once.

  • Public API: unchanged (the PublicAPI files are untouched). The internal row-source and sink seams are untouched.
  • Tests: a new 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 gated MoveNextAsync or DisposeAsync. On unfixed master the new test class fails 54 of 140 on net10 and on net11 (all the no-post cases; the ToImmutableArrayAsync cases pass), and it passes 140 of 140 after the fix.
  • Not covered: the netstandard path (the System.Linq.Async package): the test project targets net10 and net11, and the helpers are the same code on every target.

Checks

  • Release build with 0 warnings on every target framework; the public-API analyzer reports nothing.
  • The full Core test project passes (10,439 tests) on net10.0 and net11.0; the legacy-compiler tests pass on net472, net8.0, net10.0 and net11.0.
  • The full test gate and a Visual Studio MSBuild of the solution are green; leak scan clean.

🤖 Generated with Claude Code

electricessence and others added 2 commits November 15, 2026 17:10
…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>
@electricessence
electricessence marked this pull request as ready for review November 16, 2026 01:42
@electricessence
electricessence merged commit 3450bf0 into master Nov 16, 2026
7 checks passed
@electricessence
electricessence deleted the fix/foreign-query-configureawait branch November 16, 2026 01:43
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.

1 participant