Several improvements around destroying subprocesses

See test/babashka/scripts/child.bb
This commit is contained in:
Michiel Borkent 2020-08-01 16:47:10 +02:00 committed by GitHub
parent 864cf410ed
commit 65eecdfc1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 3 deletions

View file

@ -117,6 +117,7 @@
java.lang.Math
java.lang.Object
java.lang.Process
java.lang.ProcessHandle
java.lang.ProcessBuilder
java.lang.ProcessBuilder$Redirect
java.lang.Runtime
@ -198,6 +199,7 @@
java.util.jar.JarFile
java.util.jar.JarEntry
java.util.jar.JarFile$JarFileEntry
java.util.stream.Stream
java.util.Random
java.util.regex.Pattern
java.util.Base64
@ -242,7 +244,8 @@
clojure.lang.Named
clojure.lang.Keyword
clojure.lang.Symbol
clojure.lang.Sequential]
clojure.lang.Sequential
java.util.List]
:custom ~custom-map})
(defmacro gen-class-map []
@ -260,6 +263,8 @@
(fn [v]
(cond (instance? java.lang.Process v)
java.lang.Process
(instance? java.lang.ProcessHandle v)
java.lang.ProcessHandle
;; added for calling .put on .environment from ProcessBuilder
(instance? java.util.Map v)
java.util.Map
@ -279,7 +284,9 @@
(instance? java.nio.file.FileSystem v)
java.nio.file.FileSystem
(instance? java.nio.file.PathMatcher v)
java.nio.file.PathMatcher)))))
java.nio.file.PathMatcher
(instance? java.util.stream.Stream v)
java.util.stream.Stream)))))
(def class-map (gen-class-map))

View file

@ -240,7 +240,12 @@
(def output (.getInputStream ls))
(assert (int? (.waitFor ls)))
(slurp output)")
"LICENSE")))
"LICENSE"))
(when test-utils/native?
(let [output (test-utils/bb nil (io/file "test" "babashka" "scripts" "child.bb"))
parsed (edn/read-string (format "[%s]" output))]
(is (every? number? parsed))
(is (= 3 (count parsed))))))
(deftest create-temp-file-test
(let [temp-dir-path (System/getProperty "java.io.tmpdir")]

View file

@ -0,0 +1,26 @@
(import 'java.util.List)
(set! *warn-on-reflection* true)
(defn handles [^java.lang.ProcessHandle x]
(distinct (cons x (mapcat handles (iterator-seq (.iterator (.descendants x)))))))
(defn run [& args]
(let [depth (or (System/getenv "DEPTH") "0")
depth (Integer/parseInt depth)]
(when-not (= 4 depth)
(let [pb (doto (ProcessBuilder. ^List args)
(.inheritIO))
_ (.put (.environment pb) "DEPTH"
(str (inc depth)))
proc (.start pb)]
(if (= 0 depth) ;; the top process
(do
(Thread/sleep 500)
(run! (fn [^java.lang.ProcessHandle handle]
(do (prn (.pid handle))
(.destroy handle)))
(handles (.toHandle proc))))
(Thread/sleep 100000000))))))
(run "./bb" *file*)