Skip to content

test(storage-sqlite): read POSIX locks from /proc/self/fdinfo in the copy tests - #1059

Merged
eliseong merged 1 commit into
mainfrom
fix/sqlite-lock-probe
Nov 20, 2026
Merged

eliseong merged 1 commit into
mainfrom
fix/sqlite-lock-probe

Conversation

@LeeroyHannigan

Copy link
Copy Markdown
Collaborator

What

posix_locks is the helper that the two copy lock tests in crates/storage-sqlite/src/migrations/refusal_tests.rs use. It now reads the lock: lines of /proc/self/fdinfo instead of /proc/locks, keeps the POSIX lines that name the database's inode, and returns them as a set. The change is test code only.

Why

The kernel returns /proc/locks one page (about 4 KB) per read, and each read starts again at a position in the lock list. When another test in the binary or another process takes or releases a lock between two reads, the entries after it move. A read can then skip the runner's lock or list it twice, and the tests fail. CI run 43873705049 for #597 failed this way.

The kernel writes the locks of one file into its fdinfo in one pass, under the spinlock of that file's lock list. A read of fdinfo opens no descriptor on the database, so it releases no lock. A duplicated descriptor lists its file's locks again, so the helper returns a set. Linux added the fdinfo lock: lines in 4.1, and the runner of the failed job had Linux 7.0.

Closes #1058

Testing done

  • cargo test -p extenddb-storage-sqlite --lib with the two tests, the_copy_leaves_the_runners_lock_on_the_database and a_link_to_the_database_under_a_copys_name_is_not_opened: 2 passed. cargo fmt --all --check is clean.
  • Four other processes took and released POSIX locks in a loop while each test ran 100 times. With the old helper the two tests failed 10 and 14 times. With this helper both passed 100 of 100. Without the other processes, both helpers passed 100 of 100.
  • A probe with the same helper code closed a second descriptor on the database. The close released the lock, and the helper then returned an empty set. So the tests still fail when the copy releases the runner's lock.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy -- -W clippy::pedantic)
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, or public CLI surface, an RFC has been accepted or is linked
    below. Otherwise, an ADR captures the decision (link below).

ADR / RFC: n/a


By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.

…copy tests

Two copy tests in refusal_tests.rs read this process's locks on the
database from /proc/locks. The kernel returns /proc/locks one page per
read, and each read starts again at an index into the lock list. A lock
that another test or process takes or releases between two pages moves
the entries after it. So a read can skip an entry or list one twice.
CI run 43873705049 for #597 failed the setup check "the runner's
connection holds no lock" this way.

posix_locks now reads the lock: lines of /proc/self/fdinfo. The kernel
writes the locks of one file there in one pass, under the spinlock of
that file's lock list. A read of fdinfo opens no descriptor on the
database, so it releases no lock. The helper returns a set, because a
duplicated descriptor lists its file's locks again.

Four other processes took and released locks during each loop of 100
runs. With the old helper the two tests failed 10 and 14 times. With
this helper both passed 100 of 100. Without the other processes, both
helpers passed 100 of 100.

@eliseong eliseong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

I ran the two tests 50 times each on a Linux 5.10 aarch64 host. Four other processes took and released POSIX locks in a loop, and 16 processes kept the CPUs busy.

  • On main, 11 of 50 runs failed. Three failures were "the runner's connection holds no lock", at lines 475 and 512. The others were "the copy released the runner's lock" and "the search released the runner's lock".
  • On this branch, 0 of 50 runs failed.

The new helper still finds a released lock. I added drop(std::fs::File::open(db)) at the start of pre_migration_copy, and the copy test failed at line 480 with left: {}. The same line at the start of remove_superseded made the link test fail at line 523 with left: {}.

In fs/locks.c, show_fd_locks lists the locks of one file under flc_lock in one pass. It keeps only locks set through that file by this process, so the pid check is not necessary. The kernel merges POSIX locks of one owner on one file, so the set loses no entry.

No other code reads /proc/locks. cargo fmt and clippy with -D warnings on Linux are clean.

@eliseong eliseong self-assigned this Nov 20, 2026
@eliseong
eliseong added this pull request to the merge queue Nov 20, 2026
Merged via the queue into main with commit 2cfe490 Nov 20, 2026
58 checks passed
@eliseong
eliseong deleted the fix/sqlite-lock-probe branch November 20, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky: the SQLite copy lock tests can miss the runner's lock in /proc/locks

2 participants