Stale File Handle
Production Risk
Common with NFSv3 after server reboots; implement ESTALE-aware retry logic.
ESTALE (errno 116) is returned by NFS when a file handle is stale — meaning the file or directory it referred to no longer exists on the NFS server, or the server was rebooted and state was lost.
- 1NFS server rebooted — stateful file handles are invalidated
- 2File or directory deleted on the server while a client still holds an open fd or path reference
- 3NFS server failover that changes file handle mappings
read() on an NFS file after the server rebooted.
// File handle valid before server reboot ssize_t n = read(nfs_fd, buf, sizeof(buf)); // Returns -1, errno = ESTALE after server reboot
expected output
read: Stale file handle (ESTALE)
Fix 1
Close, reopen, and retry the operation
WHEN When ESTALE is returned on an NFS fd
if (errno == ESTALE) {
close(fd);
// Re-open the file by path
fd = open(filepath, O_RDWR);
if (fd >= 0) {
lseek(fd, offset, SEEK_SET);
read(fd, buf, sizeof(buf));
}
}Why this works
Closing and reopening re-establishes the file handle with the server.
Fix 2
Use NFS v4 with session recovery
WHEN For production NFS deployments
# Mount with NFSv4 for better server reboot recovery mount -t nfs -o vers=4 server:/export /mnt
Why this works
NFSv4 has session-based recovery that reduces ESTALE occurrences after server restarts.
✕ Retry the same fd after ESTALE
The file handle is permanently invalidated; the fd must be closed and reopened.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev