babashka

pieterbreed 2025-04-21T10:25:06.651259Z

I'm using babashka.process/process in bb and it seems like neither the :shutdown or :exit-fn hooks are being fired. Am I doing something wrong?

(p/process {:out :write
            :out-file "output.txt"
            :pre-start-fn (fn [args] (log/info (str "start: " args)))
            :exit-fn      (fn [args] (log/info (str "exit: " args)))
            :shutdown     (fn [args] (log/info (str "shutdown:" args)))}
            "./fake_worker.sh")
results in
2025-04-21T10:23:09.516Z mahogany INFO [get-weather:3] - start: {:cmd ["./fake_worker.sh"]}
{:proc #object[java.lang.ProcessImpl 0x193c71fd "Process[pid=1336523, exitValue=\"not exited\"]"],
 :exit nil,
 :in
 #object[java.lang.ProcessImpl$ProcessPipeOutputStream 0x751761a5 "java.lang.ProcessImpl$ProcessPipeOutputStream@751761a5"],
 :out #object[java.lang.ProcessBuilder$NullInputStream 0x30d7390e "java.lang.ProcessBuilder$NullInputStream@30d7390e"],
 :err
 #object[java.lang.ProcessImpl$ProcessPipeInputStream 0xa48311a "java.lang.ProcessImpl$ProcessPipeInputStream@a48311a"],
 :prev nil,
 :cmd ["./fake_worker.sh"]}

$> (deref *1)
{:proc #object[java.lang.ProcessImpl 0x193c71fd "Process[pid=1336523, exitValue=0]"],
 :exit 0,
 :in
 #object[java.lang.ProcessImpl$ProcessPipeOutputStream 0x751761a5 "java.lang.ProcessImpl$ProcessPipeOutputStream@751761a5"],
 :out #object[java.lang.ProcessBuilder$NullInputStream 0x30d7390e "java.lang.ProcessBuilder$NullInputStream@30d7390e"],
 :err
 #object[java.lang.ProcessImpl$ProcessPipeInputStream 0xa48311a "java.lang.ProcessImpl$ProcessPipeInputStream@a48311a"],
 :prev nil,
 :cmd ["./fake_worker.sh"]}
$> 

borkdude 2025-04-21T10:27:15.643329Z

Do you have the fake-worker script as well so I can repro?

borkdude 2025-04-21T10:31:02.393269Z

When I run this I see both :exit and :shutdown being printed:

(def my-proc (p/process {:out :write
                        :out-file "output.txt"
                        :pre-start-fn (fn [args] (prn :args args))
                        :exit-fn      (fn [args] (prn :exit))
                        :shutdown     (fn [args] (prn :shutdown))}
                       "ls"))

(prn @my-proc)

borkdude 2025-04-21T10:31:30.055409Z

$ bb /tmp/repro_process.clj
:args {:cmd ["ls"]}
:exit
:shutdown

pieterbreed 2025-04-21T10:35:42.642819Z

This is fake_worker.sh

#!/usr/bin/env bash

echo "\"working...\""
sleep 5
echo "oh crap!, quitting"
so it's nothing special, I think

pieterbreed 2025-04-21T10:37:13.667989Z

ok... one difference, I'm running my script in a cider repl...

borkdude 2025-04-21T10:37:29.318869Z

$ bb /tmp/repro_process.clj
:args {:cmd ["/tmp/fake_worker.sh"]}
:exit
:shutdown
{:proc #object[java.lang.ProcessImpl 0x97e47b4 "Process[pid=76351, exitValue=0]"], :exit 0, :in #object[java.lang.ProcessImpl$ProcessPipeOutputStream 0x3f1c4aa1 "java.lang.ProcessImpl$ProcessPipeOutputStream@3f1c4aa1"], :out #object[java.lang.ProcessBuilder$NullInputStream 0x794026ae "java.lang.ProcessBuilder$NullInputStream@794026ae"], :err #object[java.lang.ProcessImpl$ProcessPipeInputStream 0x2042025f "java.lang.ProcessImpl$ProcessPipeInputStream@2042025f"], :prev nil, :cmd ["/tmp/fake_worker.sh"]}

borkdude 2025-04-21T10:38:16.695009Z

Perhaps you're just not seeing the output because it's logged elsewhere?

borkdude 2025-04-21T10:38:24.207269Z

in your console rather than in the REPL or so

pieterbreed 2025-04-21T10:39:05.328839Z

The log/info from :pre-start-fn comes to the repl though?

borkdude 2025-04-21T10:39:17.053409Z

try a normal script first without the REPL

pieterbreed 2025-04-21T10:42:26.002919Z

that seems to make the difference. with $ bb test.clj I see it being printed...

borkdude 2025-04-21T10:43:20.666659Z

can you try a non-I/O side effect in the exit+shutdown functions to see if they are being executed in the REPL? if so, I would assume it's something weird with REPL I/O and threads perhaps

pieterbreed 2025-04-21T10:44:00.345199Z

you might be right... I tried with an atom and did a (swap! a inc) in the hooks, and the counter incremented twice...

borkdude 2025-04-21T10:45:20.952909Z

what you can also do, if you prefer, to use the normal Clojure JVM REPL tooling and then run your script with bb occasionally to see if it still works. with bb print-deps you get a deps.edn that you can use with a regular REPL

👍🏽 1
🙏🏽 1
pieterbreed 2025-04-21T10:45:48.639739Z

thanks for helping 🙂

borkdude 2025-04-21T10:47:08.314379Z

I do see that the bb.process dependency printed in the bb print-deps output is a bit behind... :-) So I think you probably want to manually bump it. I'll bump it now