What does errno 9 (EBADF) mean?

 
Could be also:
ConstantTypeOS
ERROR_INVALID_BLOCKWin32 errorWindows
KERN_MEMORY_FAILUREKern returnMac
ippStsNanArgIntel Ipp StatusAny
IRQL_NOT_GREATER_OR_EQUALBugCheck CodeWindows
Previous Next
ENOEXEC ECHILD

EBADF

Why a descriptor becomes bad

EBADF commonly means that a file descriptor number is no longer open, was never initialized, was closed by another control path, or does not have the access mode required by the call. For example, a descriptor opened write-only cannot satisfy a read request. APIs that accept a directory descriptor can also return EBADF when a relative path is paired with an invalid dirfd.

In asynchronous and multithreaded software, this error is often a lifetime bug: one thread closes or reuses a descriptor while another still believes it owns it. Reuse makes the defect especially dangerous because the same integer can later refer to a different resource.

How to investigate

  • Log descriptor acquisition, duplication, handoff, and close operations with the owning component and thread.
  • Check whether the API requires read, write, seek, directory, socket, or device semantics rather than merely any open descriptor.
  • Define one owner for close, or use explicit synchronization and reference-counted lifetime rules around handoff.
  • Do not retry the call with the same number until the ownership and resource identity are verified.

References


Looking for a different code? Search another status or error code.