Move session index I/O off async runtime threads (#49708)

## Why

Session index updates perform synchronous file I/O and wait on a blocking mutex inside async functions, blocking runtime threads.

## What changed

Run appends and thread-name removals in `spawn_blocking` workers. Acquire a Tokio mutex before dispatch and keep its owned guard in the worker so queued and running updates remain serialized even if the awaiting task is cancelled. Move the lookup's file-existence check into its blocking worker as well.

## Testing

Add tests for lock retention after cancellation of running and queued updates, write ordering after cancellation, and removal that preserves other thread names and malformed lines.

GitOrigin-RevId: 368745d8e5bbf24c527facc0a62ddd0fb76f0ffb
This commit is contained in:
Charlie Marsh
2026-09-30 18:51:29 +00:00
committed by copyberry
parent 17e5a4dbcb
commit 4f699cd642
2 changed files with 165 additions and 44 deletions
+61 -44
View File
@@ -5,8 +5,8 @@ use std::io::ErrorKind;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::LazyLock;
use std::sync::Mutex;
use std::time::SystemTime;
use crate::reverse_jsonl_scanner::ReverseJsonlScanner;
@@ -18,7 +18,8 @@ use serde::Deserialize;
use serde::Serialize;
const SESSION_INDEX_FILE: &str = "session_index.jsonl";
static SESSION_INDEX_LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
static SESSION_INDEX_LOCK: LazyLock<Arc<tokio::sync::Mutex<()>>> =
LazyLock::new(|| Arc::new(tokio::sync::Mutex::new(())));
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SessionIndexEntry {
@@ -45,28 +46,28 @@ pub async fn append_thread_name(
thread_name: name.to_string(),
updated_at,
};
append_session_index_entry(codex_home, &entry).await
append_session_index_entry(codex_home, entry).await
}
/// Append a raw session index entry to `session_index.jsonl`.
/// Consumers scan from the end to find the newest match.
pub async fn append_session_index_entry(
codex_home: &Path,
entry: &SessionIndexEntry,
entry: SessionIndexEntry,
) -> std::io::Result<()> {
let _guard = SESSION_INDEX_LOCK
.lock()
.map_err(|err| std::io::Error::other(err.to_string()))?;
let path = session_index_path(codex_home);
let mut file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&path)?;
let mut line = serde_json::to_string(entry).map_err(std::io::Error::other)?;
line.push('\n');
file.write_all(line.as_bytes())?;
file.flush()?;
Ok(())
with_session_index_lock(&SESSION_INDEX_LOCK, move || {
let mut file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&path)?;
let mut line = serde_json::to_string(&entry).map_err(std::io::Error::other)?;
line.push('\n');
file.write_all(line.as_bytes())?;
file.flush()?;
Ok(())
})
.await
}
/// Remove all recorded names for a thread from the session index.
@@ -74,33 +75,47 @@ pub async fn remove_thread_name_entries(
codex_home: &Path,
thread_id: ThreadId,
) -> std::io::Result<()> {
let _guard = SESSION_INDEX_LOCK
.lock()
.map_err(|err| std::io::Error::other(err.to_string()))?;
let path = session_index_path(codex_home);
let contents = match std::fs::read_to_string(&path) {
Ok(contents) => contents,
Err(err) if err.kind() == ErrorKind::NotFound => return Ok(()),
Err(err) => return Err(err),
};
let mut removed = false;
let mut remaining = String::with_capacity(contents.len());
for line in contents.lines() {
let should_remove = serde_json::from_str::<SessionIndexEntry>(line.trim())
.is_ok_and(|entry| entry.id == thread_id);
if should_remove {
removed = true;
} else {
remaining.push_str(line);
remaining.push('\n');
with_session_index_lock(&SESSION_INDEX_LOCK, move || {
let contents = match std::fs::read_to_string(&path) {
Ok(contents) => contents,
Err(err) if err.kind() == ErrorKind::NotFound => return Ok(()),
Err(err) => return Err(err),
};
let mut removed = false;
let mut remaining = String::with_capacity(contents.len());
for line in contents.lines() {
let should_remove = serde_json::from_str::<SessionIndexEntry>(line.trim())
.is_ok_and(|entry| entry.id == thread_id);
if should_remove {
removed = true;
} else {
remaining.push_str(line);
remaining.push('\n');
}
}
}
if !removed {
return Ok(());
}
let temp_path = path.with_extension("jsonl.tmp");
std::fs::write(&temp_path, remaining)?;
std::fs::rename(temp_path, path)
if !removed {
return Ok(());
}
let temp_path = path.with_extension("jsonl.tmp");
std::fs::write(&temp_path, remaining)?;
std::fs::rename(temp_path, path)
})
.await
}
/// Acquire the ordered index lock before dispatch and retain it through cancelled updates.
async fn with_session_index_lock(
lock: &Arc<tokio::sync::Mutex<()>>,
update: impl FnOnce() -> std::io::Result<()> + Send + 'static,
) -> std::io::Result<()> {
let guard = Arc::clone(lock).lock_owned().await;
tokio::task::spawn_blocking(move || {
let _guard = guard;
update()
})
.await
.map_err(std::io::Error::other)?
}
/// Find the latest thread name for a thread id, if any.
@@ -124,13 +139,15 @@ pub async fn find_thread_names_by_ids(
codex_home: &Path,
thread_ids: &HashSet<ThreadId>,
) -> std::io::Result<HashMap<ThreadId, String>> {
let path = session_index_path(codex_home);
if thread_ids.is_empty() || !path.exists() {
if thread_ids.is_empty() {
return Ok(HashMap::new());
}
let path = session_index_path(codex_home);
let mut remaining_ids = thread_ids.clone();
tokio::task::spawn_blocking(move || {
if !path.exists() {
return Ok(HashMap::new());
}
let mut scanner = ReverseJsonlScanner::new(File::open(path)?)?;
let mut names = HashMap::with_capacity(remaining_ids.len());
while let Some(outcome) = scanner.scan_next::<SessionIndexEntry>()? {
+104
View File
@@ -447,6 +447,110 @@ async fn find_thread_names_by_ids_skips_unusable_names_and_reads_across_chunks()
Ok(())
}
#[tokio::test]
async fn removal_preserves_other_names_and_malformed_lines() -> std::io::Result<()> {
let temp = TempDir::new()?;
let removed_id = ThreadId::new();
let kept_id = ThreadId::new();
append_thread_name(temp.path(), removed_id, "old").await?;
append_thread_name(temp.path(), removed_id, "new").await?;
append_thread_name(temp.path(), kept_id, "kept").await?;
let path = session_index_path(temp.path());
std::fs::OpenOptions::new()
.append(true)
.open(&path)?
.write_all(b"malformed\n")?;
remove_thread_name_entries(temp.path(), removed_id).await?;
assert_eq!(
find_thread_names_by_ids(temp.path(), &HashSet::from([removed_id, kept_id])).await?,
HashMap::from([(kept_id, "kept".to_string())]),
);
assert!(
std::fs::read_to_string(path)?
.lines()
.any(|line| line == "malformed")
);
Ok(())
}
#[tokio::test]
async fn cancelled_index_update_holds_lock_until_worker_finishes() -> std::io::Result<()> {
let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
let (resume_tx, resume_rx) = std::sync::mpsc::channel();
let update = tokio::spawn(with_session_index_lock(&SESSION_INDEX_LOCK, move || {
let _ = reached_tx.send(());
// Dropping the sender on a test failure also releases the blocking worker.
let _ = resume_rx.recv();
Ok(())
}));
reached_rx.await.unwrap();
update.abort();
assert!(update.await.unwrap_err().is_cancelled());
let lock_is_held = SESSION_INDEX_LOCK.try_lock().is_err();
resume_tx.send(()).unwrap();
with_session_index_lock(&SESSION_INDEX_LOCK, || Ok(())).await?;
assert!(
lock_is_held,
"cancellation released the running worker's lock"
);
Ok(())
}
#[test]
fn cancelled_queued_index_update_preserves_the_next_write() -> std::io::Result<()> {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.max_blocking_threads(1)
.build()?;
runtime.block_on(async {
let home = TempDir::new()?;
let path = home.path().join("thread-name");
// A private lock ensures shared-process test runners cannot supply another owner's guard.
let lock = Arc::new(tokio::sync::Mutex::new(()));
let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
let (resume_tx, resume_rx) = std::sync::mpsc::channel::<()>();
let blocker = tokio::task::spawn_blocking(move || {
let _ = reached_tx.send(());
// Dropping the sender on a failure also lets runtime teardown finish.
let _ = resume_rx.recv();
});
reached_rx.await.unwrap();
let queued_path = path.clone();
let mut update = Box::pin(with_session_index_lock(&lock, move || {
std::fs::write(queued_path, "obsolete")
}));
let state = std::future::poll_fn(|cx| {
std::task::Poll::Ready(std::future::Future::poll(update.as_mut(), cx))
})
.await;
assert!(
state.is_pending(),
"the blocking pool should hold the update queued"
);
assert!(
lock.try_lock().is_err(),
"this update must own the lock before cancellation"
);
drop(update);
let queued_update_holds_lock = lock.try_lock().is_err();
drop(resume_tx);
blocker.await.unwrap();
let next_path = path.clone();
with_session_index_lock(&lock, move || std::fs::write(next_path, "latest")).await?;
assert_eq!(
(queued_update_holds_lock, std::fs::read_to_string(path)?),
(true, "latest".to_string())
);
Ok(())
})
}
#[test]
fn scan_index_finds_latest_match_among_mixed_entries() -> std::io::Result<()> {
let temp = TempDir::new()?;