Skip to content

Commit abd12d0

Browse files
committed
fix(js/ts/python): support isThrowExceptionAfterDisposed overloads
1 parent 9eabd07 commit abd12d0

7 files changed

Lines changed: 174 additions & 29 deletions

File tree

‎src/Fable.Transforms/Python/Replacements.fs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3829,6 +3829,8 @@ let observable (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Exp
38293829
let mailbox (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
38303830
match thisArg with
38313831
| None ->
3832+
let args, argTypes = mailboxArgs i args
3833+
38323834
match i.CompiledName with
38333835
| ".ctor" ->
38343836
Helper.LibCall(
@@ -3837,13 +3839,13 @@ let mailbox (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr opt
38373839
"MailboxProcessor",
38383840
t,
38393841
args,
3840-
i.SignatureArgTypes,
3842+
argTypes,
38413843
isConstructor = true,
38423844
?loc = r
38433845
)
38443846
|> Some
38453847
| "Start" ->
3846-
Helper.LibCall(com, "mailbox_processor", "start", t, args, i.SignatureArgTypes, ?loc = r)
3848+
Helper.LibCall(com, "mailbox_processor", "start", t, args, argTypes, ?loc = r)
38473849
|> Some
38483850
| _ -> None
38493851
| Some callee ->

‎src/Fable.Transforms/Replacements.Util.fs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,18 @@ let (|BuiltinSystemException|_|) (ent: string) =
378378
Some entName
379379
| _ -> None
380380

381+
// isThrowExceptionAfterDisposed follows cancellationToken in the library signatures
382+
let mailboxArgs (i: CallInfo) (args: Expr list) =
383+
match args, i.SignatureArgTypes with
384+
| body :: isThrow :: rest, bodyType :: (Boolean as isThrowType) :: restTypes ->
385+
let token, tokenType =
386+
match rest, restTypes with
387+
| token :: _, tokenType :: _ -> token, tokenType
388+
| _ -> Value(NewOption(None, Any, false), None), Option(Any, false)
389+
390+
[ body; token; isThrow ], [ bodyType; tokenType; isThrowType ]
391+
| _ -> args, i.SignatureArgTypes
392+
381393
let getElementType =
382394
function
383395
| Array(t, _) -> t

‎src/Fable.Transforms/Replacements.fs‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5117,30 +5117,14 @@ let observable (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Exp
51175117
let mailbox (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
51185118
match thisArg with
51195119
| None ->
5120+
let args, argTypes = mailboxArgs i args
5121+
51205122
match i.CompiledName with
51215123
| ".ctor" ->
5122-
Helper.LibCall(
5123-
com,
5124-
"MailboxProcessor",
5125-
"default",
5126-
t,
5127-
args,
5128-
i.SignatureArgTypes,
5129-
isConstructor = true,
5130-
?loc = r
5131-
)
5124+
Helper.LibCall(com, "MailboxProcessor", "default", t, args, argTypes, isConstructor = true, ?loc = r)
51325125
|> Some
51335126
| "Start" ->
5134-
Helper.LibCall(
5135-
com,
5136-
"MailboxProcessor",
5137-
"start",
5138-
t,
5139-
args,
5140-
i.SignatureArgTypes,
5141-
genArgs = i.GenericArgs,
5142-
?loc = r
5143-
)
5127+
Helper.LibCall(com, "MailboxProcessor", "start", t, args, argTypes, genArgs = i.GenericArgs, ?loc = r)
51445128
|> Some
51455129
| _ -> None
51465130
| Some callee ->

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
protected_cont,
1717
)
1818
from .option import erase, some
19-
from .system import TimeoutException__ctor_Z721C83C5
19+
from .system import ObjectDisposedException__ctor_Z721C83C5, TimeoutException__ctor_Z721C83C5
2020

2121

2222
class AsyncReplyChannel[Reply]:
@@ -32,6 +32,7 @@ def __init__(
3232
self,
3333
body: Callable[[MailboxProcessor[Msg]], Async[None]],
3434
cancellation_token: CancellationToken | None = None,
35+
is_throw_exception_after_disposed: bool = False,
3536
):
3637
self.messages: SimpleQueue[Msg] = SimpleQueue()
3738
self.token = cancellation_token or CancellationToken()
@@ -41,6 +42,7 @@ def __init__(
4142
# Holds the continuation i.e the `done` callback of Async.from_continuations returned by `receive`.
4243
self.continuation: Continuations[Any] | None = None
4344
self.is_disposed = False
45+
self.is_throw_exception_after_disposed = is_throw_exception_after_disposed
4446

4547
def post(self, msg: Msg) -> None:
4648
"""Post a message synchronously to the mailbox processor.
@@ -228,6 +230,8 @@ def __process_events(self) -> None:
228230
def __enqueue(self, msg: Msg) -> None:
229231
with self.lock:
230232
if self.is_disposed:
233+
if self.is_throw_exception_after_disposed:
234+
raise ObjectDisposedException__ctor_Z721C83C5("Mailbox")
231235
return
232236
self.messages.put(msg)
233237

@@ -242,8 +246,9 @@ def start(
242246
cls,
243247
body: Callable[[MailboxProcessor[Msg]], Async[None]],
244248
cancellation_token: CancellationToken | None = None,
249+
is_throw_exception_after_disposed: bool = False,
245250
) -> MailboxProcessor[Msg]:
246-
mbox: MailboxProcessor[Msg] = MailboxProcessor(body, cancellation_token)
251+
mbox: MailboxProcessor[Msg] = MailboxProcessor(body, cancellation_token, is_throw_exception_after_disposed)
247252
start_immediate(body(mbox), mbox.token)
248253
return mbox
249254

@@ -274,8 +279,9 @@ def start_instance(mbox: MailboxProcessor[Any]) -> None:
274279
def start[Msg](
275280
body: Callable[[MailboxProcessor[Msg]], Async[None]],
276281
cancellationToken: CancellationToken | None = None,
282+
is_throw_exception_after_disposed: bool = False,
277283
) -> MailboxProcessor[Msg]:
278-
mbox = MailboxProcessor(body, cancellationToken)
284+
mbox = MailboxProcessor(body, cancellationToken, is_throw_exception_after_disposed)
279285
start_instance(mbox)
280286
return mbox
281287

‎src/fable-library-ts/MailboxProcessor.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Continuation, Continuations } from "./AsyncBuilder.ts";
66
import { CancellationToken } from "./AsyncBuilder.ts";
77
import type { Option } from "./Option.ts";
88
import { some } from "./Option.ts";
9-
import { TimeoutException_$ctor_Z721C83C5 } from "./System.ts";
9+
import { ObjectDisposedException_$ctor_Z721C83C5, TimeoutException_$ctor_Z721C83C5 } from "./System.ts";
1010
import { Exception, IDisposable } from "./Util.ts";
1111

1212
class QueueCell<Msg> {
@@ -60,10 +60,12 @@ export class MailboxProcessor<Msg> implements IDisposable {
6060

6161
public continuation?: Continuation<Msg>;
6262
public isDisposed = false;
63+
public isThrowExceptionAfterDisposed: boolean;
6364

64-
constructor(body: MailboxBody<Msg>, cancellationToken?: CancellationToken) {
65+
constructor(body: MailboxBody<Msg>, cancellationToken?: CancellationToken, isThrowExceptionAfterDisposed = false) {
6566
this.body = body;
6667
this.cancellationToken = cancellationToken || defaultCancellationToken;
68+
this.isThrowExceptionAfterDisposed = isThrowExceptionAfterDisposed;
6769
this.messages = new MailboxQueue<Msg>();
6870
}
6971

@@ -76,6 +78,8 @@ export class MailboxProcessor<Msg> implements IDisposable {
7678
function __enqueue<Msg>($this: MailboxProcessor<Msg>, message: Msg) {
7779
if (!$this.isDisposed) {
7880
$this.messages.add(message);
81+
} else if ($this.isThrowExceptionAfterDisposed) {
82+
throw ObjectDisposedException_$ctor_Z721C83C5("Mailbox");
7983
}
8084
}
8185

@@ -187,8 +191,12 @@ export function postAndAsyncReply<Reply, Msg>(
187191
});
188192
}
189193

190-
export function start<Msg>(body: MailboxBody<Msg>, cancellationToken?: CancellationToken) {
191-
const mbox = new MailboxProcessor(body, cancellationToken);
194+
export function start<Msg>(
195+
body: MailboxBody<Msg>,
196+
cancellationToken?: CancellationToken,
197+
isThrowExceptionAfterDisposed = false,
198+
) {
199+
const mbox = new MailboxProcessor(body, cancellationToken, isThrowExceptionAfterDisposed);
192200
startInstance(mbox);
193201
return mbox;
194202
}

‎tests/Js/Main/AsyncTests.fs‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,71 @@ let tests =
10471047
equal false replied
10481048
}
10491049

1050+
testCaseAsync "MailboxProcessor.Start with isThrowExceptionAfterDisposed works" <| fun () ->
1051+
async {
1052+
let log = ResizeArray<int>()
1053+
let agent = MailboxProcessor<int>.Start((fun inbox -> async {
1054+
while true do
1055+
let! msg = inbox.Receive()
1056+
log.Add msg }), true)
1057+
agent.Post 1
1058+
do! Async.Sleep 200
1059+
equal [|1|] (log.ToArray())
1060+
agent.Dispose()
1061+
agent.Dispose()
1062+
let thrown = try agent.Post 2; false with :? ObjectDisposedException -> true
1063+
equal true thrown
1064+
do! Async.Sleep 100
1065+
equal [|1|] (log.ToArray())
1066+
}
1067+
1068+
testCaseAsync "MailboxProcessor constructor with isThrowExceptionAfterDisposed works" <| fun () ->
1069+
async {
1070+
let log = ResizeArray<int>()
1071+
let agent = new MailboxProcessor<int>((fun inbox -> async {
1072+
while true do
1073+
let! msg = inbox.Receive()
1074+
log.Add msg }), false)
1075+
agent.Start()
1076+
agent.Post 1
1077+
do! Async.Sleep 200
1078+
agent.Dispose()
1079+
agent.Post 2
1080+
do! Async.Sleep 100
1081+
equal [|1|] (log.ToArray())
1082+
let throwing = new MailboxProcessor<AsyncReplyChannel<int>>((fun _ -> async.Zero()), true)
1083+
throwing.Dispose()
1084+
let thrown = try throwing.PostAndAsyncReply id |> ignore; false with :? ObjectDisposedException -> true
1085+
equal true thrown
1086+
}
1087+
1088+
testCaseAsync "MailboxProcessor with isThrowExceptionAfterDisposed and a cancellation token works" <| fun () ->
1089+
async {
1090+
let log = ResizeArray<int>()
1091+
use cts = new Threading.CancellationTokenSource()
1092+
let body (inbox: MailboxProcessor<int>) = async {
1093+
while true do
1094+
let! msg = inbox.Receive()
1095+
log.Add msg }
1096+
let started = MailboxProcessor<int>.Start(body, true, cts.Token)
1097+
let created = new MailboxProcessor<int>(body, true, cts.Token)
1098+
created.Start()
1099+
started.Post 1
1100+
do! Async.Sleep 100
1101+
created.Post 2
1102+
do! Async.Sleep 100
1103+
equal [|1; 2|] (log.ToArray())
1104+
cts.Cancel()
1105+
started.Post 3
1106+
created.Post 4
1107+
do! Async.Sleep 100
1108+
equal [|1; 2|] (log.ToArray())
1109+
started.Dispose()
1110+
created.Dispose()
1111+
let thrown (agent: MailboxProcessor<int>) = try agent.Post 5; false with :? ObjectDisposedException -> true
1112+
equal (true, true) (thrown started, thrown created)
1113+
}
1114+
10501115
testCaseAsync "MailboxProcessor.postAndAsyncReply works" <| fun () ->
10511116
async {
10521117
let formatString = "Msg: {0} - {1}"

‎tests/Python/TestMailboxProcessor.fs‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,74 @@ let ``test MailboxProcessor.PostAndAsyncReply never completes after Dispose`` ()
141141
equal false replied
142142
} |> Async.RunSynchronously
143143

144+
[<Fact>]
145+
let ``test MailboxProcessor.Start with isThrowExceptionAfterDisposed works`` () =
146+
async {
147+
let log = ResizeArray<int>()
148+
let agent = MailboxProcessor<int>.Start((fun inbox -> async {
149+
while true do
150+
let! msg = inbox.Receive()
151+
log.Add msg }), true)
152+
agent.Post 1
153+
do! Async.Sleep 200
154+
equal [|1|] (log.ToArray())
155+
agent.Dispose()
156+
agent.Dispose()
157+
let thrown = try agent.Post 2; false with :? ObjectDisposedException -> true
158+
equal true thrown
159+
do! Async.Sleep 100
160+
equal [|1|] (log.ToArray())
161+
} |> Async.RunSynchronously
162+
163+
[<Fact>]
164+
let ``test MailboxProcessor constructor with isThrowExceptionAfterDisposed works`` () =
165+
async {
166+
let log = ResizeArray<int>()
167+
let agent = new MailboxProcessor<int>((fun inbox -> async {
168+
while true do
169+
let! msg = inbox.Receive()
170+
log.Add msg }), false)
171+
agent.Start()
172+
agent.Post 1
173+
do! Async.Sleep 200
174+
agent.Dispose()
175+
agent.Post 2
176+
do! Async.Sleep 100
177+
equal [|1|] (log.ToArray())
178+
let throwing = new MailboxProcessor<AsyncReplyChannel<int>>((fun _ -> async.Zero()), true)
179+
throwing.Dispose()
180+
let thrown = try throwing.PostAndAsyncReply id |> ignore; false with :? ObjectDisposedException -> true
181+
equal true thrown
182+
} |> Async.RunSynchronously
183+
184+
[<Fact>]
185+
let ``test MailboxProcessor with isThrowExceptionAfterDisposed and a cancellation token works`` () =
186+
async {
187+
let log = ResizeArray<int>()
188+
use cts = new Threading.CancellationTokenSource()
189+
let body (inbox: MailboxProcessor<int>) = async {
190+
while true do
191+
let! msg = inbox.Receive()
192+
log.Add msg }
193+
let started = MailboxProcessor<int>.Start(body, true, cts.Token)
194+
let created = new MailboxProcessor<int>(body, true, cts.Token)
195+
created.Start()
196+
started.Post 1
197+
do! Async.Sleep 100
198+
created.Post 2
199+
do! Async.Sleep 100
200+
equal [|1; 2|] (log.ToArray())
201+
cts.Cancel()
202+
started.Post 3
203+
created.Post 4
204+
do! Async.Sleep 100
205+
equal [|1; 2|] (log.ToArray())
206+
started.Dispose()
207+
created.Dispose()
208+
let thrown (agent: MailboxProcessor<int>) = try agent.Post 5; false with :? ObjectDisposedException -> true
209+
equal (true, true) (thrown started, thrown created)
210+
} |> Async.RunSynchronously
211+
144212
// PostAndAsyncReply
145213

146214
[<Fact>]

0 commit comments

Comments
 (0)