A gotcha with fcntl file locking

Today I ran into an issue with fcntl(2) file locking [1] on Linux. A little research confirmed my fear:

From “On the Brokenness of File Locking”:

The worst part however is that POSIX locks are automatically released if a process calls close() on any (!) of its open file descriptors for that file.

The whole post is well worth a read.

[1] Which is what OpenJDK uses for FileChannel#lock.