Fork me on GitHub
#leiningen
<
2023-11-24
>
markx03:11:22

Hi! I'm trying to run some thing like lein run -m play.core/app and it just hangs. What's a good way to investigate this? lein run works in my other projects.

hiredman03:11:04

You can use something like jstack to get the stacks of all the threads in a jvm

hiredman03:11:53

There are some signals you can send to the jvm via kill that will cause it to print thread stacks as well

hiredman03:11:04

Or you can just comment out everything in play/core.clj and uncomment it form by form until you find the one that is causing the hang

markx03:11:47

Thanks! Turns out it actually ran everything, and then hang instead of exiting.

markx03:11:25

But... I still don't know why it doesn't exit.

hiredman03:11:08

It will exit after a minute

markx03:11:56

Why? what's that one minute for?

hiredman04:11:38

Clojure has some threadpools internally where the threads aren't marked as daemon threads, which for reasons causes the jvm not to exit for up to 60 seconds of those threadpools are ever used

hiredman04:11:21

You can call shutdown-agents to manually stop the threadpools

hiredman04:11:32

(the polls are the agent threadpools where agent actions get run, but there are a few other things like future and pmap which use one of the agent pools)

markx04:11:06

adding (shutdown-agents) didn't fix it.

hiredman04:11:18

Where did you add it?

hiredman04:11:05

It could be some other thread, not one from the clojure thread pools id preventing the jvm from exiting

hiredman04:11:59

The jvm won't exit until all non-daemon threads have stopped running, so just because the main thread stops that doesn't mean it will exit

markx04:11:46

I added it to the main function.

markx04:11:23

fwiw now my main function is just

(defn -main []
  (prn "!!!")
  (shutdown-agents))

markx04:11:33

Now I run lein run play.core. still won't exit after printing the !!!.

markx04:11:57

Ah OK. it's some other code in the file that blocks it. After commenting out all other code but the main function, it exits properly.

vemv07:11:19

> There are some signals you can send to the jvm via kill that will cause it to print thread stacks as well For completeness, it's Ctrl+\ - can be handy in many situations :)