fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Cannot wait on monitors on this runtime

Open albertwoo opened this issue 1 month ago • 5 comments

I am using fsharp WASM. Before dotnet10, the development is ok, but after update to dotnet 10. I got the issue "Cannot wait on monitors on this runtime" for below code:

    override _.OnInitializedAsync() = task {
        for i in 1..3 do
            do! Task.Delay 1000
            count <- count + i
    }

Exception detail:

blazor.webassembly.js:1 
 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Cannot wait on monitors on this runtime.
System.PlatformNotSupportedException: Cannot wait on monitors on this runtime.
   at System.Threading.Monitor.ObjWait(:3043/Int32 millisec…Timeout, Object obj)
   at System.Threading.Monitor.Wait(:3043/Object obj, In…millisecondsTimeout)
   at System.Threading.ManualResetEventSlim.Wait(:3043/Int32 millisec…n cancellationToken)
   at System.Threading.Tasks.Task.SpinThenBlockingWait(:3043/Int32 millisec…n cancellationToken)
   at System.Threading.Tasks.Task.InternalWaitCore(:3043/Int32 millisec…n cancellationToken)
   at System.Threading.Tasks.Task.InternalWait(:3043/Int32 millisec…n cancellationToken)
   at [email protected]((index))

Repro steps

I created a repo to reproduce this: repro

Expected behavior

Should works as before

Known workarounds

I found if I put a do! Task.Yield() in the for loop, then this issue will gone. Because for csharp it works fine, so I think it is related to fsharp task.

Related information

Provide any related information (optional):

  • Operating system: Windows11
  • .NET Runtime kind: .NET Core

albertwoo avatar Nov 21 '25 05:11 albertwoo

What was this projects TargetFramework before? Did you update your SDK to dotnet10, TargetFramework to dotnet10, or both?

In particular, does having TargetFramework=9 while keeping your net10 SDK work?

(the version of the compiler is dictated by the SDK you are having, no matter what you target. The TargetFramework then dictates what versions of .NET libraries your project gets)

T-Gro avatar Nov 21 '25 09:11 T-Gro

To add some context:

https://github.com/dotnet/runtime/issues/53228#issuecomment-896651769

but also

https://github.com/dotnet/runtime/issues/61308

majocha avatar Nov 21 '25 09:11 majocha

I found if I put a do! Task.Yield() in the for loop, then this issue will gone. Because for csharp it works fine, so I think it is related to fsharp task.

Removing the for loop also works.

majocha avatar Nov 21 '25 10:11 majocha

Should there be an alternative builder for WASM that avoids problematic API calls? A library replacing the built in one?

T-Gro avatar Nov 21 '25 10:11 T-Gro

What was this projects TargetFramework before? Did you update your SDK to dotnet10, TargetFramework to dotnet10, or both?

In particular, does having TargetFramework=9 while keeping your net10 SDK work?

(the version of the compiler is dictated by the SDK you are having, no matter what you target. The TargetFramework then dictates what versions of .NET libraries your project gets)

@T-Gro I updated from dotnet 6 to dotnet10 both for SDK and target framework. I also can confirm that dotnet9 is also working, because I have other similar project using that.

albertwoo avatar Nov 21 '25 10:11 albertwoo