1
0
mirror of https://github.com/janet-lang/janet synced 2025-04-06 07:17:16 +00:00

Ensure ev/gather fibers are fully canceled on error.

This commit is contained in:
Christopher Chambers 2023-06-04 09:59:10 -04:00
parent c4c86f8671
commit 53447e9d0b
2 changed files with 14 additions and 3 deletions

View File

@ -3663,17 +3663,21 @@
(,ev/deadline ,deadline nil ,f)
(,resume ,f))))
(defn- cancel-all [fibers reason] (each f fibers (ev/cancel f reason) (put fibers f nil)))
(defn- cancel-all [chan fibers reason]
(each f fibers (ev/cancel f reason))
(let [n (length fibers)]
(table/clear fibers)
(repeat n (ev/take chan))))
(defn- wait-for-fibers
[chan fibers]
(defer (cancel-all fibers "parent canceled")
(defer (cancel-all chan fibers "parent canceled")
(repeat (length fibers)
(def [sig fiber] (ev/take chan))
(if (= sig :ok)
(put fibers fiber nil)
(do
(cancel-all fibers "sibling canceled")
(cancel-all chan fibers "sibling canceled")
(propagate (fiber/last-value fiber) fiber))))))
(defmacro ev/gather

View File

@ -168,6 +168,13 @@
(assert (deep= @[] (ev/gather)) "ev/gather 2")
(assert-error "ev/gather 3" (ev/gather 1 2 (error 3)))
(var cancel-counter 0)
(assert-error "ev/gather 4.1" (ev/gather
(defer (++ cancel-counter) (ev/take (ev/chan)))
(defer (++ cancel-counter) (ev/take (ev/chan)))
(error :oops)))
(assert (= cancel-counter 2) "ev/gather 4.2")
# Net testing
# 2904c19ed
(repeat 10