4 Replies Latest reply on Dec 11, 2007 1:02 PM by Manik Surtani

    Problem with ShutdownHook

    Michael Light Newbie

      I found possible problem in class org.jboss.cache.CacheImpl.java in method internalStart() at line ~820: "Runtime.getRuntime().addShutdownHook(shutdownHook);"
      It works perfectly in stand-alone environment, but in MBeanServer it may cause problems.
      MBean server has internal life-cycle definition for mbeans, and defines "stop" method for mbeans. When ShutdownHookMBean is processed in MBeanServer, the server stops mbeans in order reversed to their start. When MBeans depending on "CacheMBean" in stop method need to do some finalization work with cache (e.g. remove/update nodes), exception "java.lang.IllegalStateException" throwns, because Cache had already switched to stop state (due to hardcoded shutdown hook). Solution is to check runtime environment and in depend on this check to rely on shutdownhook (as it is now) or to stop method.
      PS: Sorry for my english.

        • 2. Re: Problem with ShutdownHook
          Brian Stansberry Master

          I suggest this be controlled with a configuration flag rather than relying on the presence/absence of an mbean server. The behavior of moving an mbean through create/start/stop/destroy is not inherent in an mbean server; it's a special function of JBoss AS. So, you could have JBC running in the presence of an mbean server that would not call stop/destroy for you. Theoretically, you could also have JBC running in a pure microcontainer version of JBoss AS 5 where there is no mbean server. But that AS would still have its own ShutdownHook that takes care of create/start/stop/destroy and would not want JBC's own hook getting in the way.

          This is not an urgent thing; in the real world JBoss AS always has an mbean server, so the existing JBCACHE-1204 fix stops the problem of the JBC shutdown hook screwing up an AS shutdown.

          • 3. Re: Problem with ShutdownHook
            Brian Stansberry Master

            Another issue with the shutdown hook I'm seeing with Hibernate testing is that the hook doesn't get removed in internalStop(). JBC should keep a ref to the hook and remove it in internal stop; otherwise:

            1) The hook executes at vm shutdown even though the cache was previously stopped/destroyed. That currently just causes a DEBUG log, but it's prone to breakage.
            2) Until that happens you've leaked a ref to the CacheImpl to the shutdown hook thread.

            Might want to make the hook impl a bit more sophisticated so internalStop() can check if it is executing -- i.e. don't call Runtime.removeShutdownHook if it is the hook itself that calls internalStop().