mirror of
https://github.com/openai/codex.git
synced 2026-12-05 01:00:54 +00:00
Use a synchronous mutex for remote metadata request tracking (#54475)
## What changed Replace the Tokio mutex protecting `RemoteFileSystem`'s in-flight metadata request map with `std::sync::Mutex`. Keep metadata I/O outside the lock and centralize locking in a helper that recovers poisoned guards. Update the existing concurrent metadata request test to use the locking helper. GitOrigin-RevId: 78aaecfbd8e8af9c1e5ea273758a414b0ad679a5
This commit is contained in:
committed by
copyberry
parent
6b991972c5
commit
e790ce2717
@@ -1,11 +1,12 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::MutexGuard;
|
||||
|
||||
use base64::Engine as _;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use tokio::io;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::OnceCell;
|
||||
use tracing::trace;
|
||||
|
||||
@@ -46,6 +47,7 @@ type InFlightMetadataRequest = OnceCell<Result<FileMetadata, Arc<io::Error>>>;
|
||||
|
||||
pub(crate) struct RemoteFileSystem {
|
||||
client: LazyRemoteExecServerClient,
|
||||
// Only the in-flight request map is locked; metadata I/O happens outside this lock.
|
||||
metadata_requests: Mutex<HashMap<PathUri, Arc<InFlightMetadataRequest>>>,
|
||||
}
|
||||
|
||||
@@ -58,6 +60,14 @@ impl RemoteFileSystem {
|
||||
}
|
||||
}
|
||||
|
||||
fn lock_metadata_requests(
|
||||
&self,
|
||||
) -> MutexGuard<'_, HashMap<PathUri, Arc<InFlightMetadataRequest>>> {
|
||||
self.metadata_requests
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
async fn canonicalize(
|
||||
&self,
|
||||
path: &PathUri,
|
||||
@@ -126,7 +136,7 @@ impl RemoteFileSystem {
|
||||
sandbox: sandbox.cloned(),
|
||||
})
|
||||
.await;
|
||||
self.metadata_requests.lock().await.clear();
|
||||
self.lock_metadata_requests().clear();
|
||||
result.map_err(map_remote_error)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -147,7 +157,7 @@ impl RemoteFileSystem {
|
||||
sandbox: sandbox.cloned(),
|
||||
})
|
||||
.await;
|
||||
self.metadata_requests.lock().await.clear();
|
||||
self.lock_metadata_requests().clear();
|
||||
result.map_err(map_remote_error)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -164,7 +174,7 @@ impl RemoteFileSystem {
|
||||
}
|
||||
|
||||
let request = {
|
||||
let mut requests = self.metadata_requests.lock().await;
|
||||
let mut requests = self.lock_metadata_requests();
|
||||
requests.retain(|_, in_flight| Arc::strong_count(in_flight) > 1);
|
||||
Arc::clone(requests.entry(path.clone()).or_default())
|
||||
};
|
||||
@@ -180,7 +190,7 @@ impl RemoteFileSystem {
|
||||
Err(error) => Err(io::Error::new(error.kind(), error.to_string())),
|
||||
};
|
||||
|
||||
let mut requests = self.metadata_requests.lock().await;
|
||||
let mut requests = self.lock_metadata_requests();
|
||||
if requests
|
||||
.get(path)
|
||||
.is_some_and(|in_flight| Arc::ptr_eq(in_flight, &request))
|
||||
@@ -278,7 +288,7 @@ impl RemoteFileSystem {
|
||||
sandbox: sandbox.cloned(),
|
||||
})
|
||||
.await;
|
||||
self.metadata_requests.lock().await.clear();
|
||||
self.lock_metadata_requests().clear();
|
||||
result.map_err(map_remote_error)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -300,7 +310,7 @@ impl RemoteFileSystem {
|
||||
sandbox: sandbox.cloned(),
|
||||
})
|
||||
.await;
|
||||
self.metadata_requests.lock().await.clear();
|
||||
self.lock_metadata_requests().clear();
|
||||
result.map_err(map_remote_error)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -402,7 +402,7 @@ async fn concurrent_remote_metadata_requests_share_only_in_flight_results() {
|
||||
};
|
||||
assert_eq!(first.expect("first metadata request"), expected);
|
||||
assert_eq!(second.expect("second metadata request"), expected);
|
||||
assert!(file_system.metadata_requests.lock().await.is_empty());
|
||||
assert!(file_system.lock_metadata_requests().is_empty());
|
||||
|
||||
let initializing_file_system = std::sync::Arc::clone(&file_system);
|
||||
let initializing_path = path.clone();
|
||||
@@ -424,9 +424,7 @@ async fn concurrent_remote_metadata_requests_share_only_in_flight_results() {
|
||||
assert!(futures::poll!(follower.as_mut()).is_pending());
|
||||
assert_eq!(
|
||||
file_system
|
||||
.metadata_requests
|
||||
.lock()
|
||||
.await
|
||||
.lock_metadata_requests()
|
||||
.get(&path)
|
||||
.map(std::sync::Arc::strong_count),
|
||||
Some(3)
|
||||
@@ -448,7 +446,7 @@ async fn concurrent_remote_metadata_requests_share_only_in_flight_results() {
|
||||
.expect("waiting metadata request should retry the abandoned initializer"),
|
||||
refreshed
|
||||
);
|
||||
assert!(file_system.metadata_requests.lock().await.is_empty());
|
||||
assert!(file_system.lock_metadata_requests().is_empty());
|
||||
assert_eq!(
|
||||
captured_params.await.expect("captured metadata requests"),
|
||||
vec![
|
||||
|
||||
Reference in New Issue
Block a user