I have a situation where one processes locks a particular file with Python's fcntl
module, by doing lockf(fd, LOCK_SH)
. I have another process SIGKILL it, wait
for that process to die, and then more or less immediately lockf(fd, LOCK_EX | LOCK_NB)
the same file.
Something like this:
import os
import fcntl
import time
import sys
import signal
pid = os.fork()
if pid:
time.sleep(1)
os.kill(pid, signal.SIGKILL)
os.waitpid(pid, 0)
fcntl.lockf(os.open(sys.argv[0], os.O_WRONLY), fcntl.LOCK_EX | fcntl.LOCK_NB)
else:
fcntl.lockf(os.open(sys.argv[0], os.O_RDONLY), fcntl.LOCK_SH)
time.sleep(1000)
The file is on a normal ext4 local filesystem, and all the processes are on the same machine, so bad lock implementations on weird filesystems are not a concern here.
I want to know if it is possible in theory for the second lock to fail, given sufficiently pathological scheduling.
- Does POSIX guarantee that a process is truly dead and buried when you wait on it and get its exit code? Or can some cleanup for the process still be pending?
- Does POSIX guarantee any sort of happens-before ordering between the death of a process and the resolution of locks it was holding at death?
- What is the actual behavior of the current Linux kernel with regard to ordering process death and file unlocking when a process is killed?