Public bug reported:

As a developer, I found a counter-intuitive behavior in lockf function
provided by glibc and Linux kernel that is likely a bug.

In glibc, lockf function is implemented on top of fcntl system call: 
https://github.com/lattera/glibc/blob/master/io/lockf.c
man page says that lockf can sometimes detect deadlock: 
http://manpages.ubuntu.com/manpages/xenial/man3/lockf.3.html
Same with fcntl(F_SETLKW), on top of which lockf is implemented: 
http://manpages.ubuntu.com/manpages/hirsute/en/man3/fcntl.3posix.html

Deadlock detection algorithm 
(https://github.com/torvalds/linux/blob/master/fs/locks.c) seems buggy because 
it can easily give false positives. Suppose we have two processes A and B, 
process A has threads 1 and 2, process B has threads 3 and 4. When this 
processes execute concurrently, following sequence of actions is possible:
1. processA thread1 gets lockI
2. processB thread2 gets lockII
3. processA thread3 tries to get lockII, starts to wait
4. processB thread4 tries to get lockI, kernel detects deadlock, EDEADLK is 
returned from lockf function

Steps to reproduce this scenario (see attached file):
1. gcc -o edeadlk ./edeadlk.c -lpthread
2. Launch "./edeadlk a b" in first terminal window.
3. Launch "./edeadlk a b" in second terminal window.

What I expected to happen: two instances of the program is steadily
working.

What happened instead:
Assertion failed: (lockf(fd, 1, 1)) != -1 file: ./edeadlk.c, line:25, errno:35 
. Error:: Resource deadlock avoided
Aborted (core dumped)

Surely, this behavior is kind of "right". lockf file locks belongs to
process, so on the process level it seems that deadlock is just about to
happen: process A holds lockI and waits for lockII, process B holds
lockII and is going to wait for lockI. However, algorithm in kernel
doesn't take threads into account. In fact, deadlock is not gonna happen
here if thread scheduler will give control to some thread holding a
lock.

I think there's a problem with deadlock detection algorithm because it's
overly pessimistic, which in turn creates problems -- lockf errors in
applications.

# lsb_release -rd
Description:    Ubuntu 20.04.1 LTS
Release:        20.04

# uname -a
Linux test-x64-ub20 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 
2020 x86_64 x86_64 x86_64 GNU/Linux

** Affects: linux (Ubuntu)
     Importance: Undecided
         Status: New

** Attachment added: "test to reproduce the problem"
   https://bugs.launchpad.net/bugs/1926481/+attachment/5493082/+files/edeadlk.c

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/1926481

Title:
  lockf returns false-positive EDEADLK in multiprocess multithreaded
  environment

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1926481/+subscriptions

-- 
ubuntu-bugs mailing list
ubuntu-bugs@lists.ubuntu.com
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to