I wonder, why MailboxProcessor's default strategy of handling exceptions is just silently ignore them. For example:
let counter =
MailboxProcessor.Start(fun inbox ->
let rec loop() =
async { printfn "waiting for data..."
let! data = inbox.Receive()
failwith "fail" // simulate throwing of an exception
printfn "Got: %d" data
return! loop()
}
loop ())
()
counter.Post(42)
counter.Post(43)
counter.Post(44)
Async.Sleep 1000 |> Async.RunSynchronously
and nothing happens. There is no fatal stop of the program execution, or message box with "An unhandled exception" arises. Nothing.
This situation becomes worse if someone uses PostAndReply method: a guaranteed deadlock as the result.
Any reasons for such behavior?
I think the reason why the
MailboxProcessorin F# does not contain any mechanism for handling exceptions is that it is not clear what is the best way for doing that. For example, you may want to have a global event that is triggered when an unhandled exception happens, but you may want to rethrow the exception on the next call toPostorPostAndReply.Both of the options can be implemented based on the standard
MailboxProcessor, so it is possible to add the behaviour you want. For example, the following snippet showsHandlingMailboxthat adds a global exception handler. It has the same interface as normalMailboxProcessor(I omitted some methods), but it addsOnErrorevent that is triggered when an exception happens:To use it, you would write the same code as what you wrote before, but you can now handle exceptions asynchronously: