dotnet / dotnet/fsharp

Passing a CancellationToken into a MailboxProcessor breaks them when they are initialized in a Blazor component.

Open
#15,272 4 comments 0 reactions 0 assignees View on GitHub
Area-Library Bug Impact-Low
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

I have a Blazor component which looks like this:

```fs
type ViewComponent() as this =
inherit ComponentBase()

let token_source = new CancellationTokenSource()
let cancellation_token = token_source.Token

let mb init update =
let mb =
new MailboxProcessor<_>(fun mb -> async {
let mutable model = init
while true do
let! msg = mb.Receive()
model <- update msg model mb.Post
},cancellation_token) // Removing the cancellation token makes it work.
mb.Start()
mb

let srv = mb () (fun x () _ -> printfn "%s" x)

do srv.Post("Hello from view")
do this.Say("123456789")

member _.Say(x) = srv.Post x
```

And I have a Blazor index page with a button. It inherits from it.

```razor
@page "/"

@inherits Fun.ViewComponent

Click me.

@code
{
private void Hello()
{
Say("qwe");
}
}

```

**Repro steps**

Run the project and click on the button.

**Expected behavior**

Look in the browser console. `Hello from view` will get printed, but `qwe` won't when you click on it. Neither will `123456789`

**Actual behavior**

I expected all of them would be printed.

**Known workarounds**

Remove the cancellation token. Then the mailbox works as expected.

**Related information**

Provide any related information (optional):

* Operating system: Win 10
* .NET Runtime kind (.NET Core, .NET Framework, Mono): .NET 7.0.302 & 8.0.100-preview.3.23178.7

Min repro: https://github.com/mrakgr/CFR-In-Fable/tree/mailbox_bug_dotnet7
Min repro: https://github.com/mrakgr/CFR-In-Fable/tree/mailbox_bug_dotnet8_preview3

I've verified that this issue is present in the latest .NET 7 and .NET 8 Preview 3.

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.