Skip to content

Commit a048d9c

Browse files
authored
Fix ParAff cancellation (#131)
Fixes #130
1 parent 1d2f56e commit a048d9c

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/Control/Monad/Aff.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,9 @@ var Aff = function () {
536536
joins[jid] = join;
537537

538538
return function() {
539-
delete joins[jid];
539+
if (joins !== null) {
540+
delete joins[jid];
541+
}
540542
};
541543
};
542544
}
@@ -937,14 +939,20 @@ var Aff = function () {
937939
}
938940

939941
// Cancels the entire tree. If there are already subtrees being canceled,
940-
// we need to first cancel those joins. This is important so that errors
941-
// don't accidentally get swallowed by irrelevant join callbacks.
942+
// we need to first cancel those joins. We will then add fresh joins for
943+
// all pending branches including those that were in the process of being
944+
// canceled.
942945
function cancel(error, cb) {
943946
interrupt = util.left(error);
944-
947+
var innerKills;
945948
for (var kid in kills) {
946949
if (kills.hasOwnProperty(kid)) {
947-
kills[kid]();
950+
innerKills = kills[kid];
951+
for (kid in innerKills) {
952+
if (innerKills.hasOwnProperty(kid)) {
953+
innerKills[kid]();
954+
}
955+
}
948956
}
949957
}
950958

test/Test/Main.purs

+20
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,25 @@ test_kill_parallel_alt = assert "kill/parallel/alt" do
513513
_ ← try $ joinFiber f2
514514
eq "killedfookilledbardone" <$> readRef ref
515515

516+
test_kill_parallel_alt_finalizer eff. TestAff eff Unit
517+
test_kill_parallel_alt_finalizer = assert "kill/parallel/alt/finalizer" do
518+
ref ← newRef ""
519+
f1 ← forkAff $ sequential $
520+
parallel (delay (Milliseconds 10.0)) <|> parallel do
521+
bracket
522+
(pure unit)
523+
(\_ → do
524+
delay (Milliseconds 10.0)
525+
modifyRef ref (_ <> "killed"))
526+
(\_ → delay (Milliseconds 20.0))
527+
f2 ← forkAff do
528+
delay (Milliseconds 15.0)
529+
killFiber (error "Nope") f1
530+
modifyRef ref (_ <> "done")
531+
_ ← try $ joinFiber f1
532+
_ ← try $ joinFiber f2
533+
eq "killeddone" <$> readRef ref
534+
516535
test_fiber_map eff. TestAff eff Unit
517536
test_fiber_map = assert "fiber/map" do
518537
ref ← newRef 0
@@ -628,6 +647,7 @@ main = do
628647
test_parallel_alt_sync
629648
test_parallel_mixed
630649
test_kill_parallel_alt
650+
test_kill_parallel_alt_finalizer
631651
test_avar_order
632652
test_efffn
633653
test_fiber_map

0 commit comments

Comments
 (0)