6 Replies Latest reply on Feb 21, 2017 2:57 AM by Martin Kouba

    Weld shutdown hook question: maybe interrupt() container thread?

    Laird Nelson Apprentice

      Consider the shutdown hook installed by Weld: core/WeldContainer.java at master · weld/core · GitHub

       

      I am experimenting with blocking the container thread from within an observer method like this:

       

      private final void block(@Observes @BeforeDestroyed(ApplicationScoped.class) @Priority(LIBRARY_AFTER) final Object event) throws InterruptedException {

        // ...

        Thread.currentThread().join();

      }

       

      (I'm continuing various experiments where I run other containers inside a CDI container; obviously we want them to stay up, but we also want the ability for CDI components to do things in a normal fashion.)

       

      If I'm reading things right, WeldContainer#shutdown() is in the process of being called at this point, and holds the lock (it is a synchronized method).  Fine.

       

      The shutdown hook that Weld installs attempts to blindly do (for each WeldContainer): weldContainer.shutdown().  Obviously if shutdown() is already entered on the main thread, then this shutdown hook thread will never acquire the lock and it will stall and ultimately CTRL-C doesn't do anything.

       

      I understand what I'm experimenting with is weird, and may ultimately be a mistake.  :-)  Nevertheless it seems like it might be useful to have the shutdown hook notify the main thread in some way (perhaps an interruption? or a notify()? or...?) so that annoying people like me :-) have a chance to unblock and proceed with shutdown normally.

       

      Right now I am taking advantage of the fact that the shutdown hook is not registered until after an @Initialized(ApplicationScoped.class) Object payload event is fired, so I am setting the system property then to disable the Weld shutdown hook and add my own instead.  If the main thread were interrupted, then I don't think I'd have to do this.

       

      I am not deeply familiar with the Weld codebase so I don't know what ramifications this might have.