{"record":{"id":"395a0552f66d3bb2","repo":"apache/hadoop","slug":"shutdownhook-cannot-be-null","errorCode":null,"errorMessage":"shutdownHook cannot be NULL","messagePattern":"shutdownHook cannot be NULL","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ShutdownHookManager.java","lineNumber":298,"sourceCode":"        return o2.priority - o1.priority;\n      }\n    });\n    return list;\n  }\n\n  /**\n   * Adds a shutdownHook with a priority, the higher the priority\n   * the earlier will run. ShutdownHooks with same priority run\n   * in a non-deterministic order.\n   *\n   * @param shutdownHook shutdownHook <code>Runnable</code>\n   * @param priority priority of the shutdownHook.\n   */\n  @InterfaceAudience.Public\n  @InterfaceStability.Stable\n  public void addShutdownHook(Runnable shutdownHook, int priority) {\n    if (shutdownHook == null) {\n      throw new IllegalArgumentException(\"shutdownHook cannot be NULL\");\n    }\n    if (shutdownInProgress.get()) {\n      throw new IllegalStateException(\"Shutdown in progress, cannot add a \" +\n          \"shutdownHook\");\n    }\n    hooks.add(new HookEntry(shutdownHook, priority));\n  }\n\n  /**\n   *\n   * Adds a shutdownHook with a priority and timeout the higher the priority\n   * the earlier will run. ShutdownHooks with same priority run\n   * in a non-deterministic order. The shutdown hook will be terminated if it\n   * has not been finished in the specified period of time.\n   *\n   * @param shutdownHook shutdownHook <code>Runnable</code>\n   * @param priority priority of the shutdownHook\n   * @param timeout timeout of the shutdownHook","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ShutdownHookManager.java#L280-L316","documentation":"ShutdownHookManager is Hadoop's prioritized, timeout-capable replacement for Runtime.addShutdownHook. addShutdownHook(Runnable, int) rejects a null hook with IllegalArgumentException before touching the hook registry. The hook object was never constructed — usually a factory, DI field, or optional component that produced null.","triggerScenarios":"ShutdownHookManager.get().addShutdownHook(null, priority) — typically hook built lazily (e.g. hook = service != null ? service.newCleanup() : null) and the guard branch lost.","commonSituations":"Registering cleanup for a service whose initialization failed earlier; optional plugins disabled via config injecting null; test wiring that forgot to instantiate the hook runnable.","solutions":["Find who passes null: check the argument expression at the call site","Construct the Runnable before registering, or skip registration entirely when the component is disabled","Add Objects.requireNonNull(hook, \"shutdownHook\") at your own call boundary for a clearer failure","If the component may legitimately be absent, branch: if (cleanup != null) manager.addShutdownHook(cleanup, prio);"],"exampleFix":"// before\nShutdownHookManager.get().addShutdownHook(cleanupTask, 50); // cleanupTask null when init failed\n\n// after\nif (cleanupTask != null) {\n  ShutdownHookManager.get().addShutdownHook(cleanupTask, 50);\n}","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(hook, \"shutdownHook must be constructed before registration\");\nShutdownHookManager.get().addShutdownHook(hook, priority);","typeGuard":null,"tryCatchPattern":"try { ShutdownHookManager.get().addShutdownHook(hook, priority); } catch (IllegalArgumentException e) { /* hook was null: fix construction, skip registration */ }","preventionTips":["Construct the hook Runnable at declaration site so a null cannot flow into registration","Skip registration for disabled/failed components instead of passing null","Use Objects.requireNonNull with a message at your own API boundary for clearer failures"],"tags":["hadoop","shutdown-hook","null-argument","lifecycle"],"backgroundTag":"null-argument","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}