File Descriptor in Bad State
Production Risk
Indicates mismanaged fd lifecycle; review shutdown and close patterns.
EBADFD (errno 77) is returned when a file descriptor is in an invalid state for the requested operation — for example, writing to an fd after a half-close, or certain ioctl operations on a partially initialized fd.
- 1Writing to a socket fd after shutdown(SHUT_WR)
- 2ioctl on an fd in an intermediate state
- 3Attempting operations on an fd in an error state
Write to a socket after shutdown().
shutdown(sockfd, SHUT_WR); // half-close // Attempt to send more data send(sockfd, buf, len, 0); // Returns -1, errno = EBADFD or EPIPE
expected output
send: File descriptor in bad state (EBADFD)
Fix
Check fd state before operating
WHEN When managing socket half-close or fd lifecycle
// Track fd state in your application
if (!write_shutdown) {
send(sockfd, buf, len, 0);
}Why this works
Maintain application-level state flags to avoid operating on a fd after shutdown or error.
✕ Reuse an fd after close() or after a fatal error
The fd may be reused for another resource by the kernel; operating on it corrupts the new resource.
Linux Programmer Manual errno(3)
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev