Skip to content

Commit 6e63b1a

Browse files
committed
fix(driver-docker): scope pending sandbox matching by id and workspace
Signed-off-by: Artem Lytvyn <[email protected]>
1 parent 118b250 commit 6e63b1a

2 files changed

Lines changed: 439 additions & 37 deletions

File tree

‎crates/openshell-driver-docker/src/lib.rs‎

Lines changed: 98 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ impl DockerComputeDriver {
794794
return Ok(Some(sandbox));
795795
}
796796

797-
Ok(self.pending_snapshot(sandbox_id, sandbox_name).await)
797+
self.pending_snapshot(sandbox_id, sandbox_name).await
798798
}
799799

800800
async fn current_snapshots(&self) -> Result<Vec<DriverSandbox>, Status> {
@@ -1067,7 +1067,9 @@ impl DockerComputeDriver {
10671067
sandbox_id: &str,
10681068
sandbox_name: &str,
10691069
) -> Result<bool, Status> {
1070-
let pending = self.remove_pending_sandbox(sandbox_id, sandbox_name).await;
1070+
let pending = self
1071+
.remove_pending_sandbox(sandbox_id, sandbox_name)
1072+
.await?;
10711073
if let Some(record) = pending.as_ref()
10721074
&& let Some(task) = record.task.as_ref()
10731075
{
@@ -1132,7 +1134,10 @@ impl DockerComputeDriver {
11321134
.find_managed_container_summary(sandbox_id, sandbox_name)
11331135
.await?
11341136
else {
1135-
if let Some(record) = self.remove_pending_sandbox(sandbox_id, sandbox_name).await {
1137+
if let Some(record) = self
1138+
.remove_pending_sandbox(sandbox_id, sandbox_name)
1139+
.await?
1140+
{
11361141
if let Some(task) = record.task {
11371142
task.abort();
11381143
}
@@ -1249,10 +1254,11 @@ impl DockerComputeDriver {
12491254

12501255
async fn reserve_pending_sandbox(&self, sandbox: &DriverSandbox) -> Result<(), Status> {
12511256
let mut pending = self.pending.lock().await;
1252-
if pending
1253-
.values()
1254-
.any(|record| record.sandbox.id == sandbox.id || record.sandbox.name == sandbox.name)
1255-
{
1257+
if pending.values().any(|record| {
1258+
record.sandbox.id == sandbox.id
1259+
|| (record.sandbox.name == sandbox.name
1260+
&& record.sandbox.workspace == sandbox.workspace)
1261+
}) {
12561262
return Err(Status::already_exists("sandbox already exists"));
12571263
}
12581264

@@ -1275,12 +1281,12 @@ impl DockerComputeDriver {
12751281
&self,
12761282
sandbox_id: &str,
12771283
sandbox_name: &str,
1278-
) -> Option<DriverSandbox> {
1284+
) -> Result<Option<DriverSandbox>, Status> {
12791285
let pending = self.pending.lock().await;
1280-
pending
1281-
.values()
1282-
.find(|record| pending_sandbox_matches(&record.sandbox, sandbox_id, sandbox_name))
1283-
.map(|record| record.sandbox.clone())
1286+
let Some(id) = resolve_pending_id(&pending, sandbox_id, sandbox_name)? else {
1287+
return Ok(None);
1288+
};
1289+
Ok(pending.get(&id).map(|record| record.sandbox.clone()))
12841290
}
12851291

12861292
async fn pending_snapshot_map(&self) -> HashMap<String, DriverSandbox> {
@@ -1300,12 +1306,12 @@ impl DockerComputeDriver {
13001306
&self,
13011307
sandbox_id: &str,
13021308
sandbox_name: &str,
1303-
) -> Option<PendingSandboxRecord> {
1309+
) -> Result<Option<PendingSandboxRecord>, Status> {
13041310
let mut pending = self.pending.lock().await;
1305-
let id = pending.iter().find_map(|(id, record)| {
1306-
pending_sandbox_matches(&record.sandbox, sandbox_id, sandbox_name).then(|| id.clone())
1307-
})?;
1308-
pending.remove(&id)
1311+
let Some(id) = resolve_pending_id(&pending, sandbox_id, sandbox_name)? else {
1312+
return Ok(None);
1313+
};
1314+
Ok(pending.remove(&id))
13091315
}
13101316

13111317
async fn fail_pending_sandbox(
@@ -1560,21 +1566,14 @@ impl DockerComputeDriver {
15601566
.map_err(|err| internal_status("find Docker sandbox container", err))?;
15611567

15621568
Ok(containers.into_iter().find(|summary| {
1563-
let Some(labels) = summary.labels.as_ref() else {
1564-
return false;
1565-
};
1566-
let namespace_matches = labels
1567-
.get(LABEL_SANDBOX_NAMESPACE)
1568-
.is_some_and(|value| value == &self.config.sandbox_namespace);
1569-
let id_matches = sandbox_id.is_empty()
1570-
|| labels
1571-
.get(LABEL_SANDBOX_ID)
1572-
.is_some_and(|value| value == sandbox_id);
1573-
let name_matches = sandbox_name.is_empty()
1574-
|| labels
1575-
.get(LABEL_SANDBOX_NAME)
1576-
.is_some_and(|value| value == sandbox_name);
1577-
namespace_matches && id_matches && name_matches
1569+
summary.labels.as_ref().is_some_and(|labels| {
1570+
managed_container_identity_matches(
1571+
labels,
1572+
&self.config.sandbox_namespace,
1573+
sandbox_id,
1574+
sandbox_name,
1575+
)
1576+
})
15781577
}))
15791578
}
15801579

@@ -2162,9 +2161,73 @@ fn pending_sandbox_snapshot(
21622161
}
21632162
}
21642163

2165-
fn pending_sandbox_matches(sandbox: &DriverSandbox, sandbox_id: &str, sandbox_name: &str) -> bool {
2166-
(!sandbox_id.is_empty() && sandbox.id == sandbox_id)
2167-
|| (!sandbox_name.is_empty() && sandbox.name == sandbox_name)
2164+
/// Decides whether a managed container satisfies a lifecycle request.
2165+
///
2166+
/// `sandbox_id` is authoritative, matching [`resolve_pending_id`]. Requiring
2167+
/// the name to agree as well would discard a correct id match whenever the
2168+
/// caller pairs it with a stale name, leaving the container and its token file
2169+
/// behind while the driver reports the sandbox as absent.
2170+
///
2171+
/// A request with no identifier matches nothing. `require_sandbox_identifier`
2172+
/// rejects that upstream, but the label filters degenerate to "every managed
2173+
/// container in the namespace", so this does not rely on the caller to guard it.
2174+
fn managed_container_identity_matches(
2175+
labels: &HashMap<String, String>,
2176+
namespace: &str,
2177+
sandbox_id: &str,
2178+
sandbox_name: &str,
2179+
) -> bool {
2180+
if labels
2181+
.get(LABEL_SANDBOX_NAMESPACE)
2182+
.is_none_or(|value| value != namespace)
2183+
{
2184+
return false;
2185+
}
2186+
if !sandbox_id.is_empty() {
2187+
return labels
2188+
.get(LABEL_SANDBOX_ID)
2189+
.is_some_and(|value| value == sandbox_id);
2190+
}
2191+
!sandbox_name.is_empty()
2192+
&& labels
2193+
.get(LABEL_SANDBOX_NAME)
2194+
.is_some_and(|value| value == sandbox_name)
2195+
}
2196+
2197+
/// Resolves a lifecycle request to at most one pending sandbox id.
2198+
///
2199+
/// `sandbox_id` is authoritative: when the caller supplies one, the name is
2200+
/// never consulted as an alternative. The name fallback rejects ambiguity
2201+
/// instead of letting `HashMap` iteration order pick a match, because sandbox
2202+
/// names are unique per workspace and the driver request carries no workspace.
2203+
fn resolve_pending_id(
2204+
pending: &HashMap<String, PendingSandboxRecord>,
2205+
sandbox_id: &str,
2206+
sandbox_name: &str,
2207+
) -> Result<Option<String>, Status> {
2208+
if !sandbox_id.is_empty() {
2209+
return Ok(pending
2210+
.contains_key(sandbox_id)
2211+
.then(|| sandbox_id.to_string()));
2212+
}
2213+
if sandbox_name.is_empty() {
2214+
return Ok(None);
2215+
}
2216+
2217+
let mut matches = pending
2218+
.iter()
2219+
.filter(|(_, record)| record.sandbox.name == sandbox_name)
2220+
.map(|(id, _)| id.clone());
2221+
2222+
let Some(id) = matches.next() else {
2223+
return Ok(None);
2224+
};
2225+
if matches.next().is_some() {
2226+
return Err(Status::failed_precondition(
2227+
"sandbox_name matches multiple pending sandboxes; specify sandbox_id",
2228+
));
2229+
}
2230+
Ok(Some(id))
21682231
}
21692232

21702233
fn provisioning_condition() -> DriverCondition {

0 commit comments

Comments
 (0)