← LeaderboardThe Skill Leaderboard
rust-file-lock-lifetime
Keep a Rust file lock alive for the protected operation. Verify a competing process sees contention until the lock owner releases it.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: rust-file-lock-lifetimedescription: Keep a Rust file lock alive for the protected operation. Verify a competing process sees contention until the lock owner releases it.category: developmentrisk: criticalsource: selfsource_type: selfauthor: ShianMikedate_added: "2026-12-03"tags: [rust, files, locking, windows, testing]Rust file lock lifetime
When to Use
- A Rust helper calls
File::locksuccessfully but another process obtains the same lock while the caller runs. - An application needs to keep a file lock across a complete read or publication operation.
- A failure to lock must be distinguished from normal contention.
Prerequisites
- Identify the file that coordinates access and where each participating caller opens it.
- Confirm that each participant uses a compatible locking API and the same file identity.
- Use Rust 1.89 or later for the standard-library locking APIs in the example.
Workflow
- Trace the file handle lifetime. A helper returning
()can drop its only handle before the protected operation begins. Reproduce with an independently opened file in another local process. - Retain the owning handle. Return the open locked
Fileor hold it in the scope that performs the protected work. Include duplicated or inherited handles in the lifetime analysis. For explicit early release, useunlockand propagate its error. - Preserve the coordination file. Use compatible open modes and a stable file identity for each participant. If a separate lock file protects an atomically replaced payload, keep that lock file stable while replacing the payload.
- Probe without blocking. Use
try_lockin the second process. DistinguishWouldBlockfrom any other I/O error. Test before the lock, during protection, and after release. - Check the actual integration. Verify the full mutation runs while the owner is held. Review platform behavior, cleanup on errors, and the application's wait or retry policy.
Example
Run Test the lock with another process. The fixture reproduces a lock released at a helper boundary, then verifies contention while the correct owning handle remains live.
Limitations
- File-lock interaction with ordinary reads and writes depends on the platform. Treat this as coordination among participating lockers.
- The fixture uses a single file on a local Windows filesystem. Network filesystems and alternative lock libraries need separate verification.
- Reacquiring with the same handle or a clone can have platform-specific behavior. Probe through independently opened handles.
Safety and Side Effects
- Run the example in an empty scratch directory. It creates a new fixture lock file and starts its own local executable as a contender.
- Keep the existing access policy and lock-file identity when adapting the example.
- Check the lifetime on errors and returns before expanding a critical section.