1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09:54 +00:00

Merge pull request #1181 from chris-chambers/ev-gather-cancel

Ensure ev/gather fibers are fully canceled on error
This commit is contained in:
Calvin Rose 2023-06-04 11:10:24 -05:00 committed by GitHub
commit 88ba99b87e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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