{"record":{"id":"6c98a75ae6d585c7","repo":"apache/hadoop","slug":"cannot-close-proxy-since-it-is-null","errorCode":null,"errorMessage":"Cannot close proxy since it is null","messagePattern":"Cannot close proxy since it is null","errorType":"exception","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/RPC.java","lineNumber":797,"sourceCode":"    throws IOException {\n\n    return getProtocolProxy(protocol, clientVersion, addr, conf, NetUtils\n        .getDefaultSocketFactory(conf));\n  }\n\n  /**\n   * Stop the proxy. Proxy must either implement {@link Closeable} or must have\n   * associated {@link RpcInvocationHandler}.\n   * \n   * @param proxy\n   *          the RPC proxy object to be stopped\n   * @throws HadoopIllegalArgumentException\n   *           if the proxy does not implement {@link Closeable} interface or\n   *           does not have closeable {@link InvocationHandler}\n   */\n  public static void stopProxy(Object proxy) {\n    if (proxy == null) {\n      throw new HadoopIllegalArgumentException(\n          \"Cannot close proxy since it is null\");\n    }\n    try {\n      if (proxy instanceof Closeable) {\n        ((Closeable) proxy).close();\n        return;\n      } else {\n        InvocationHandler handler = Proxy.getInvocationHandler(proxy);\n        if (handler instanceof Closeable) {\n          ((Closeable) handler).close();\n          return;\n        }\n      }\n    } catch (IOException e) {\n      LOG.error(\"Closing proxy or invocation handler caused exception\", e);\n    } catch (IllegalArgumentException e) {\n      LOG.error(\"RPC.stopProxy called on non proxy: class=\" + proxy.getClass().getName(), e);\n    }","sourceCodeStart":779,"sourceCodeEnd":815,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/RPC.java#L779-L815","documentation":"RPC.stopProxy(null) throws HadoopIllegalArgumentException immediately. The API deliberately refuses null so that lost or never-created proxy references surface as bugs at the call site instead of no-op cleanup. Hitting it means your code attempted to stop a proxy that was never created or whose reference was already cleared.","triggerScenarios":"Calling RPC.stopProxy(proxy) in a finally block when proxy creation threw earlier and the field stayed null; double-cleanup paths that null the field after the first stop; utility code that closes whatever it is handed, including null.","commonSituations":"Connection failure in try/finally cleanup; refactors that moved proxy assignment after an exception-throwing step; guard code written for one path reused on a path where creation never happened.","solutions":["Null-guard the call: if (proxy != null) { RPC.stopProxy(proxy); proxy = null; }","Ensure stopProxy only runs on paths where assignment succeeded, e.g., track a 'created' flag or assign the field only inside the try after success.","Centralize creation and cleanup in one owner class so the proxy is stopped exactly once from exactly one place."],"exampleFix":"// before\nMyProto proxy = null;\ntry {\n  proxy = RPC.getProxy(...);\n  use(proxy);\n} finally {\n  RPC.stopProxy(proxy); // throws if getProxy failed\n}\n// after\nMyProto proxy = null;\ntry {\n  proxy = RPC.getProxy(...);\n  use(proxy);\n} finally {\n  if (proxy != null) {\n    RPC.stopProxy(proxy);\n    proxy = null;\n  }\n}","handlingStrategy":"validation","validationCode":"if (proxy == null) {\n  LOG.debug(\"no proxy to stop\");\n} else {\n  RPC.stopProxy(proxy);\n  proxy = null;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Null-check every RPC.stopProxy call, especially in finally blocks.","Assign the field to null right after stopping so double-cleanup is a guarded no-op.","Keep proxy creation and cleanup symmetric and owned by one class."],"tags":["rpc","lifecycle","null-check","cleanup","client"],"backgroundTag":"null-argument","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}