Skip to content

Commit 89fcc52

Browse files
authored
fix(supervisor): bound dedicated SIGTERM shutdown and stop recovering a lost boundary (#5342)
* fix(supervisor): stop recovering a lost boundary after SIGTERM After SIGTERM the supervisor signals, waits for, and terminates the boundary. When the boundary connection drops during that sequence, each call retried reattach for up to 30s, so the supervisor outlived the runtime's stop timeout and was killed. Once shutdown starts, report a lost connection instead of recovering it. Un-ignore the conformance test that covers slow-TERM deletes. Signed-off-by: divesh <dgude@nvidia.com> * fix(supervisor): bound SIGTERM shutdown and give Docker supervisors 10s Stopping a dedicated sandbox now finishes within 8 seconds, including the exit report and trace flush, while the workload keeps its 5 second TERM grace. A boundary connection lost while stopping is reported as a terminated workload instead of failing the shutdown. Only a dedicated supervisor's own shutdown skips boundary recovery; a shared supervisor removing one sandbox keeps recovering as before. The Docker driver gave the supervisor 5 seconds, the same as its wait for the workload, so stops ended in SIGKILL. It now allows 10 seconds. Signed-off-by: divesh <dgude@nvidia.com> * fix(supervisor): keep shared stops unbounded and report the workload's exit Only a dedicated supervisor's own shutdown is bounded by the 8 second deadline and treats a lost boundary as the end of the workload. A shared supervisor stopping one hosted instance keeps the host's removal grace and reports a stop it could not confirm as a failure, as before. When the boundary reports the workload's exit status before it is lost, that status is reported rather than the terminated fallback. Signed-off-by: divesh <dgude@nvidia.com> --------- Signed-off-by: divesh <dgude@nvidia.com>
1 parent 31f230e commit 89fcc52

7 files changed

Lines changed: 270 additions & 29 deletions

File tree

‎crates/openshell-driver-docker/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ New generations require matching supervisor and sandbox runtime binaries.
128128

129129
## Stop, Start, and Delete
130130

131-
Stop terminates the supervisor companion and stops the workload container
132-
without removing it. Docker retains the workload writable layer and attached
131+
Stop terminates the supervisor companion, allowing it 10 seconds to stop the
132+
workload, and stops the workload container without removing it. Docker retains the workload writable layer and attached
133133
volumes. Start stages a fresh sandbox bootstrap bundle, restarts that workload,
134134
and creates a new supervisor companion. A durably stopped sandbox stays stopped
135135
across gateway restarts.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ const WATCH_POLL_MAX_BACKOFF: Duration = Duration::from_secs(30);
9595
// request a bounded opportunity to mark the control exit intentional before
9696
// publishing a fail-closed runtime error.
9797
const SUPERVISOR_INTENTIONAL_SHUTDOWN_GRACE: Duration = Duration::from_secs(1);
98+
// Longer than the supervisor's own bound on stopping its workload, so it
99+
// finishes stopping before Docker kills it.
100+
const SUPERVISOR_STOP_TIMEOUT_SECS: i32 = 10;
98101
const SUPERVISOR_HEALTH_INTERVAL_NS: i64 = 30_000_000_000;
99102
// Docker probes at this interval only before the first healthy result and
100103
// within the start period.
@@ -5610,7 +5613,7 @@ async fn spawn_docker_control_process(
56105613
_ = &mut shutdown_requested => {
56115614
let _ = monitored_docker.stop_container(
56125615
&monitored_supervisor_id,
5613-
Some(StopContainerOptionsBuilder::default().t(5).build()),
5616+
Some(StopContainerOptionsBuilder::default().t(SUPERVISOR_STOP_TIMEOUT_SECS).build()),
56145617
).await;
56155618
let _ = remove_supervisor().await;
56165619
}

‎crates/openshell-sandbox-backend/src/runtime.rs‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,12 @@ impl RunningBoundary {
971971
expect_response(response, "boundary_terminated")
972972
}
973973

974+
/// Stop waiting for a lost boundary connection to recover. The owner is
975+
/// stopping, and the boundary may be stopping with it.
976+
pub fn begin_stop(&self) {
977+
self.process.client.stopping.store(true, Ordering::Release);
978+
}
979+
974980
/// Whether the negotiated backend advertised suspend/resume support.
975981
pub fn supports_suspend(&self) -> bool {
976982
self.process
@@ -1615,6 +1621,9 @@ struct BoundaryClient {
16151621
recovery_timeout_secs: Option<u32>,
16161622
/// True once a connection of this client was confirmed.
16171623
confirmed: tokio::sync::watch::Sender<bool>,
1624+
/// Set when the owner stops the boundary; a lost connection is then
1625+
/// reported instead of recovered.
1626+
stopping: AtomicBool,
16181627
}
16191628

16201629
#[derive(Clone)]
@@ -1815,6 +1824,7 @@ impl BoundaryClient {
18151824
requires_takeover: false,
18161825
recovery_timeout_secs: None,
18171826
confirmed: tokio::sync::watch::Sender::new(false),
1827+
stopping: AtomicBool::new(false),
18181828
}
18191829
}
18201830

@@ -2223,6 +2233,11 @@ impl BoundaryClient {
22232233
}
22242234
cached.transport.abort();
22252235
}
2236+
if self.stopping.load(Ordering::Acquire) {
2237+
return Err(BackendError::Unavailable(
2238+
"boundary connection lost while stopping".to_string(),
2239+
));
2240+
}
22262241

22272242
*self.mediation.lock().await = None;
22282243
let attach = self
@@ -3992,6 +4007,72 @@ mod tests {
39924007
assert_eq!(requests.load(Ordering::Acquire), 5);
39934008
}
39944009

4010+
#[tokio::test]
4011+
async fn stopping_client_reports_a_lost_boundary_without_recovery() {
4012+
let certificate = test_certificate();
4013+
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
4014+
let address = listener.local_addr().unwrap();
4015+
let server_config = certificate.server_config.clone();
4016+
let server = tokio::spawn(async move {
4017+
let (stream, _) = listener.accept().await.unwrap();
4018+
let stream = tokio_rustls::TlsAcceptor::from(server_config)
4019+
.accept(stream)
4020+
.await
4021+
.unwrap();
4022+
let service = TestGrpcBoundary {
4023+
wait_for_half_close: false,
4024+
expected_token: "a".repeat(32),
4025+
requests: Arc::default(),
4026+
mediation_failures: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
4027+
mediation_ready: false,
4028+
provider_environment_generation: 0,
4029+
confirmation: test_confirmation(),
4030+
attach_snapshots: Arc::default(),
4031+
received: Arc::default(),
4032+
};
4033+
tonic::transport::Server::builder()
4034+
.add_service(IsolationBackendServer::new(TestIsolationBackend::default()))
4035+
.add_service(SandboxProtocolServer::new(service))
4036+
.serve_with_incoming(tokio_stream::iter([Ok::<_, std::io::Error>(TestTlsIo(
4037+
Box::new(stream),
4038+
))]))
4039+
.await
4040+
.unwrap();
4041+
});
4042+
let client = BoundaryClient::new(
4043+
tls_runtime_descriptor(address, certificate.client_tls),
4044+
test_bearer(&"a".repeat(32)),
4045+
);
4046+
let attach = Request::Attach {
4047+
supervisor_instance_id: client.supervisor_instance_id,
4048+
policy: Box::new(SandboxPolicyWire::from(sandbox().policy)),
4049+
supervisor_takeover: None,
4050+
supervisor_recovery_timeout_secs: None,
4051+
};
4052+
client.call_idempotent(attach).await.unwrap();
4053+
client.call_idempotent(Request::Confirm).await.unwrap();
4054+
4055+
// The boundary stops with its owner: no listener remains to reattach.
4056+
server.abort();
4057+
let _ = server.await;
4058+
client.stopping.store(true, Ordering::Release);
4059+
client
4060+
.grpc_channel
4061+
.lock()
4062+
.await
4063+
.as_ref()
4064+
.expect("cached channel")
4065+
.transport
4066+
.abort();
4067+
let result = tokio::time::timeout(
4068+
Duration::from_secs(5),
4069+
client.call_idempotent(Request::TerminateBoundary),
4070+
)
4071+
.await
4072+
.expect("a stopping client must not wait for the boundary to recover");
4073+
assert!(matches!(result, Err(BackendError::Unavailable(_))));
4074+
}
4075+
39954076
#[test]
39964077
fn wait_recovery_window_begins_at_transport_failure() {
39974078
let wait_started = tokio::time::Instant::now();

‎crates/openshell-supervisor/README.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ loads the launch configuration from its arguments and environment and prepares
5757
push and the OCSF JSON settings with its logging layers, so renewal by either
5858
gateway client keeps both current. `run_sandbox_with_backend` sets the
5959
process-wide OCSF context, binds the readiness socket and port to instance
60-
readiness, and stops the instance on SIGTERM or SIGINT.
60+
readiness, and stops the instance on SIGTERM or SIGINT. Stopping gives the
61+
workload 5 seconds to exit after TERM, and the instance stops, including its
62+
exit report and trace flush, within 8 seconds. Telemetry exporters shut down
63+
after that and are not covered by this bound. A boundary connection lost while
64+
the process stops is not recovered; the workload's exit status is reported
65+
when the boundary returned it, otherwise the workload is reported as
66+
terminated.
6167

6268
`sandbox_instance_host::SandboxInstanceHost` runs several sandbox instances in
6369
one process. Each instance gets its own Tokio runtime; every thread of that

0 commit comments

Comments
 (0)