From: Trond Myklebust <trondmy@hammerspace.com>
To: "linux-nfs@vger.kernel.org" <linux-nfs@vger.kernel.org>,
"tychokirchner@mail.de" <tychokirchner@mail.de>
Subject: Re: Parallel shared to exclusive flock conversion blocks forever on single NFS client
Date: Wed, 12 Mar 2025 22:57:50 +0000 [thread overview]
Message-ID: <05ce8da1909fd21c2511abf1d21536a872077324.camel@hammerspace.com> (raw)
In-Reply-To: <d27d885e-568c-42b8-a204-2f4a3e949d64@mail.de>
On Wed, 2025-03-12 at 22:57 +0100, Tycho Kirchner wrote:
> Dear NFS kernel developers,
> In `man 2 flock` it is documented, that an existing lock can be
> converted to a new lock mode. Multiple processes on the *same* client
> converting their LOCK_SH to LOCK_EX quickly results in a deadlock of
> the
> client processes. This can already be reproduced on a single physical
> machine, with for instance the NFS server running in a VM and the
> host
> machine connecting to it as a client.
>
> Steps to reproduce:
> - Setup a virtual machine with Virtualbox and install NFS-server
> - Create an /etc/export: /home/VMUSER/nfs 10.0.2.2(rw,async)
> - Create a NAT firewall rule forwarding NFS port 2049 to the VM
> - Mount the export on the host, chdir it and create an empty file:
> $ sudo mount -t nfs 127.0.0.1:/home/VMUSER/nfs /somedir
> $ cd /somedir
> $ touch foo
> - Execute below attached ~/locktest.py in parallel on the client:
> $ for i in {1..10}; do ~/locktest.py foo & done; wait
> - Wait half a minute. The command does not terminate. Ever.
> - Abort execution with Ctrl+C and kill leftovers: pkill -f
> locktest.py
>
> Notes:
> - According to my tests, from three concurrent client-processes
> onwards,
> the block quickly occurs.
> - Placing a `fcntl.flock(a, fcntl.LOCK_UN)` before fcntl.LOCK_EX is
> enough, so the deadlock never occurs.
> - OR'ing `| fcntl.LOCK_NB` quickly results in endless
> »BlockingIOError«
> exceptions with no client process making any progress. See the also
> attached ~/locktest_NB.py.
> - Multiple distributions, Kernelversions and combinations tested,
> e.g.
> NFS-client KVER 6.6.67 on Debian12 and KVER 6.12.17-amd64 on
> DebianTesting, or KVER 6.4.0-150600.23.38-default on openSUSE Leap
> 15.6.
> The error was always and quickly reproducible.
>
The same manpage also states:
Converting a lock (shared to exclusive, or vice versa) is not guaranteed
to be atomic: the existing lock is first removed, and then a new lock is
established. Between these two steps, a pending lock request by another
process may be granted, with the result that the conversion either
blocks, or fails if LOCK_NB was specified. (This is the original BSD
behavior, and occurs on many other implementations.)
so there is no harm in adding the LOCK_UN because you cannot expect
atomicity.
Cheers
Trond
--
Trond Myklebust
Linux NFS client maintainer, Hammerspace
trond.myklebust@hammerspace.com
next prev parent reply other threads:[~2025-03-12 22:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-12 21:57 Parallel shared to exclusive flock conversion blocks forever on single NFS client Tycho Kirchner
2025-03-12 22:57 ` Trond Myklebust [this message]
2025-03-13 9:37 ` Tycho Kirchner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=05ce8da1909fd21c2511abf1d21536a872077324.camel@hammerspace.com \
--to=trondmy@hammerspace.com \
--cc=linux-nfs@vger.kernel.org \
--cc=tychokirchner@mail.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox