fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

AsyncLocal diagnostics, clean slate

Open majocha opened this issue 1 year ago • 10 comments

We used node CE, a customized async CE variant to ensure DiagnosticsThreadStatics flow from thread to thread along with the execution of asynchronous compiler tasks.

AsyncLocal<'T> achieves this without the need for a specialized CE.

from documetation:

AsyncLocal<T> Class Represents ambient data that is local to a given asynchronous control flow, such as an asynchronous method.

This allows us to just remove NodeCode and replace node expressions with async.

Tests show AsyncLocal works as expected in scenarios involving task, async, Async.SwitchToNewThread() SwitchToThreadPool(), Thread.Start() as well as TPL parallel execution.

  • [x] Added concurrent diagnostics logging for running many computations:
  • MultipleDiagnosticsLoggers.Parallel generally for use in place of NodeCode.Parallel. Replays diagnostics onto caller's logger preserving the given order of computations.
  • MultipleDiagnosticsLoggers.Sequential replaces NodeCode.Parallel. Unlike Async.Sequential it starts immediately on the same thread so It automatically uses caller's diagnostics context.
  • [x] Added tests for AsyncLocal diagnostics flow in async and sync contexts.

Previous attempts: #16602, #16645

majocha avatar Feb 28 '24 07:02 majocha

:heavy_exclamation_mark: Release notes required


:white_check_mark: Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/8.0.400.md
vsintegration/src docs/release-notes/.VisualStudio/17.11.md

github-actions[bot] avatar Feb 28 '24 07:02 github-actions[bot]

Might shed some light on what's going on here with the deadlocks: https://github.com/dotnet/fsharp/pull/12638

Also: https://devblogs.microsoft.com/premier-developer/the-danger-of-taskcompletionsourcet-class/

And maybe informative, since both GraphNode and AsyncMemoize that we have are variations on AsyncLazy:

https://github.com/dotnet/roslyn/blob/01b7c233fdda946c1a5628d4692ed827a07e33dd/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/AsyncLazy%601.cs#L525-L530

    // We want to always run continuations asynchronously. Running them synchronously could result in deadlocks:
    // if we're looping through a bunch of Requests and completing them one by one, and the continuation for the
    // first Request was then blocking waiting for a later Request, we would hang. It also could cause performance
    // issues. If the first request then consumes a lot of CPU time, we're not letting other Requests complete that
    // could use another CPU core at the same time.
    public Request() : base(TaskCreationOptions.RunContinuationsAsynchronously)

Looking at the source of Async.AwaitTask, It's also not clear to me, whether we respect TaskCreationOptions.RunContinuationsAsynchronously flag. It seems to work, but is it deliberate or by accident?

majocha avatar Mar 11 '24 12:03 majocha

Still a draft, or ready for review?

T-Gro avatar Apr 29 '24 11:04 T-Gro

This was kinda put on hold as a risky change IIRC. I can try to revive it but it would require a really careful look.

majocha avatar Apr 29 '24 16:04 majocha

/run fantomas

psfinaki avatar Apr 30 '24 12:04 psfinaki

Failed to run fantomas: https://github.com/dotnet/fsharp/actions/runs/8894616544

github-actions[bot] avatar Apr 30 '24 12:04 github-actions[bot]

I added one more test to make sure it works with ListParallel. (In use with graph-based type checking).

majocha avatar Apr 30 '24 13:04 majocha

/run fantomas

psfinaki avatar May 02 '24 13:05 psfinaki

Failed to run fantomas: https://github.com/dotnet/fsharp/actions/runs/8924428488

github-actions[bot] avatar May 02 '24 13:05 github-actions[bot]

I think this is good for a review now.

majocha avatar May 09 '24 18:05 majocha

@majocha i saw reports from @ForNeVeR that some of the changes result in the deadlock

vzarytovskii avatar May 21 '24 18:05 vzarytovskii

ah, ok I'm seeing the issue now.

majocha avatar May 21 '24 18:05 majocha