dotnet / dotnet/fsharp

Cannot wait on monitors on this runtime

Open
#19,110 5 comments 0 reactions 0 assignees View on GitHub
Bug Needs-Triage
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

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:

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

Exception detail:

```text
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 Startup.OnInitializedAsync@14.MoveNext((index))
```

**Repro steps**

I created a repo to reproduce this: [repro](https://github.com/albertwoo/FsharpWasmForTaskIssue)

**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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.