Skip to content

testing/fs/filelock: Add a test for a signal during a file lock wait - #4017

Merged
acassis merged 1 commit into
apache:masterfrom
casaroli:apps-setlkw-test-up
Nov 11, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
casaroli:apps-setlkw-test-up

Conversation

@casaroli

@casaroli casaroli commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

No test in nuttx-apps covers a wait for a file lock. This PR adds testing/fs/filelock. A thread waits for a write lock that the main thread holds, with fcntl(F_SETLKW), flock(LOCK_EX) and lockf(F_LOCK). A signal to the thread (pthread_sigqueue(), a handler without SA_RESTART) must end the wait with EINTR before the lock is free, as POSIX asks for fcntl(). The test also checks that F_SETLK, flock(LOCK_EX | LOCK_NB) and lockf(F_TLOCK) fail on a held lock, and that a wait without a signal gets the lock after the unlock.

The main thread always unlocks after 2 seconds, so the test fails and does not hang when the signal does not end the wait. The program prints PASS or FAIL for each of the 13 checks and returns the number of errors.

Impact

Test only. The new option TESTING_FILELOCK is off by default, as the other tests in testing/fs, and depends on FS_LOCK_BUCKET_SIZE > 0, !DISABLE_PTHREAD and !DISABLE_ALL_SIGNALS. No defconfig sets it, so CI does not build or run it. The argument is the lock file (default /tmp/filelock.lck); its file system must support F_GETPATH (tmpfs, FAT, littlefs, v9fs, hostfs, ...).

The cmocka fs tests are not a good place for this case: the configurations that enable them (rv-virt:citest, sim:citest) have FS_LOCK_BUCKET_SIZE=0, so the case would be compiled out there too.

Testing

filelock /lk/lock on a tmpfs, nuttx master 80a052ff9cc vs master + apache/nuttx#21651, apps master ee3d671 + this PR, CONFIG_FS_LOCK_BUCKET_SIZE=16, EXTRAFLAGS="-Wno-cpp -Werror":

Configuration nuttx master with apache/nuttx#21651 ostest
qemu-armv8a:knsh (kernel, arm64, QEMU HVF), make 6 errors 0 errors status 0 (both)
qemu-armv8a:nsh (flat, arm64, QEMU HVF), make 6 errors 0 errors status 0 (both)
mps2-an500:knsh (protected, Cortex-M7, QEMU TCG), make 6 errors 0 errors status 0 (both)
qemu-armv8a:nsh (flat), CMake not run 0 errors status 0

On master fcntl(F_SETLKW), flock(LOCK_EX) and lockf(F_LOCK) all return 0 after 2001 ms, when the main thread unlocks: the handler ran, but the wait went on (eintr and early fail for each call). With the fix each call returns -1 with EINTR after about 301 ms. The busy, handler and wait checks pass with both. The builds have no warnings. tools/checkpatch.sh -c -u -m -g origin/master..HEAD passes.

Not run: SMP, other architectures.

A thread waits for a file lock that the main thread holds, with
fcntl(F_SETLKW), flock(LOCK_EX) and lockf(F_LOCK).  A signal to the
thread must end the wait with EINTR before the lock is free, as POSIX
asks for fcntl().  The test also checks that the calls that do not wait
fail on a held lock, and that a wait without a signal gets the lock
after the unlock.  The main thread always unlocks, so the test does not
hang.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@xiaoxiang781216
xiaoxiang781216 marked this pull request as ready for review November 11, 2026 01:45
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/42941207688

@acassis
acassis merged commit 0943949 into apache:master Nov 11, 2026
74 of 88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants