Systems Concept

The Lifetime of a File Descriptor

A file descriptor lasts until it is closed, until the process exits, or until an exec replaces the process and the descriptor is close-on-exec; closing it only drops one reference, and the file's data disappears when its last link and last open reference are gone.

The short answer

A file descriptor is a small integer index into a per-process table. Closing it removes that entry and drops one reference to the kernel object behind it, called the open file description. The file's data remains available while a directory entry still links the inode, or while any open file description still references it. A descriptor is closed by close(), by process exit, or implicitly by execve() when FD_CLOEXEC is set.

A minimal example

import os

fd = os.open("/tmp/fd-check", os.O_CREAT | os.O_RDWR, 0o600)
os.write(fd, b"hello")
os.unlink("/tmp/fd-check")        # remove the only directory entry
os.lseek(fd, 0, os.SEEK_SET)
print(os.read(fd, 5))            # b'hello': the data is still reachable
os.close(fd)                     # last reference; space can be reclaimed

Duplicated descriptors share one open file description, including its offset:

a = os.open("/tmp/fd-check2", os.O_CREAT | os.O_RDWR, 0o600)
os.write(a, b"abcdef")
b = os.dup(a)
os.lseek(a, 0, os.SEEK_SET)
print(os.read(b, 3))                 # b'abc'
print(os.lseek(a, 0, os.SEEK_CUR))   # 3: a and b share the offset

Two separate open() calls create independent descriptions with independent offsets.

Why this happens

Layer Lifetime Shared by
Descriptor Until close, close-on-exec, or exit Threads of one process
Open file description Until the last reference is closed dup() copies and fork() children
Inode Until the last link and last reference are gone Every open of the file

dup() and fork() copy descriptor table entries that point at the same open file description, so they share the offset and status flags. fcntl(F_DUPFD_CLOEXEC) duplicates with close-on-exec set in one step. A fresh open() creates a new description. Memory mappings also hold a reference to the file, so a mapped file stays valid after unlink().

Linux exposes unlinked-but-open data through /proc/<pid>/fd/<n>; macOS exposes it through /dev/fd/<n>. The storage is not reclaimed until the last reference disappears, which is why df can report a full disk while du finds nothing.

What changes the answer

  • O_CLOEXEC or FD_CLOEXEC prevents a descriptor from leaking into a program run by execve(). Set it at open time; setting it with fcntl() after open() leaves a race in multithreaded programs.
  • dup2() closes its target before duplicating, which is how shells implement redirection.
  • Threads share the descriptor table, so one thread closing a descriptor affects all of them.
  • Pipes and sockets have no directory entry. Their lifetime ends when the last descriptor closes; writing to a pipe whose read end is closed raises EPIPE and usually SIGPIPE.
  • Passing a descriptor over a Unix socket with SCM_RIGHTS creates a reference in another process.
  • On Linux, close() releases the descriptor even when it reports an error. Retrying after EINTR can therefore close a descriptor number that has already been reused; POSIX leaves the state unspecified, so portable code should avoid retrying.

Practical checks

ls -l /proc/<pid>/fd lists open descriptors and shows (deleted) for unlinked files; lsof -p <pid> works on Linux and macOS, and lsof +L1 finds unlinked files that are still open. In Python, an unclosed file raises ResourceWarning. Leaked descriptors accumulate until open() fails with EMFILE ("Too many open files"). Descriptors and the current working directory are both inherited across execve(), which is why both need deliberate handling in long-running programs.

Sources

Revision history

Published
Updated
— Initial publication.