diff --git a/CHANGELOG.md b/CHANGELOG.md index 72820f3..8fd69d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,9 @@ # Unreleased -## Added - -## Fixed - ## Changed +- Make sure child processes are killed when launchpad receives a INT/TERM/KILL signal + # 0.29.135-alpha (2024-06-03 / 00df813) ## Added @@ -231,4 +229,4 @@ Initial release - lambdaisland.classpath integration - Support for cider-nrepl, refactor-nrepl - Basic support for shadow-cljs cljs nREPL-base REPL -- Auto-connect for Emacs \ No newline at end of file +- Auto-connect for Emacs diff --git a/src/lambdaisland/launchpad.clj b/src/lambdaisland/launchpad.clj index 7160f0a..d812371 100644 --- a/src/lambdaisland/launchpad.clj +++ b/src/lambdaisland/launchpad.clj @@ -15,6 +15,29 @@ (java.net ServerSocket) (java.util.concurrent TimeUnit))) +(defonce processes (atom [])) + +(defn cleanup [sig] + (println "Received" (str "SIG" sig)) + (doseq [process @processes] + (print "Killing" (.pid process)) + (flush) + (.destroy process) + (println " ->" (.waitFor process)) + (flush)) + (System/exit 0)) + +(defmacro set-signal-handler! + [signal f] + `(sun.misc.Signal/handle + (sun.misc.Signal. ~signal) + (proxy [sun.misc.SignalHandler] [] + (handle [signal#] (~f signal#))))) + +(set-signal-handler! "INT" cleanup) +(set-signal-handler! "TERM" cleanup) +(set-signal-handler! "KILL" cleanup) + (def cli-opts [["-h" "--help"] ["-v" "--verbose" "Print debug information"] @@ -518,6 +541,7 @@ color (mod (hash (or prefix (first cmd))) 8) prefix (str "[" (ansi-fg (+ 30 color) (or prefix (first cmd))) "] ") process (pipe-process-output (.start proc-builder) prefix) + _ (swap! processes conj process) ctx (update ctx :processes (fnil conj []) process)] (when show-command? (apply println (str prefix "$") (map shellquote cmd))) @@ -617,7 +641,5 @@ after-steps end-steps))) processes (:processes ctx)] - (.addShutdownHook (Runtime/getRuntime) - (Thread. (fn [] (run! #(.destroy %) processes)))) (System/exit (apply min (for [p processes] (.waitFor p))))))