mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
refactor: narrow async lock guard lifetimes
Follow-up to https://github.com/openai/codex/pull/18178. This handles the straightforward await-holding cleanup first, without enabling the Clippy lint yet; the long-term goal is to enable the lint in a follow-up once the remaining cases are resolved or explicitly justified.
This commit is contained in:
@@ -162,12 +162,11 @@ async fn spawn_process_with_stdin_mode(
|
||||
let (stdout_tx, stdout_rx) = mpsc::channel::<Vec<u8>>(128);
|
||||
let (stderr_tx, stderr_rx) = mpsc::channel::<Vec<u8>>(128);
|
||||
let writer_handle = if let Some(stdin) = stdin {
|
||||
let writer = Arc::new(tokio::sync::Mutex::new(stdin));
|
||||
tokio::spawn(async move {
|
||||
let mut writer = stdin;
|
||||
while let Some(bytes) = writer_rx.recv().await {
|
||||
let mut guard = writer.lock().await;
|
||||
let _ = guard.write_all(&bytes).await;
|
||||
let _ = guard.flush().await;
|
||||
let _ = writer.write_all(&bytes).await;
|
||||
let _ = writer.flush().await;
|
||||
}
|
||||
})
|
||||
} else {
|
||||
|
||||
@@ -277,17 +277,36 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_returns_error_when_lock_is_held() {
|
||||
let flag = ReadinessFlag::new();
|
||||
let _guard = flag
|
||||
.tokens
|
||||
.try_lock()
|
||||
.expect("initial lock acquisition should succeed");
|
||||
let flag = Arc::new(ReadinessFlag::new());
|
||||
let (locked_tx, locked_rx) = std::sync::mpsc::channel();
|
||||
let (release_tx, release_rx) = std::sync::mpsc::channel();
|
||||
let lock_thread = {
|
||||
let flag = Arc::clone(&flag);
|
||||
std::thread::spawn(move || {
|
||||
let _guard = flag.tokens.blocking_lock();
|
||||
locked_tx
|
||||
.send(())
|
||||
.expect("test should receive lock acquisition notification");
|
||||
release_rx
|
||||
.recv()
|
||||
.expect("test should release held readiness lock");
|
||||
})
|
||||
};
|
||||
locked_rx
|
||||
.recv()
|
||||
.expect("test should observe held readiness lock");
|
||||
|
||||
let err = flag
|
||||
.subscribe()
|
||||
.await
|
||||
.expect_err("contended subscribe should report a lock failure");
|
||||
assert_matches!(err, ReadinessError::TokenLockFailed);
|
||||
release_tx
|
||||
.send(())
|
||||
.expect("test should release readiness lock thread");
|
||||
lock_thread
|
||||
.join()
|
||||
.expect("readiness lock thread should not panic");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user