Compare commits

..

1 commit

Author SHA1 Message Date
Michiel Borkent
f4f1d9e945 Resemble Clojure's behavior wrt/ daemon threads 2025-05-09 20:12:38 +02:00
3 changed files with 21 additions and 5 deletions

View file

@ -9,7 +9,6 @@ A preview of the next release can be installed from
## Unreleased
- [#1818](https://github.com/babashka/babashka/issues/1818): wrong argument order in `clojure.java.io/resource` implementation
- Add `java.text.BreakIterator`
- Bump `fs` to `0.5.25`
- Bump `jsoup` to `1.20.1`

View file

@ -84,8 +84,8 @@
cp)))
(defn resource
(^URL [path] (resource path @the-url-loader))
(^URL [path loader]
(^URL [path] (resource @the-url-loader path))
(^URL [loader path]
(if (str/starts-with? path "/") nil ;; non-relative paths always return nil
(getResource loader [path] true))))

View file

@ -1190,7 +1190,7 @@ Use bb run --help to show this help output.
(println "[babashka] WARNING: config file does not exist:" config))
nil))
jar (let [jar (resolve-symbolic-link jar)]
(some->> [jar] cp/new-loader (cp/resource "META-INF/bb.edn") .toString))
(some-> [jar] cp/new-loader (cp/resource "META-INF/bb.edn") .toString))
:else (if (and file (fs/exists? file))
;; file relative to bb.edn
(let [file (abs-path file) ;; follow symlink
@ -1250,10 +1250,26 @@ Use bb run --help to show this help output.
@v#)
`(apply main ~args)))
(defn- set-daemon-agent-executor
"Set Clojure's send-off agent executor (also affects futures). This is almost
an exact rewrite of the Clojure's executor, but the Threads are created as
daemons."
[]
(let [thread-counter (atom 0)
thread-factory (reify java.util.concurrent.ThreadFactory
(newThread [_ runnable]
(doto (Thread. runnable)
(.setDaemon true) ;; DIFFERENT
(.setName (format "CLI-agent-send-off-pool-%d"
(first (swap-vals! thread-counter inc)))))))
executor (java.util.concurrent.Executors/newCachedThreadPool thread-factory)]
(set-agent-send-off-executor! executor)))
(defn -main
[& args]
(handle-pipe!)
(handle-sigint!)
(set-daemon-agent-executor)
(if-let [dev-opts (System/getenv "BABASHKA_DEV")]
(let [{:keys [:n]} (if (= "true" dev-opts) {:n 1}
(edn/read-string dev-opts))
@ -1265,7 +1281,8 @@ Use bb run --help to show this help output.
(binding [*out* *err*]
(println "ran" n "times"))))))
(let [exit-code (run args)]
(System/exit exit-code))))
(when-not (zero? exit-code)
(System/exit exit-code)))))
(sci/alter-var-root main-var (constantly -main))
;;;; Scratch