Skip to content

Commit f97f274

Browse files
fix(python): make MailboxProcessor disposable
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.
1 parent 9d7f2cb commit f97f274

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎src/fable-library-py/fable_library/mailbox_processor.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ def __process_events(self) -> None:
162162
if cont:
163163
cont[0](msg)
164164

165+
def Dispose(self) -> None:
166+
"""Like .NET, disposing neither stops the agent nor drops its messages.
167+
168+
.NET only releases a wait handle, which has no counterpart here.
169+
"""
170+
165171
@classmethod
166172
def start(
167173
cls,

‎tests/Python/TestMailboxProcessor.fs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ let ``test MailboxProcessor.post works`` () =
4545
equal (Some 2) res // Mailbox has finished
4646
} |> Async.StartImmediate
4747

48+
[<Fact>]
49+
let ``test MailboxProcessor can be disposed`` () =
50+
async {
51+
let mutable res = None
52+
do! async {
53+
use agent = MailboxProcessor<int>.Start(fun inbox -> async {
54+
let! msg = inbox.Receive()
55+
res <- Some msg
56+
})
57+
agent.Post(1)
58+
do! Async.Sleep 200
59+
}
60+
equal (Some 1) res
61+
let agent = new MailboxProcessor<int>(fun _ -> async.Zero())
62+
(agent :> IDisposable).Dispose()
63+
} |> Async.StartImmediate
64+
4865
// PostAndAsyncReply
4966

5067
[<Fact>]

0 commit comments

Comments
 (0)