Skip to content

Commit 67d7528

Browse files
committed
fix(cli): wait for terminal sandbox deletion
Part of #2999 Wait for GetSandbox to report durable absence by default, provide an explicit asynchronous CLI opt-out, and add a Go SDK terminal wait helper. Signed-off-by: Drew Newberry <[email protected]>
1 parent 6e369f2 commit 67d7528

16 files changed

Lines changed: 617 additions & 13 deletions

File tree

‎crates/openshell-cli/src/main.rs‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use openshell_cli::color::{self, ColorChoice, Colorize};
1111
use std::collections::HashMap;
1212
use std::io::Write;
1313
use std::path::PathBuf;
14+
use std::time::Duration;
1415

1516
use openshell_bootstrap::{
1617
edge_token::load_edge_token, get_gateway_metadata, list_gateways, load_active_gateway,
@@ -1667,6 +1668,14 @@ enum SandboxCommands {
16671668
/// Delete all sandboxes.
16681669
#[arg(long, conflicts_with = "names")]
16691670
all: bool,
1671+
1672+
/// Return after the gateway accepts the deletion request.
1673+
#[arg(long, conflicts_with = "timeout")]
1674+
no_wait: bool,
1675+
1676+
/// Maximum seconds to wait for terminal deletion.
1677+
#[arg(long, value_name = "SECONDS")]
1678+
timeout: Option<u64>,
16701679
},
16711680

16721681
/// Stop a sandbox while preserving its workspace.
@@ -3560,14 +3569,23 @@ async fn run_async() -> Result<()> {
35603569
)
35613570
.await?;
35623571
}
3563-
SandboxCommands::Delete { names, all } => {
3572+
SandboxCommands::Delete {
3573+
names,
3574+
all,
3575+
no_wait,
3576+
timeout,
3577+
} => {
35643578
run::sandbox_delete(
35653579
endpoint,
35663580
&names,
35673581
all,
35683582
&cli.workspace,
35693583
&tls,
35703584
&ctx.name,
3585+
run::SandboxDeleteOptions {
3586+
no_wait,
3587+
timeout: timeout.map(Duration::from_secs),
3588+
},
35713589
)
35723590
.await?;
35733591
}
@@ -5509,6 +5527,59 @@ mod tests {
55095527
));
55105528
}
55115529

5530+
#[test]
5531+
fn sandbox_delete_waits_by_default_and_accepts_async_options() {
5532+
let default_delete = Cli::try_parse_from(["openshell", "sandbox", "delete", "demo"])
5533+
.expect("sandbox delete should parse");
5534+
assert!(matches!(
5535+
default_delete.command,
5536+
Some(Commands::Sandbox {
5537+
command: Some(SandboxCommands::Delete {
5538+
no_wait: false,
5539+
timeout: None,
5540+
..
5541+
})
5542+
})
5543+
));
5544+
5545+
let no_wait = Cli::try_parse_from(["openshell", "sandbox", "delete", "demo", "--no-wait"])
5546+
.expect("sandbox delete --no-wait should parse");
5547+
assert!(matches!(
5548+
no_wait.command,
5549+
Some(Commands::Sandbox {
5550+
command: Some(SandboxCommands::Delete { no_wait: true, .. })
5551+
})
5552+
));
5553+
5554+
let timeout =
5555+
Cli::try_parse_from(["openshell", "sandbox", "delete", "demo", "--timeout", "42"])
5556+
.expect("sandbox delete --timeout should parse");
5557+
assert!(matches!(
5558+
timeout.command,
5559+
Some(Commands::Sandbox {
5560+
command: Some(SandboxCommands::Delete {
5561+
no_wait: false,
5562+
timeout: Some(42),
5563+
..
5564+
})
5565+
})
5566+
));
5567+
5568+
assert!(
5569+
Cli::try_parse_from([
5570+
"openshell",
5571+
"sandbox",
5572+
"delete",
5573+
"demo",
5574+
"--no-wait",
5575+
"--timeout",
5576+
"42",
5577+
])
5578+
.is_err(),
5579+
"--no-wait and --timeout should conflict"
5580+
);
5581+
}
5582+
55125583
#[test]
55135584
fn sandbox_list_accepts_output_json() {
55145585
let cli = Cli::try_parse_from(["openshell", "sandbox", "list", "-o", "json"])

‎crates/openshell-cli/src/run.rs‎

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ async fn finalize_sandbox_create_session(
410410
}
411411

412412
let names = [sandbox_name.to_string()];
413-
if let Err(err) = sandbox_delete(server, &names, false, workspace, tls, gateway).await {
413+
if let Err(err) = sandbox_delete(
414+
server, &names, false, workspace, tls, gateway, SandboxDeleteOptions::default(),
415+
).await {
414416
if let Ok(exit_code) = session_result.as_ref() {
415417
return Err(miette::miette!(
416418
"sandbox command exited with status {exit_code}, but ephemeral cleanup failed: {err}"
@@ -3714,6 +3716,15 @@ fn labels_display(labels: &HashMap<String, String>) -> String {
37143716
pairs.join(", ")
37153717
}
37163718

3719+
/// Controls whether sandbox deletion waits for durable absence.
3720+
#[derive(Debug, Clone, Copy, Default)]
3721+
pub struct SandboxDeleteOptions {
3722+
/// Return after the gateway acknowledges an asynchronous delete.
3723+
pub no_wait: bool,
3724+
/// Override the lifecycle timeout. The environment/default applies when unset.
3725+
pub timeout: Option<Duration>,
3726+
}
3727+
37173728
/// Delete a sandbox by name, or all sandboxes when `all` is true.
37183729
pub async fn sandbox_delete(
37193730
server: &str,
@@ -3722,6 +3733,7 @@ pub async fn sandbox_delete(
37223733
workspace: &str,
37233734
tls: &TlsOptions,
37243735
gateway: &str,
3736+
options: SandboxDeleteOptions,
37253737
) -> Result<()> {
37263738
let mut client = grpc_client(server, tls).await?;
37273739

@@ -3791,12 +3803,23 @@ pub async fn sandbox_delete(
37913803
}
37923804
};
37933805

3794-
match response.into_inner().outcome() {
3806+
let deletion = response.into_inner();
3807+
match deletion.outcome() {
37953808
DeletionOutcome::Completed => println!("{} Deleted sandbox {name}", "✓".green().bold()),
3796-
DeletionOutcome::Accepted => println!(
3809+
DeletionOutcome::Accepted if options.no_wait => println!(
37973810
"{} Sandbox {name} deletion accepted; cleanup is pending",
37983811
"✓".green().bold()
37993812
),
3813+
DeletionOutcome::Accepted => {
3814+
if let Err(err) = wait_for_sandbox_deleted(
3815+
&mut client, name, workspace, &deletion.sandbox_id, options.timeout,
3816+
).await {
3817+
eprintln!("{} Failed to verify sandbox {name} deletion: {err}", "!".red().bold());
3818+
failures.push(name.clone());
3819+
continue;
3820+
}
3821+
println!("{} Deleted sandbox {name}", "✓".green().bold());
3822+
}
38003823
DeletionOutcome::AlreadyAbsent => {
38013824
println!("{} Sandbox {name} already deleted", "✓".green().bold());
38023825
}
@@ -3815,6 +3838,76 @@ pub async fn sandbox_delete(
38153838
aggregate_delete_failures("sandbox", &failures)
38163839
}
38173840

3841+
fn lifecycle_timeout(explicit: Option<Duration>) -> Duration {
3842+
explicit.unwrap_or_else(|| {
3843+
Duration::from_secs(
3844+
std::env::var("OPENSHELL_LIFECYCLE_TIMEOUT")
3845+
.ok()
3846+
.and_then(|value| value.parse().ok())
3847+
.unwrap_or(300),
3848+
)
3849+
})
3850+
}
3851+
3852+
async fn wait_for_sandbox_deleted(
3853+
client: &mut crate::tls::GrpcClient,
3854+
name: &str,
3855+
workspace: &str,
3856+
expected_sandbox_id: &str,
3857+
timeout_override: Option<Duration>,
3858+
) -> Result<()> {
3859+
const INITIAL_POLL_INTERVAL: Duration = Duration::from_millis(100);
3860+
const MAX_POLL_INTERVAL: Duration = Duration::from_secs(2);
3861+
3862+
let timeout = lifecycle_timeout(timeout_override);
3863+
let deadline = Instant::now()
3864+
.checked_add(timeout)
3865+
.ok_or_else(|| miette!("sandbox deletion timeout is too large"))?;
3866+
let timeout_error = || {
3867+
miette!(
3868+
"timed out after {}s waiting for sandbox {name} deletion to complete; deletion continues asynchronously",
3869+
timeout.as_secs()
3870+
)
3871+
};
3872+
let mut poll_interval = INITIAL_POLL_INTERVAL;
3873+
3874+
loop {
3875+
let remaining = deadline.saturating_duration_since(Instant::now());
3876+
if remaining.is_zero() {
3877+
return Err(timeout_error());
3878+
}
3879+
3880+
match tokio::time::timeout(
3881+
remaining,
3882+
client.get_sandbox(GetSandboxRequest {
3883+
name: name.to_string(),
3884+
workspace_scope: Some(openshell_core::proto::workspace_selector(workspace)),
3885+
}),
3886+
)
3887+
.await
3888+
{
3889+
Ok(Ok(response)) if response.into_inner().sandbox.as_ref().is_some_and(|sandbox| {
3890+
!expected_sandbox_id.is_empty() && sandbox.object_id() != expected_sandbox_id
3891+
}) => return Ok(()),
3892+
Ok(Ok(_)) => {}
3893+
Ok(Err(status)) if status.code() == Code::NotFound => return Ok(()),
3894+
Ok(Err(status)) => {
3895+
return Err(miette!(
3896+
"failed to verify deletion of sandbox {name}: {status}"
3897+
));
3898+
}
3899+
Err(_) => return Err(timeout_error()),
3900+
}
3901+
3902+
let remaining = deadline.saturating_duration_since(Instant::now());
3903+
if remaining.is_zero() {
3904+
return Err(timeout_error());
3905+
}
3906+
tokio::time::sleep(poll_interval.min(remaining)).await;
3907+
poll_interval = poll_interval.saturating_mul(2).min(MAX_POLL_INTERVAL);
3908+
}
3909+
}
3910+
38183911
/// Stop a sandbox while retaining its persistent workspace.
38193912
pub async fn sandbox_stop(
38203913
server: &str,

0 commit comments

Comments
 (0)