| Previous | Next |
| ELIBEXEC | EBADMACHO |
ENOTSOCK
ENOTSOCK means that an API requiring a socket received a file descriptor for some other object. The descriptor can be a regular file, pipe, event descriptor, device, or a number whose original socket lifetime has already ended. It differs from EBADF: EBADF says the descriptor is invalid or closed, while ENOTSOCK says it is valid enough to identify an object, but that object is not a socket.
In multithreaded or asynchronous code, this is often a lifetime and ownership problem. One path closes a socket while another retains the integer value; the kernel may then reuse that number for an unrelated descriptor. Merely logging the number at the failing call is insufficient unless acquisition, duplication, handoff, and close are also recorded.
How to narrow it down
- Record the descriptor's creation site, owner, duplications, transfers between threads, and every close attempt.
- Verify the resource type before invoking socket-specific operations;
fstat()and controlled ownership metadata are more useful than guessing from a descriptor number. - Check whether an abstraction accepts both generic file descriptors and sockets, then ensure that its call path enforces the required resource kind.
- Do not retry a socket operation on the same descriptor until its identity and lifetime have been established.
Linux getsockopt(2) · Linux shutdown(2) · POSIX sendto()
Looking for a different code? Search another status or error code.