Skip to content

test(remove): retry the drain when a forked child still holds the trash lock - #4530

Merged
max-sixty merged 4 commits into
mainfrom
fix/ci-39376521570
Oct 20, 2026
Merged

max-sixty merged 4 commits into
mainfrom
fix/ci-39376521570

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

The nightly feature-powerset job failed in git::remove::tests::drain_trash_deletes_staged_entries_once_the_lock_is_free (added in #4516) with assertion failed: staged_trash_entries(&trash).is_empty(). This is a race in the test, not a bug in the drain. After the test drops its lock holder, it expects the next drain_trash to get the lock. But another test in the same process can fork a child while the holder is open. That child inherits the descriptor, and the flock stays held until it execs. The drain sees WouldBlock and exits, leaving the deletion to the holder, as designed. test_configure_shell_opens_rc_with_exclusive_lock has the same race on the rc-file lock.

Both tests now wait for the release through the shared testing::wait_for helper. It polls with a bounded timeout and stops at the first success, so on Windows the test never asks a handle that already holds the lock to take it again.

Verification: I reproduced the race with a scratch test that ran the same lock/release/drain sequence 2000 times while four threads spawned true in a loop. The first drain left the entry behind in 17–20 of the 2000 iterations. With the retry, no iteration left it. Both tests pass locally, and clippy is clean.

The same window exists in production, but it only delays cleanup: a staged entry waits in the trash until the next removal's drain. The doc comment on trash_drain_running already accepts a similar window, so this PR doesn't change the drain itself.

Refs #4509

…sh lock

drain_trash_deletes_staged_entries_once_the_lock_is_free failed in the
nightly feature-powerset job. A child that a concurrent test forks while
the test holds the trash lock inherits the flock until it execs, so the
drain after the release can still find the lock held and exit as
designed, leaving the staged entry. Retry the drain until the entries
are gone, bounded at 10s.
@worktrunk-bot worktrunk-bot added the automated-fix Automated CI fix label Oct 20, 2026

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

test_configure_shell_opens_rc_with_exclusive_lock in src/commands/configure_shell.rs has the same race. It holds an fs2 flock and drops it, then calls contender.try_lock_exclusive().unwrap() once. A child that a concurrent test forks while locked is open inherits the lock and keeps it until it execs, so that unwrap can panic. I'll push the same deadline retry there so both tests are fixed in this PR.

…lds it

Same fork-inherits-flock race as the trash drain test: a child forked by a
concurrent test while the lock is held keeps it until exec, so the first
try_lock_exclusive after release can fail.
…twice

Windows LockFileEx is not reentrant, so a second try_lock_exclusive after
the loop's successful one fails.
…lper

Both lock-release tests now poll through testing::wait_for, which the
test conventions name for eventual conditions. It stops at the first
successful attempt, so the Windows relock can't collide with a lock the
handle already holds, and it leaves no uncovered retry body in either
test.
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

The same race recurred on main: the nightly feature-powerset run at d890a16 failed drain_trash_deletes_staged_entries_once_the_lock_is_free with the same assertion failed: staged_trash_entries(&trash).is_empty() at src/git/remove.rs:882, while the other 1590 lib tests passed. That commit (#4524) doesn't touch the trash drain, so this PR's retry covers it.

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

The race failed main again in the next nightly feature-powerset run, at 0559edc (#4533). It hit the same assertion at src/git/remove.rs:882, this time in the cli,shell-integration-tests,syntax-highlighting combination; the other 1592 lib tests passed. That's three failed nightlies in a row from this one test. #4533 changes signal handling, not the trash drain, so this PR's retry covers it, and the PR's checks are still green.

@max-sixty
max-sixty merged commit a4a3063 into main Oct 20, 2026
37 checks passed
@max-sixty
max-sixty deleted the fix/ci-39376521570 branch October 20, 2026 20:49
max-sixty pushed a commit that referenced this pull request Nov 1, 2026
The `feature-powerset` check on #4597 failed because
`background_removal_preserves_native_path_arguments` expected staged
trash to be empty immediately after removal. A concurrently forked child
can briefly inherit the trash drain lock, so the test's one cleanup
attempt may leave the staged entry behind. The test now uses the bounded
`wait_for` helper to retry cleanup and still verifies the checkout and
registration are removed. This follows the same accepted fix for another
drain test in #4530.

The [failed feature powerset
job](https://github.com/max-sixty/worktrunk/actions/runs/40644576267/job/122670315541)
exercised the original assertion. The targeted test passes locally after
the change. The full `cargo run -- hook pre-merge --yes` gate ran 5,463
tests and reported seven failures outside this test; some require socket
operations unavailable in this sandbox. CI on this PR will provide the
complete verification.

Co-authored-by: worktrunk-bot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants