Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 99 additions & 6 deletions crates/openshell-sandbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod sidecar_control;

use miette::{IntoDiagnostic, Result, WrapErr};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::AtomicU32;
use std::time::Duration;
Expand Down Expand Up @@ -361,7 +362,7 @@ pub async fn run_sandbox(
#[cfg(not(target_os = "linux"))]
drop(bypass_activity_tx);

let networking = if network_enabled {
let mut networking = if network_enabled {
#[cfg(target_os = "linux")]
let proxy_bind_ip = netns
.as_ref()
Expand Down Expand Up @@ -628,6 +629,19 @@ pub async fn run_sandbox(
.zip(bootstrap.proxy_ca_bundle_path.clone())
});

let proxy_exited: Pin<Box<dyn Future<Output = ()> + Send>> = if let Some(rx) = networking
.as_mut()
.and_then(|n| n.proxy.as_mut())
.map(|p| p.take_exit_receiver())
{
Box::pin(async {
let _ = rx.await;
})
} else {
Box::pin(std::future::pending())
};
tokio::pin!(proxy_exited);

let exit_code = if process_enabled {
let ca_file_paths = networking
.as_ref()
Expand Down Expand Up @@ -707,9 +721,41 @@ pub async fn run_sandbox(
"authoritative network-sidecar control channel closed"
));
}
_ = &mut proxy_exited => {
ocsf_emit!(
AppLifecycleBuilder::new(ocsf_ctx())
.activity(ActivityId::Fail)
.severity(SeverityId::High)
.status(StatusId::Failure)
.message(
"Proxy accept loop exited unexpectedly; terminating sandbox"
)
.build()
);
return Err(miette::miette!(
"proxy accept loop exited unexpectedly"
));
}
}
} else {
process.await?
tokio::select! {
result = process => result?,
_ = &mut proxy_exited => {
ocsf_emit!(
AppLifecycleBuilder::new(ocsf_ctx())
.activity(ActivityId::Fail)
.severity(SeverityId::High)
.status(StatusId::Failure)
.message(
"Proxy accept loop exited unexpectedly; terminating sandbox"
)
.build()
);
return Err(miette::miette!(
"proxy accept loop exited unexpectedly"
));
}
}
}
} else {
// Network-only sidecar mode: keep the proxy and its background
Expand All @@ -725,15 +771,62 @@ pub async fn run_sandbox(
warn!(?result, "Authoritative sidecar control channel exited; restarting sidecar");
1
}
_ = &mut proxy_exited => {
ocsf_emit!(
AppLifecycleBuilder::new(ocsf_ctx())
.activity(ActivityId::Fail)
.severity(SeverityId::High)
.status(StatusId::Failure)
.message(
"Proxy accept loop exited unexpectedly; terminating sandbox"
)
.build()
);
return Err(miette::miette!(
"proxy accept loop exited unexpectedly"
));
}
}
} else {
wait_for_shutdown_signal().await;
0
tokio::select! {
() = wait_for_shutdown_signal() => 0,
_ = &mut proxy_exited => {
ocsf_emit!(
AppLifecycleBuilder::new(ocsf_ctx())
.activity(ActivityId::Fail)
.severity(SeverityId::High)
.status(StatusId::Failure)
.message(
"Proxy accept loop exited unexpectedly; terminating sandbox"
)
.build()
);
return Err(miette::miette!(
"proxy accept loop exited unexpectedly"
));
}
}
}
#[cfg(not(target_os = "linux"))]
{
wait_for_shutdown_signal().await;
0
tokio::select! {
() = wait_for_shutdown_signal() => 0,
_ = &mut proxy_exited => {
ocsf_emit!(
AppLifecycleBuilder::new(ocsf_ctx())
.activity(ActivityId::Fail)
.severity(SeverityId::High)
.status(StatusId::Failure)
.message(
"Proxy accept loop exited unexpectedly; terminating sandbox"
)
.build()
);
return Err(miette::miette!(
"proxy accept loop exited unexpectedly"
));
}
}
}
};

Expand Down
Loading
Loading