From 6edc26c834ff74779819a4ca8a1734c1fe35ebf5 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 10 May 2021 18:29:26 +0200 Subject: [PATCH] [#831] Workaround for musl stack size issues --- .circleci/script/release | 1 - script/compile | 4 +++- src/babashka/main.clj | 23 +++++++++++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.circleci/script/release b/.circleci/script/release index 48e9b2ab..b4a142e6 100755 --- a/.circleci/script/release +++ b/.circleci/script/release @@ -3,7 +3,6 @@ rm -rf /tmp/release mkdir -p /tmp/release cp bb /tmp/release -# cp src-bash/bbk /tmp/release VERSION=$(cat resources/BABASHKA_VERSION) diff --git a/script/compile b/script/compile index 1b4cdfbd..37988d41 100755 --- a/script/compile +++ b/script/compile @@ -76,7 +76,9 @@ args=( "-jar" "$BABASHKA_JAR" BABASHKA_STATIC=${BABASHKA_STATIC:-} if [ "$BABASHKA_STATIC" = "true" ]; then - args+=("--static" "--libc=musl") + args+=("--static" "--libc=musl" + # see https://github.com/oracle/graal/issues/3398 + "-H:CCompilerOption=-Wl,-z,stack-size=2097152") # needs to be in a separate script as we need sudo and >> redirects in it wont work. sudo bash script/setup-musl diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 703236f8..cb0e9162 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -863,6 +863,25 @@ Use bb run --help to show this help output. (let [opts (parse-opts args)] (exec opts))) +(def static? + "Captured at compile time, to know if we are running inside a + statically compiled executable." + (System/getenv "BABASHKA_STATIC")) + +(defmacro run [args] + (if static? + ;; When running in musl-compiled static executable we lift execution of bb + ;; inside a thread, so we have a larger than default stack size, set by an + ;; argument to the linker. See https://github.com/oracle/graal/issues/3398 + `(let [v# (volatile! nil) + f# (fn [] + (vreset! v# (apply main ~args)))] + (doto (Thread. nil f# "main") + (.start) + (.join)) + @v#) + `(apply main ~args))) + (defn -main [& args] (handle-pipe!) @@ -874,10 +893,10 @@ Use bb run --help to show this help output. (dotimes [i n] (if (< i last-iteration) (with-out-str (apply main args)) - (do (apply main args) + (do (run args) (binding [*out* *err*] (println "ran" n "times")))))) - (let [exit-code (apply main args)] + (let [exit-code (run args)] (System/exit exit-code)))) ;;;; Scratch