Skip to content

fix(js/ts/python): make MailboxProcessor disposable - #5364

Merged
MangelMaxime merged 7 commits into
fable-compiler:mainfrom
leonitousconforti:support-mailbox-dispose
Dec 2, 2026
Merged

MangelMaxime merged 7 commits into
fable-compiler:mainfrom
leonitousconforti:support-mailbox-dispose

Conversation

@leonitousconforti

@leonitousconforti leonitousconforti commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

MailboxProcessor<'Msg> implements IDisposable on .NET. On JS/TS and Python, the class has no Dispose:

  • use agent = MailboxProcessor.Start(...) in async fails at the end of the scope, with resource.Dispose is not a function on JS and AttributeError on Python.
  • Fable does not support a direct agent.Dispose() call.

Dispose now does what FSharp.Core 11 does:

  • It removes the messages that are in the queue.
  • The mailbox ignores a Post or PostAndAsyncReply that comes after Dispose. A PostAndAsyncReply after Dispose never completes.
  • If you create the mailbox with isThrowExceptionAfterDisposed = true, a post after Dispose throws ObjectDisposedException.
  • The agent continues to run.

Other changes:

  • The isThrowExceptionAfterDisposed overloads of MailboxProcessor and MailboxProcessor.Start work. Before, Fable gave the bool to the library as the cancellation token.
  • New ObjectDisposedException on JS/TS and Python, with its 3 public constructors and ObjectName.
  • InvalidOperationException has a (message, innerException) constructor, because ObjectDisposedException inherits from it. On Python, InvalidOperationException.__init__ now takes 2 arguments. This changes only hand-written Python code or Emit code that calls it.
  • The Python MailboxProcessor tests use Async.RunSynchronously. Before, they used Async.StartImmediate, and the test ended before the assertions ran.

Beam is not changed: its MailboxProcessor is in Erlang. Dart and Rust do not support MailboxProcessor.

leonitousconforti and others added 7 commits December 2, 2026 08:50
MailboxProcessor implements IDisposable on .NET, but the class had no
Dispose, so `use agent = ...` inside an async workflow failed with
"resource.Dispose is not a function" and an explicit Dispose threw.
Dispose does nothing, as on .NET, where it only releases a wait handle:
the agent keeps running and keeps its messages.
Add the Dispose that .NET's IDisposable MailboxProcessor has, so `use`
inside an async workflow and an explicit Dispose no longer fail with
AttributeError. Like .NET, it neither stops the agent nor drops its
messages.
@MangelMaxime
MangelMaxime force-pushed the support-mailbox-dispose branch from f97f274 to abd12d0 Compare December 2, 2026 08:02
@MangelMaxime
MangelMaxime merged commit 306438b into fable-compiler:main Dec 2, 2026
39 checks passed
@MangelMaxime

Copy link
Copy Markdown
Member

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants