java.lang.Object
java.lang.Thread
net.i2p.router.tasks.ShutdownHook
All Implemented Interfaces:
Runnable

public class ShutdownHookextends Thread
Emergency shutdown hook for JVM termination.

This hook is registered with the JVM Runtime to ensure clean router shutdown even if the normal shutdown process is bypassed or interrupted. It serves as a failsafe mechanism to handle unexpected JVM termination scenarios such as:

  • System.exit(int) calls outside normal shutdown flow
  • JVM crashes or fatal errors
  • Process termination signals (SIGTERM, etc.)
  • Power loss or system shutdown

The hook logs the shutdown event and performs a hard shutdown to prevent the Java Service Wrapper from becoming confused about the router's state.

Note: During normal router shutdown, this hook should be cancelled to avoid unnecessary emergency shutdown procedures.

  • Field Details

    • _context

      private final RouterContext _context
      The router being shut down.
    • __id

      private static final AtomicInteger __id
      Counts the hooks created.
    • _id

      private final int _id
      This hook's number, used in its name.
  • Constructor Details

    • ShutdownHook

      public ShutdownHook(RouterContext ctx)
      Create a new shutdown hook for emergency router shutdown. This hook is registered with the JVM to ensure clean shutdown even if the normal shutdown process is bypassed.
      Parameters:
      ctx - router context for accessing router services
  • Method Details

    • run

      public void run()
      Perform emergency shutdown when JVM is terminating.

      This method is called by the JVM during shutdown to ensure the router shuts down cleanly even in abnormal termination scenarios. It logs the shutdown and performs a hard shutdown.

      This is a failsafe mechanism - normal shutdown should cancel this hook to avoid unnecessary emergency procedures.

      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread