Skip to content

Commit 6a6c53e

Browse files
authored
flambda-backend: Better handling of multiple simultaneous async exns (#2453)
1 parent a1fe776 commit 6a6c53e

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

runtime/callback.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "caml/memory.h"
2727
#include "caml/mlvalues.h"
2828
#include "caml/platform.h"
29+
#include "caml/signals.h"
2930

3031
/* A note about callbacks and GC. For best performance, a callback such as
3132
[caml_callback_exn(value closure, value arg)]
@@ -494,8 +495,15 @@ CAMLprim value caml_with_async_exns(value body_callback)
494495
res = caml_callback_exn(body_callback, Val_unit);
495496

496497
/* raised as a normal exn, even if it was asynchronous */
497-
if (Is_exception_result(res))
498-
caml_raise(Extract_exception(res));
498+
if (Is_exception_result(res)) {
499+
/* Drain the queue of pending actions. We may need to do
500+
this several times if some raise */
501+
do {
502+
res = Extract_exception(res);
503+
res = caml_process_pending_actions_with_root_exn(res);
504+
} while (Is_exception_result(res));
505+
caml_raise(res);
506+
}
499507

500508
return res;
501509
}

runtime4/callback.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "caml/fail.h"
2424
#include "caml/memory.h"
2525
#include "caml/mlvalues.h"
26+
#include "caml/signals.h"
2627

2728
static value raise_if_exception(value res)
2829
{
@@ -278,8 +279,15 @@ CAMLprim value caml_with_async_exns(value body_callback)
278279
res = caml_callback_exn(body_callback, Val_unit);
279280

280281
/* raised as a normal exn, even if it was asynchronous */
281-
if (Is_exception_result(res))
282-
caml_raise(Extract_exception(res));
282+
if (Is_exception_result(res)) {
283+
/* Drain the queue of pending actions. We may need to do
284+
this several times if some raise */
285+
do {
286+
res = Extract_exception(res);
287+
res = caml_process_pending_actions_with_root_exn(res);
288+
} while (Is_exception_result(res));
289+
caml_raise(res);
290+
}
283291

284292
return res;
285293
}

0 commit comments

Comments
 (0)