Files
codex/codex-rs/rollout/src/writer_lock_tests.rs
T
Dmitriy Kovalenko e428b9a16b Reduce contention in thread writer lock coordination (#59969)
## Why

Exclusive coordination serialized writer acquisition across unrelated threads and could delay releasing writer ownership while waiting to remove a lock file.

## What changed

Use shared coordination when opening thread writer locks. Attempt the initial stale-lock sweep only when exclusive coordination is immediately available, and keep rollout publication exclusively coordinated.

When dropping a writer guard, remove its lock file only if exclusive coordination can be acquired immediately. Otherwise, release ownership and leave the file available for reuse or later cleanup.

## Testing

Extend the writer-lock test to cover acquisition for different threads under shared coordination, rejection of competing owners for the same thread, and prompt ownership release and reacquisition when cleanup cannot acquire exclusive coordination.

GitOrigin-RevId: 3e187b32dbef3bcfdd8c62e91eca58b615f3879e
2026-11-06 20:16:15 +00:00

156 lines
5.2 KiB
Rust

use std::fs;
use std::sync::Arc;
use std::sync::mpsc;
use std::time::Duration;
use codex_protocol::ThreadId;
use tempfile::TempDir;
use super::COORDINATION_LOCK_FILE;
use super::WRITER_LOCK_DIR;
use super::WriterLockCoordinator;
use pretty_assertions::assert_eq;
use std::io::ErrorKind;
#[test]
fn writer_locks_reject_competing_owners_and_release_their_files() {
let home = TempDir::new().expect("temp dir");
let primary = Arc::new(WriterLockCoordinator::new(home.path()));
let secondary = Arc::new(WriterLockCoordinator::new(home.path()));
let thread_id = ThreadId::default();
let other_thread_id = ThreadId::default();
let owner = primary.acquire(thread_id).expect("acquire writer lock");
let lock_path = home
.path()
.join(WRITER_LOCK_DIR)
.join(format!("{thread_id}.lock"));
assert!(lock_path.exists());
// Another process is opening a writer. Distinct threads can start together,
// but even during this overlap the same thread must still have one owner.
let opening = fs::OpenOptions::new()
.read(true)
.write(true)
.open(
home.path()
.join(WRITER_LOCK_DIR)
.join(COORDINATION_LOCK_FILE),
)
.expect("open coordination");
opening.lock_shared().expect("another writer is opening");
let err = match secondary.acquire(thread_id) {
Ok(_) => panic!("competing owner should fail"),
Err(err) => err,
};
assert_eq!(err.kind(), ErrorKind::WouldBlock);
let other_owner = secondary
.acquire(other_thread_id)
.expect("other thread should acquire its own lock");
let next_owner = std::thread::scope(|scope| {
let _opening = opening;
let (closed, wait_for_close) = mpsc::channel();
scope.spawn(move || {
drop(owner);
let _ = closed.send(());
});
wait_for_close
.recv_timeout(Duration::from_secs(/*secs*/ 10))
.expect("writer should close while another writer is opening");
assert!(lock_path.exists());
secondary
.acquire(thread_id)
.expect("released thread should accept another owner")
});
drop(next_owner);
drop(other_owner);
let entries = fs::read_dir(home.path().join(WRITER_LOCK_DIR))
.expect("read lock directory")
.map(|entry| entry.expect("lock directory entry").file_name())
.collect::<Vec<_>>();
assert_eq!(entries, vec![COORDINATION_LOCK_FILE]);
}
#[test]
fn first_acquisition_removes_stale_locks_without_removing_active_locks() {
let home = TempDir::new().expect("temp dir");
let primary = Arc::new(WriterLockCoordinator::new(home.path()));
let active_thread_id = ThreadId::default();
let active_owner = primary
.acquire(active_thread_id)
.expect("acquire active writer lock");
let stale_thread_id = ThreadId::default();
let stale_path = home
.path()
.join(WRITER_LOCK_DIR)
.join(format!("{stale_thread_id}.lock"));
fs::File::create(&stale_path).expect("create stale writer lock");
let secondary = Arc::new(WriterLockCoordinator::new(home.path()));
let secondary_owner = secondary
.acquire(ThreadId::default())
.expect("acquire writer lock after cleanup");
assert!(!stale_path.exists());
let err = match secondary.acquire(active_thread_id) {
Ok(_) => panic!("active writer should survive cleanup"),
Err(err) => err,
};
assert_eq!(err.kind(), ErrorKind::WouldBlock);
drop(secondary_owner);
drop(active_owner);
}
#[test]
fn publication_skips_live_writers_and_keeps_coordination_locked() {
let home = TempDir::new().expect("temp dir");
let publisher = Arc::new(WriterLockCoordinator::new(home.path()));
let writer = Arc::new(WriterLockCoordinator::new(home.path()));
let thread_id = ThreadId::default();
let owner = writer.acquire(thread_id).expect("writer owns thread");
assert!(
publisher
.try_acquire_for_publication(thread_id)
.unwrap()
.is_none()
);
drop(owner);
let publication = publisher
.try_acquire_for_publication(thread_id)
.unwrap()
.unwrap();
let coordination = fs::OpenOptions::new()
.read(true)
.write(true)
.open(writer.directory.join(COORDINATION_LOCK_FILE))
.unwrap();
assert!(matches!(
coordination.try_lock(),
Err(fs::TryLockError::WouldBlock)
));
let error = writer
.acquire(ThreadId::default())
.err()
.expect("coordination contention must return an error instead of hanging");
assert_eq!(error.kind(), ErrorKind::TimedOut);
drop(publication);
coordination
.try_lock()
.expect("publication releases coordination");
drop(coordination);
// An existing but unlocked file is also idle; file existence is not ownership.
fs::File::create(writer.directory.join(format!("{thread_id}.lock"))).unwrap();
drop(
publisher
.try_acquire_for_publication(thread_id)
.unwrap()
.expect("stale lock is idle"),
);
assert!(writer.acquire(thread_id).is_ok());
}