{"record":{"id":"a1d6dddec4f5df8b","repo":"apache/hadoop","slug":"ebadf","errorCode":"EBADF","errorMessage":"The handle is invalid.","messagePattern":"The handle is invalid\\.","errorType":"exception","errorClass":"NativeIOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/nativeio/NativeIO.java","lineNumber":589,"sourceCode":"    /**\n     * Returns the file stat for a file descriptor.\n     *\n     * @param fd file descriptor.\n     * @return the file descriptor file stat.\n     * @throws IOException thrown if there was an IO error while obtaining the file stat.\n     */\n    public static Stat getFstat(FileDescriptor fd) throws IOException {\n      Stat stat = null;\n      if (!Shell.WINDOWS) {\n        stat = fstat(fd); \n        stat.owner = getName(IdCache.USER, stat.ownerId);\n        stat.group = getName(IdCache.GROUP, stat.groupId);\n      } else {\n        try {\n          stat = fstat(fd);\n        } catch (NativeIOException nioe) {\n          if (nioe.getErrorCode() == 6) {\n            throw new NativeIOException(\"The handle is invalid.\",\n                Errno.EBADF);\n          } else {\n            LOG.warn(String.format(\"NativeIO.getFstat error (%d): %s\",\n                nioe.getErrorCode(), nioe.getMessage()));\n            throw new NativeIOException(\"Unknown error\", Errno.UNKNOWN);\n          }\n        }\n      }\n      return stat;\n    }\n\n    /**\n     * Return the file stat for a file path.\n     *\n     * @param path  file path\n     * @return  the file stat\n     * @throws IOException  thrown if there is an IO error while obtaining the\n     * file stat","sourceCodeStart":571,"sourceCodeEnd":607,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/nativeio/NativeIO.java#L571-L607","documentation":"On Windows, NativeIO.getFstat(FileDescriptor) maps Win32 error 6 (ERROR_INVALID_HANDLE) to NativeIOException(\"The handle is invalid.\", Errno.EBADF). The FileDescriptor passed in is closed, stale, or not resolvable to a valid OS handle by the native layer.","triggerScenarios":"Calling getFstat with a FileDescriptor captured from a stream that was already closed(); reusing a fd stored after its owner closed; a fd fabricated or mocked rather than obtained from a real open file.","commonSituations":"Close-then-stat ordering bugs (stat after finally-close); fd cached across operations and outliving its stream; tests constructing fake FileDescriptors.","solutions":["Reorder so the fstat happens while the stream is still open — stat first, then close.","Audit fd ownership: exactly one owner, no caching across close(), close in finally at the end of use.","In tests use real temporary files (JUnit TemporaryFolder) instead of fabricated descriptors."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  Stat st = NativeIO.getFstat(fd);\n} catch (NativeIOException e) {\n  if (e.getErrno() == Errno.EBADF) {\n    // fd closed/invalid: reopen the file or skip — a lifecycle bug, not transient\n  } else { throw e; }\n}","preventionTips":["Stat within the scope where the owning stream is guaranteed open.","Never cache FileDescriptor across close().","Treat EBADF as a lifecycle defect to fix, not an error to retry."],"tags":["native-io","windows","fstat","file-descriptor","ebadf"],"backgroundTag":"ebadf-bad-file-descriptor","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}