EDEADLK
Linux / POSIXERRORCriticalLocksMEDIUM confidence

Resource Deadlock Avoided

Production Risk

Deadlocks between long-running processes can halt production workflows. Add lock acquisition timeouts and deadlock detection.

What this means

The kernel detected that granting the requested lock would result in a deadlock with existing locks held by other processes. This is returned by fcntl POSIX file locking when the lock dependency graph forms a cycle.

Why it happens
  1. 1Process A holds a lock on file1 and is waiting for file2, while Process B holds file2 and is waiting for file1.
  2. 2Multiple processes acquire file locks in inconsistent orders.
  3. 3A database or file-locking library is using POSIX locks incorrectly.
How to reproduce

Two processes waiting for locks held by each other.

trigger — this will error
trigger — this will error
// Process A:
fcntl(fd_file1, F_SETLKW, &lock);  // holds file1
fcntl(fd_file2, F_SETLKW, &lock);  // waits for file2 -> EDEADLK

// Process B simultaneously:
fcntl(fd_file2, F_SETLKW, &lock);  // holds file2
fcntl(fd_file1, F_SETLKW, &lock);  // waits for file1

expected output

fcntl(F_SETLKW) returned -1, errno = EDEADLK (Resource deadlock avoided)

Fix

Acquire all locks in a globally consistent order

WHEN When multiple processes lock multiple resources

Acquire all locks in a globally consistent order
// Always acquire locks in sorted order by resource ID
void lock_two_files(int fd_a, int fd_b) {
  if (fd_a < fd_b) {
    lock(fd_a); lock(fd_b);
  } else {
    lock(fd_b); lock(fd_a);
  }
}

Why this works

A consistent global ordering of lock acquisition eliminates the possibility of circular wait, preventing deadlock.

What not to do

Ignore EDEADLK and retry immediately

The deadlock condition will persist until one of the processes releases its locks. Retrying without releasing locks will not resolve the deadlock.

Sources

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

← All Linux / POSIX errors