Skip to content

[RuntimeAsync] Awaiting a configured ValueTaskSource may end up running in a wrong context. #120709

Closed

Description

@VSadov

ValueTaskSource can be configured to not run continuations in the current context (or scheduler).
That takes precedence over what awaiter wants.

For example an IO.Pipe can be configured to run callbacks/continuations in default/threadpool context. In such case immediate users of the IO.Pipe do not need to do ConfigureAwait(false).

Runtime async always considers what awaiter wants. In this case it results in testcase hangs infinitely.

Activity

  1. self-assigned this
    on Oct 14, 2025
  2. dotnet-policy-service commented on Oct 14, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @mangod9
    See info in area-owners.md if you want to be subscribed.

  3. added
    untriaged
    on Oct 14, 2025
  4. VSadov commented on Oct 14, 2025

    @VSadov
    MemberAuthor

    An example of affected testcase.

            [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
            public async Task UseSynchronizationContextFalseIgnoresSyncContextForReaderScheduler()
            {
                SynchronizationContext previous = SynchronizationContext.Current;
                var sc = new CustomSynchronizationContext();
                try
                {
                    SynchronizationContext.SetSynchronizationContext(sc);
    
                    var pipe = new Pipe(new PipeOptions(useSynchronizationContext: false));
    
                    Func<Task> doRead = async () =>
                    {
                        ReadResult result = await pipe.Reader.ReadAsync();
    
                        pipe.Reader.AdvanceTo(result.Buffer.End, result.Buffer.End);
    
                        pipe.Reader.Complete();
                    };
    
                    // This needs to run on the current SynchronizationContext
                    Task reading = doRead();
    
                    PipeWriter buffer = pipe.Writer;
                    buffer.Write("Hello World"u8.ToArray());
    
                    // Don't run code on our sync context (we just want to make sure the callbacks)
                    // are scheduled on the sync context
                    await buffer.FlushAsync().ConfigureAwait(false);
    
                    // Nothing posted to the sync context
                    Assert.Equal(0, sc.Callbacks.Count);
    
                    pipe.Writer.Complete();
    
                    // Don't run code on our sync context
                    await reading.ConfigureAwait(false);
                }
                finally
                {
                    SynchronizationContext.SetSynchronizationContext(previous);
                }
            }

    The part

                        ReadResult result = await pipe.Reader.ReadAsync();

    should behave as

                        ReadResult result = await pipe.Reader.ReadAsync().ConfigureAwait(false);

    Otherwise we end up posting the doRead continuation to the tracking CustomSynchronizationContext which only tracks Posts and does not run them, thus the test makes no progress.

  5. added this to the 11.0.0 milestone on Oct 15, 2025
  6. removed
    untriaged
    on Oct 15, 2025
  7. added a commit that references this issue on Nov 7, 2025
    704d429
  8. locked and limited conversation to collaborators on Dec 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions