diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 3ec2708880..da19f30103 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -1247,6 +1247,7 @@ version = "0.0.0" dependencies = [ "serde_json", "thiserror 2.0.16", + "tokio", ] [[package]] diff --git a/codex-rs/internal-storage/Cargo.toml b/codex-rs/internal-storage/Cargo.toml index cd419326c8..ccc30337b9 100644 --- a/codex-rs/internal-storage/Cargo.toml +++ b/codex-rs/internal-storage/Cargo.toml @@ -6,6 +6,7 @@ edition.workspace = true [dependencies] serde_json = { workspace = true } thiserror = { workspace = true } +tokio = { workspace = true, features = ["rt", "sync"] } [lints] workspace = true diff --git a/codex-rs/internal-storage/src/lib.rs b/codex-rs/internal-storage/src/lib.rs index 6a2efbaca5..5504626368 100644 --- a/codex-rs/internal-storage/src/lib.rs +++ b/codex-rs/internal-storage/src/lib.rs @@ -4,9 +4,11 @@ use std::fs; use std::io; use std::path::Path; use std::path::PathBuf; -use std::sync::Mutex; use std::sync::OnceLock; use thiserror::Error; +use tokio::runtime::Builder as RuntimeBuilder; +use tokio::runtime::Handle as RuntimeHandle; +use tokio::sync::Mutex; const INTERNAL_STORAGE_FILENAME: &str = "internal_storage.json"; @@ -58,14 +60,14 @@ impl Storage { Ok(()) } - fn read(&self, key: &str) -> Result, InternalStorageError> { - let _guard = self.lock.lock().expect("internal storage lock poisoned"); + async fn read_async(&self, key: &str) -> Result, InternalStorageError> { + let _guard = self.lock.lock().await; let map = self.read_map()?; Ok(map.get(key).map(value_to_string)) } - fn write(&self, key: &str, value: &str) -> Result<(), InternalStorageError> { - let _guard = self.lock.lock().expect("internal storage lock poisoned"); + async fn write_async(&self, key: &str, value: &str) -> Result<(), InternalStorageError> { + let _guard = self.lock.lock().await; let mut map = self.read_map()?; map.insert(key.to_string(), Value::String(value.to_string())); self.write_map(&map) @@ -87,11 +89,19 @@ pub fn initialize(codex_home: PathBuf) { } pub fn read(key: &str) -> Result, InternalStorageError> { - storage()?.read(key) + let fut = storage()?.read_async(key); + match block_on(fut) { + Ok(res) => res, + Err(err) => Err(err.into()), + } } pub fn write(key: &str, value: &str) -> Result<(), InternalStorageError> { - storage()?.write(key, value) + let fut = storage()?.write_async(key, value); + match block_on(fut) { + Ok(res) => res, + Err(err) => Err(err.into()), + } } fn storage() -> Result<&'static Storage, InternalStorageError> { @@ -111,3 +121,16 @@ fn value_to_string(value: &Value) -> String { _ => value.to_string(), } } + +fn block_on(fut: F) -> Result +where + F: std::future::Future, +{ + match RuntimeHandle::try_current() { + Ok(handle) => Ok(handle.block_on(fut)), + Err(_) => { + let rt = RuntimeBuilder::new_current_thread().enable_all().build()?; + Ok(rt.block_on(fut)) + } + } +}