Why does Leiningen keep its own JVM running?
Asked Answered
S

2

13

If I call

lein trampoline repl

Leiningen launches its own JVM process which then starts a separate JVM for the repl and quits. However calling the default

lein repl

leaves two JVMs running. Is there any reason for keeping the original Leiningen JVM process running? Why not make lein trampoline behavior the default and quit every time?

Surgy answered 7/12, 2014 at 3:16 Comment(4)
namespace separation ? flyingmachinestudios.com/programming/lein-trampolineEyler
That would break the process tree, preventing the processes from being cleaned up when something happens to the parent process that started lein.Howardhowarth
It's perfectly clear why lein starts a separate JVM, there are plenty of reasons to do so. By why keeping original JVM running for the whole time?Energid
@Eyler great article, I read it last year. It explains how trampoline works and when it should be used. But it doesn't answer the question why not always use trampoline.Energid
H
6

lein help trampoline states:

Run a task without nesting the project's JVM inside Leiningen's.

Calculates the Clojure code to run in the project's process for the given task and allows Leiningen's own JVM process to exit before running it rather than launching a subprocess of Leiningen's JVM.

Use this to save memory or to work around stdin issues.

Arguments: ([task-name & args])

So you can see, without trampoline the second JVM runs as a sub-process of the first. That's why the first one can't exit — exiting would disrupt the second.

On the other hand, with trampoline the first JVM constructs a shell script which is then executed by the lein script to spawn the second JVM. So in this case, the second JVM is a child of the lein script. How Clojure Babies are Made: Leiningen's Trampoline covers this in quite a bit of detail.

As to why trampoline is not the default, I'm not entirely sure. But remember that not every lein command runs project code, so the second JVM is not needed for every command.

Also, there may be disadvantages to using trampoline. For example, take a look at the following lines of code from the above article:

# Just don't change :target-path in project.clj, mkay?
TRAMPOLINE_FILE="target/trampolines/$INPUT_CHECKSUM"

To me, that implies that there could be problems if :target-path is set in project.clj.

Hapten answered 20/12, 2014 at 18:26 Comment(1)
Starting the bounty, I hoped to see some more reasoning about why lein is not terminating its main process by default. Personally, I believe that lein team added trampoline to boost its performance, but never trusted it enough to make it lein's default behavior, though I have nothing to prove it. Anyway, your answer is the best one and deserve this bounty.Energid
F
0

This is because first instance sets up the environment and passes other required parameters to the real jvm which keeps on running.

First instance of JVM is just a kind of wrapper which is for users ease, otherwise user have to do all those work of passing required parameters at every exeution point, which neither user friendly nor looks good from security point of view.

Frail answered 7/12, 2014 at 3:26 Comment(3)
Why not make lein trampoline behavior the default and quit every time?Surgy
Read the question please.Surgy
Well, the question explicitly asks, "Is there any reason for keeping the original Leiningen JVM process running?". This seems to answer that, no?Hapten

© 2022 - 2024 — McMap. All rights reserved.