AsyncBridge icon indicating copy to clipboard operation
AsyncBridge copied to clipboard

(net35) System.Threading polyfill dependency incorrectly flows SynchronizationContext

Open jnm2 opened this issue 6 years ago • 3 comments

For reference, this demonstrates the bug: https://github.com/jnm2/AsyncBridge/compare/sync_context_flow_bug

This differs from the .NET Framework 4.5 behavior. Example of what can go wrong:

// UI thread
await Task.Run(async () =>
{
    // Thread pool thread
    DoCPUIntensiveWork();
    await DoIOBoundWorkAsync();
    // PROBLEM: the await resumes on the UI thread, not a thread pool thread!
    DoCPUIntensiveWork(); // Ends up blocking the UI thread
});

The workaround is to await DoIOBoundWorkAsync().ConfigureAwait(false). (Once v0.3.0 is out with the fix for https://github.com/OmerMor/AsyncBridge/issues/7!) To be clear, ConfigureAwait(false) is better code, but it should still not be necessary.

Looks like another easy fix. The problem is, the source code for TaskParallelLibrary 1.0.2856 is not on the internet as far as I can tell. The nupkg and binaries are first committed to this repo in https://github.com/OmerMor/AsyncBridge/commit/f97269a5679dd1d85565f52dbcdd3522fc71e62c and had been downloaded from https://www.nuget.org/packages/rx-core/1.0.2856. I've scoured https://rx.codeplex.com/ and https://github.com/Reactive-Extensions/Rx.NET and am pretty sure they do not contain the source of System.Threading.dll from TaskParallelLibrary.

At this point it seems likely that creating a fresh net47-net35 or perhaps corefx/net35 shim from scratch in a single DLL would be cleanest.

jnm2 avatar Jan 03 '18 05:01 jnm2

Recommend closing all TaskParallelLibrary 1.0.2856 bugs as won't fix with a known workaround. No one has binding redirects in place for this and trying to re-port this is going to be a mess.

sharwell avatar May 28 '18 23:05 sharwell

There's another reason to re-port: net20 support. There is a measurable difference between running against the net20 BCL and the net35 and net45 BCLs, and we at NUnit have not been excited about dropping support for old targets, so I'm actually looking for this at the moment.

jnm2 avatar May 28 '18 23:05 jnm2

I have TaskParallelLibrary replaced with .NET Core 2.1 code which enabled me to fix this bug and add a net20 build in https://github.com/OmerMor/AsyncBridge/compare/no_dependencies. Planning for some minor API cleanup, so this will be version 0.4.

jnm2 avatar Jun 02 '18 23:06 jnm2