This is why it errs in that direction; whether it is necessary or
desirable given a fuller understanding of the system, I can't say
offhand.
commit 6c1815706fc00e32eb1a1a07ace9df4c801ce76c
Author: extempore <extempore@5e8d7ff9-d8ef-0310-90f0-a4852d11357a>
Date: Sun Aug 15 15:45:02 2010 +0000
Since r22186 scripts wait for all non-daemon threads to exit
before the script will exit (not doing so was the cause of several
other bugs involving early script termination) but this means we
must be careful not to introduce unnecessary non-daemon threads
in the script infrastructure. Two such sources (now fixed) were
the shutdown hook which deletes temporary files and the stream
consumers used by Process. Closes #3678, review by harrah.
I think the motivating issue wasn't clear to you. I have to wait for
all threads which I can't exclude. Nothing is explicitly waiting for
shutdown threads, it's waiting for all threads which might be
user-initiated so it doesn't call exit with the program still running.
The risk posed by the shutdown threads is deadlock: they're waiting
for shutdown, I'm waiting for them.
I have another idea though: rather than waiting for all non-daemon
threads, I can exit when there are no running non-daemon threads
(because shutdown hook threads won't have been started.)
Yep, I missed the point. Thanks for clarifying.
> I have another idea though: rather than waiting for all non-daemon
> threads, I can exit when there are no running non-daemon threads
> (because shutdown hook threads won't have been started.)
The idea sounds good, but it's also exactly what the JVM already does:
initiate shutdown when all non-daemon threads have exited. This makes
me wonder if your code could itself be a shutdown hook? Probably I'm
missing something again.
Do you have a pointer to the scripting code in question? I may be
able to contribute something more intelligent with some additional
context.
Thanks,
Mark.
The only way to return an exit code is to call System.exit. Would
have been nice if "public static void main" had set off some alarm
bells somewhere in the early '90s, but so it goes. So those are the
requirements: you have to call System.exit, but only after everything
which should complete has completed.
https://github.com/scala/scala/blob/master/src/compiler/scala/tools/nsc/ScriptRunner.scala
There are a bunch of other files if you really want to get into it,
but there's a starting point.
Ah, that's a nasty problem. I see now why you've had to go down this
path. I like your util.waitingForThreads() function.
> Would have been nice if "public static void main" had set off some alarm
> bells somewhere in the early '90s, but so it goes. So those are the
> requirements: you have to call System.exit, but only after everything
> which should complete has completed.
>
> https://github.com/scala/scala/blob/master/src/compiler/scala/tools/nsc/ScriptRunner.scala
>
> There are a bunch of other files if you really want to get into it,
> but there's a starting point.
I think waiting for non-daemon, live threads like you've done
(https://github.com/scala/scala/commit/71e733b887a1db34cc9fb461f561c6e7223f88f8#L0R36)
is a good improvement. Thanks for taking the time to look at this and
explain the issues to me.
Mark.