{"record":{"id":"e9559abb6931a764","repo":"apache/hadoop","slug":"illegal-argument-for-connect","errorCode":null,"errorMessage":"Illegal argument for connect()","messagePattern":"Illegal argument for connect\\(\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java","lineNumber":594,"sourceCode":"    connect(socket, address, null, timeout);\n  }\n\n  /**\n   * Like {@link NetUtils#connect(Socket, SocketAddress, int)} but\n   * also takes a local address and port to bind the socket to. \n   * \n   * @param socket socket.\n   * @param endpoint the remote address\n   * @param localAddr the local address to bind the socket to\n   * @param timeout timeout in milliseconds\n   * @throws IOException raised on errors performing I/O.\n   */\n  public static void connect(Socket socket, \n                             SocketAddress endpoint,\n                             SocketAddress localAddr,\n                             int timeout) throws IOException {\n    if (socket == null || endpoint == null || timeout < 0) {\n      throw new IllegalArgumentException(\"Illegal argument for connect()\");\n    }\n    \n    SocketChannel ch = socket.getChannel();\n    \n    if (localAddr != null) {\n      Class localClass = localAddr.getClass();\n      Class remoteClass = endpoint.getClass();\n      Preconditions.checkArgument(localClass.equals(remoteClass),\n          \"Local address %s must be of same family as remote address %s.\",\n          localAddr, endpoint);\n      socket.bind(localAddr);\n    }\n\n    try {\n      if (ch == null) {\n        // let the default implementation handle it.\n        socket.connect(endpoint, timeout);\n      } else {","sourceCodeStart":576,"sourceCodeEnd":612,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java#L576-L612","documentation":"IllegalArgumentException('Illegal argument for connect()') from NetUtils.connect — a pure argument sanity check before any network activity: socket is null, endpoint (SocketAddress) is null, or timeout is negative. It is a guard against programming/config errors rather than a network condition; the actual connection attempt happens after this check and reports real failures as IOExceptions.","triggerScenarios":"Passing a null endpoint obtained from an unset address property or a failed lookup; computing timeout from configuration where a negative default (-1) leaks through; calling connect with a socket variable that was never initialized.","commonSituations":"Code paths where the endpoint came from NetUtils.createSocketAddr on a missing config; retry loops that null out the socket/endpoint between iterations; a timeout constant configured as -1 meaning 'infinite' passed to an API that requires >= 0.","solutions":["Check for null socket/endpoint and non-negative timeout before calling connect","Trace where the null endpoint came from — usually an unset configuration property; set it or fail with a clearer message","Use 0 for an infinite/system timeout instead of negative values"],"exampleFix":"// before\nNetUtils.connect(sock, null, 0); // null endpoint -> IllegalArgumentException\n\n// after\nPreconditions.checkNotNull(endpoint, \"endpoint is null (check the address config)\");\nPreconditions.checkArgument(timeout >= 0, \"timeout must be >= 0, got %s\", timeout);\nNetUtils.connect(sock, endpoint, timeout);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(socket, \"socket\");\nObjects.requireNonNull(endpoint, \"endpoint\");\nif (timeout < 0) throw new IllegalArgumentException(\"timeout >= 0 required, got \" + timeout);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check nullness of socket/endpoint and timeout sign before every connect call","Trace null endpoints to their config source instead of special-casing","Use 0 (or the documented infinite value) rather than negative timeouts"],"tags":["socket","connect","argument-validation","hadoop-common"],"backgroundTag":"invalid-connect-arguments","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}