dev@glassfish.java.net

Re: IMPORTANT - Daemon Threads

From: Joseph Fialli <joe.fialli_at_oracle.com>
Date: Wed, 13 Oct 2010 09:26:53 -0400

  All,

I realize that this email is addressing why the server is not stopping
with force set to true.

I would like to add that there are other situations under which the
server is not stopping than non-daemon threads described below.

- see last comments added to
https://glassfish.dev.java.net/issues/show_bug.cgi?id=13592 . An ejb
invocation method that is not
    terminating in less than a minute is causing the ejb-container to
stay running. (the method is a test method and not a typical ejb method)

- Thread pools (such as those created by Executors or
ThreadPoolExecutors) needed to be considered.
    The user does not create the thread instances directly in a thread
pool. These threads will not stop immediately
    unless thread pool is stopped with shutdownNow(). GMS is careful
to call shutdown for all its thread pools (or the gms client does not
    exit).

-Joe


On 10/13/10 2:01 AM, Byron Nevins wrote:
> Here is the message to take away if you don't read the rest of the email:
>
> /If you create a Thread in GlassFish -- *always *make it a daemon
> thread -- unless you have a very good reason not to. I.e. use the
> OPPOSITE of the default for Thread creation.
> /
> It's trivial to do:
>
> Thread.setDaemon(true)
>
> =============================================
>
> You all probably know this already but I'll repeat anyways: when the
> main() method in a java application (even GlassFish is but a java
> application!) returns -- the JVM exits.
>
> Unless there are non-daemon threads left running. In that case the
> JVM waits for the threads to die. Now if the thread is like this one:
>
> "Thread-22" prio=6 tid=0x070c9400 nid=0x1608 runnable [0x08daf000]
> java.lang.Thread.State: RUNNABLE
> .... boring stack trace removed
> at
> com.sun.messaging.jmq.admin.jmsspi.JMSAdminImpl$StreamGobbler.run(JMSAdminImpl.java:1507)
>
> it never dies and the JVM will be waiting forever -- i.e. the Server
> is now permanently hung.
>
> Daemon threads will identify themselves as daemon like this:
> "fileinstall-C:\glassfish3\glassfish\nodes\localhost\i1/autodeploy/bundles/"
> *daemon *prio=6 tid=0x06176400 nid=0x5a0 in ...
>
> We take care of this sort of problem now with a System.exit() after
> stopping services. This is not ideal though. Please be very careful
> when creating long-running threads!
> We ought to be able to exit the JVM cleanly without resporting to the
> sledge hammer of System.exit().
>
> Do you have a non-daemon thread running that ought to be a daemon
> thread? Here is how you can find out:
>
> (0) Start DAS
> (1) run this in a browser:
> http://localhost:4848/__asadmin/stop-domain?force=false
> (2) ZOMBIE!!! Run jstack on the Zombie process to see all the threads
> that are preventing an orderly shutdown.
>
> (The browser technique will go away within a week so try it right away
> if you think you may have a "bad" thread)
>
>
> --
> Byron Nevins - Oracle Corporation
> Home: 650-359-1290
> Cell: 650-784-4123
> Sierra: 209-295-2188
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net For
> additional commands, e-mail: dev-help_at_glassfish.dev.java.net