Skip to content

Commit 3e0fe13

Browse files
committed
fix(providers): serialize profile import invariants
Signed-off-by: John Myers <[email protected]>
1 parent 8ee094b commit 3e0fe13

4 files changed

Lines changed: 95 additions & 8 deletions

File tree

‎architecture/gateway.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,12 @@ responses copy the stored version onto the profile payload so exported YAML can
253253
carry the expected version for safe single-profile updates. Database migrations
254254
backfill existing rows with version 1.
255255

256-
Provider profile updates also hold the sandbox synchronization guard while
257-
checking attached-sandbox dynamic token grant ambiguity and writing the updated
258-
profile. Sandbox provider attach/detach uses the same guard, so one gateway
259-
process cannot interleave an attach with a profile update that would leave an
260-
ambiguous final dynamic-token state.
256+
Provider profile imports and updates hold the sandbox synchronization guard while
257+
checking attached-sandbox dynamic token grant ambiguity and writing the profile.
258+
Sandbox creation with initial providers and sandbox provider attach/detach use
259+
the same guard, so one gateway process cannot interleave a profile mutation with
260+
a sandbox provider-set mutation that would leave an ambiguous final dynamic-token
261+
state.
261262

262263
Policy and runtime settings are delivered together through the effective sandbox
263264
config path. A gateway-global policy can override sandbox-scoped policy. The

‎crates/openshell-server/src/compute/mod.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ impl ComputeRuntime {
282282
})
283283
}
284284

285-
/// Serializes sandbox object read-modify-write operations within this
286-
/// gateway process.
285+
/// Serializes sandbox/provider-profile invariant checks and object writes
286+
/// within this gateway process.
287287
///
288-
/// This is a temporary single-gateway guard for full-object sandbox writes.
288+
/// This is a temporary single-gateway guard for cross-object invariants.
289289
/// It is not HA-safe; replace it with DB-backed CAS/resource-version writes
290290
/// tracked by #1255 before enabling multiple gateway writers.
291291
pub(crate) async fn sandbox_sync_guard(&self) -> tokio::sync::OwnedMutexGuard<()> {

‎crates/openshell-server/src/grpc/provider.rs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,7 @@ pub(super) async fn handle_import_provider_profiles(
12991299
let request = request.into_inner();
13001300
let (profiles, mut diagnostics) = profiles_from_import_items(&request.profiles);
13011301
add_empty_profile_set_diagnostic(&profiles, &mut diagnostics);
1302+
let _sandbox_sync_guard = state.compute.sandbox_sync_guard().await;
13021303
diagnostics.extend(profile_conflict_diagnostics(state.store.as_ref(), &profiles).await?);
13031304
diagnostics.extend(validate_profile_set(&profiles));
13041305
if !has_errors(&diagnostics) {
@@ -2660,6 +2661,40 @@ mod tests {
26602661
}));
26612662
}
26622663

2664+
#[tokio::test]
2665+
async fn import_provider_profile_waits_for_sandbox_sync_guard() {
2666+
let state = test_server_state().await;
2667+
let guard = state.compute.sandbox_sync_guard().await;
2668+
let task_state = state.clone();
2669+
let task = tokio::spawn(async move {
2670+
handle_import_provider_profiles(
2671+
&task_state,
2672+
Request::new(ImportProviderProfilesRequest {
2673+
profiles: vec![ProviderProfileImportItem {
2674+
profile: Some(custom_profile("guarded-import")),
2675+
source: "guarded-import.yaml".to_string(),
2676+
}],
2677+
}),
2678+
)
2679+
.await
2680+
});
2681+
2682+
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
2683+
assert!(
2684+
!task.is_finished(),
2685+
"profile import should wait for sandbox sync guard"
2686+
);
2687+
drop(guard);
2688+
2689+
let response = tokio::time::timeout(std::time::Duration::from_secs(5), task)
2690+
.await
2691+
.expect("import should finish after guard release")
2692+
.expect("join import task")
2693+
.expect("import should succeed")
2694+
.into_inner();
2695+
assert!(response.imported);
2696+
}
2697+
26632698
#[tokio::test]
26642699
async fn update_provider_profile_replaces_custom_profile_and_preserves_metadata() {
26652700
let state = test_server_state().await;

‎crates/openshell-server/src/grpc/sandbox.rs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ async fn handle_create_sandbox_inner(
132132
crate::grpc::validation::validate_label_value(value)?;
133133
}
134134

135+
let _sandbox_sync_guard = if spec.providers.is_empty() {
136+
None
137+
} else {
138+
Some(state.compute.sandbox_sync_guard().await)
139+
};
140+
135141
// Validate provider names exist (fail fast).
136142
for name in &spec.providers {
137143
state
@@ -2588,6 +2594,51 @@ mod tests {
25882594
assert!(err.message().contains("provider-b"));
25892595
}
25902596

2597+
#[tokio::test]
2598+
async fn create_sandbox_with_providers_waits_for_sandbox_sync_guard() {
2599+
let state = test_server_state().await;
2600+
state
2601+
.store
2602+
.put_message(&test_provider("work-github", "github"))
2603+
.await
2604+
.unwrap();
2605+
2606+
let guard = state.compute.sandbox_sync_guard().await;
2607+
let task_state = state.clone();
2608+
let task = tokio::spawn(async move {
2609+
handle_create_sandbox(
2610+
&task_state,
2611+
Request::new(CreateSandboxRequest {
2612+
name: "guarded-create".to_string(),
2613+
spec: Some(openshell_core::proto::SandboxSpec {
2614+
providers: vec!["work-github".to_string()],
2615+
..Default::default()
2616+
}),
2617+
labels: HashMap::new(),
2618+
}),
2619+
)
2620+
.await
2621+
});
2622+
2623+
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
2624+
assert!(
2625+
!task.is_finished(),
2626+
"sandbox create with initial providers should wait for sandbox sync guard"
2627+
);
2628+
drop(guard);
2629+
2630+
let response = tokio::time::timeout(std::time::Duration::from_secs(5), task)
2631+
.await
2632+
.expect("create should finish after guard release")
2633+
.expect("join create task")
2634+
.expect("create should succeed")
2635+
.into_inner();
2636+
assert_eq!(
2637+
response.sandbox.unwrap().spec.unwrap().providers,
2638+
vec!["work-github".to_string()]
2639+
);
2640+
}
2641+
25912642
#[tokio::test]
25922643
async fn attach_sandbox_provider_rejects_credential_key_collisions() {
25932644
let state = test_server_state().await;

0 commit comments

Comments
 (0)