leiningen

markx 2023-11-24T03:41:22.698729Z

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.

2023-11-24T03:49:04.426539Z

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

2023-11-24T03:49:53.585879Z

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

2023-11-24T03:51:04.160459Z

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

markx 2023-11-24T03:54:47.368349Z

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

markx 2023-11-24T03:56:25.151439Z

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

2023-11-24T03:59:08.628179Z

It will exit after a minute

markx 2023-11-24T03:59:56.376169Z

Why? what's that one minute for?

2023-11-24T04:00:38.501149Z

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

2023-11-24T04:01:21.772289Z

You can call shutdown-agents to manually stop the threadpools

2023-11-24T04:02:32.210969Z

(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)

markx 2023-11-24T04:05:06.293909Z

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

2023-11-24T04:11:18.806529Z

Where did you add it?

2023-11-24T04:12:05.442939Z

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

2023-11-24T04:12:59.669309Z

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

markx 2023-11-24T04:13:46.759369Z

I added it to the main function.

markx 2023-11-24T04:14:23.388149Z

fwiw now my main function is just

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

markx 2023-11-24T04:15:33.380289Z

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

markx 2023-11-24T04:16:57.683459Z

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.

markx 2023-11-24T04:17:02.043579Z

Thanks!

vemv 2023-11-24T07:08:19.179129Z

> 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 :)