EBADFD
Linux / POSIXERRORNotableFilesystemHIGH confidence

File Descriptor in Bad State

Production Risk

Indicates mismanaged fd lifecycle; review shutdown and close patterns.

What this means

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.

Why it happens
  1. 1Writing to a socket fd after shutdown(SHUT_WR)
  2. 2ioctl on an fd in an intermediate state
  3. 3Attempting operations on an fd in an error state
How to reproduce

Write to a socket after shutdown().

trigger — this will error
trigger — this will error
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

Check fd state before operating
// 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.

What not to do

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.

Sources
Official documentation ↗

Linux Programmer Manual errno(3)

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Linux / POSIX errors