You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please provide a succinct description of the issue.
MailboxProcessor agent did not receive message and run forever!
Expected behavior
After dispose agent, agent should stop.
Actual behavior
agent run forever.
Known workarounds
add some sleep time, it may be work.
Related information
Provide any related information (optional):
Windows 10
.NET Framework 4.7
Visual Studio 2022
// print text to consoleletprintText=letagent= MailboxProcessor.Start(fun inbox ->async{whiletruedolet!msg= inbox.Receive()
printfn "%s: %s"(System.DateTime.Now.ToLongTimeString()) msg
})
agent.Post
letteskMailboxLeak(withSleep)=use agent =new MailboxProcessor<int>(fun inbox ->async{let mutablei=0while i >=0dolet!(msg:int option)= inbox.TryReceive(2000)match msg with| None ->
sprintf "idle %d" i |> printText
i <- i +1| Some x ->
sprintf "receive %d" x |> printText
let!cts= Async.CancellationToken
if cts.IsCancellationRequested then
i <--1
printText "exit"})
agent.Start()
agent.Post(3)
agent.Post(4)
agent.Post(5)if withSleep then
System.Threading.Thread.Sleep(4000)//! with sleep, everything works as expected.//! without sleep//! the agent run forever! //! f# 4.7.2 can receive numbers, f# 8 or 9 can not!//! Is it normal?[<EntryPoint>]letmain argv =letwithSleep=false
printText ("start "+if withSleep then"with sleep"else"without sleep")
teskMailboxLeak(withSleep)//System.Threading.Thread.Sleep(5000)// force GCfor i =1to5do
sprintf "GC %d" i |> printText
System.GC.Collect(System.GC.MaxGeneration)|> ignore
System.GC.WaitForFullGCComplete()|> ignore
System.Threading.Thread.Sleep(1000)
System.Console.ReadLine()|> ignore
0// return an integer exit code
Yes. this is counterintuitive. MailboxProcessor does not hold any internal CancellationTokenSource that it can cancel on Dispose.
Internally it just does a Async.Start. I guess if there is no stopping condition in the body, it will run even after disposal, or wait forever on a disposed handle.
We could probably add cancellation on Dispose utilizing a linked token source. Would it be a breaking change?
Please provide a succinct description of the issue.
MailboxProcessor agent did not receive message and run forever!
Expected behavior
After dispose agent, agent should stop.
Actual behavior
agent run forever.
Known workarounds
add some sleep time, it may be work.
Related information
Provide any related information (optional):
log without sleep
log with sleep
The text was updated successfully, but these errors were encountered: