Skip to content

Commit 2c95079

Browse files
committed
fix(supervisor): accept legacy supervisors without a protocol revision
Sandboxes keep their supervisor binary until they are recreated, so a gateway upgrade meets supervisors that predate the handshake and report revision zero. Rejecting them severs every running sandbox with no automatic recovery. Accept revision zero for one release, log a warning per session, and count them in openshell_supervisor_protocol_legacy_sessions_total. The supervisor mirrors the allowance for gateways that predate the handshake. Add a shared ConnectSupervisor test harness and handler-level tests for legacy acceptance and unknown-revision rejection. Move the skill troubleshooting paragraph out of the numbered deployment list so the list renders. Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com>
1 parent fc3a92d commit 2c95079

9 files changed

Lines changed: 196 additions & 61 deletions

File tree

‎architecture/sandbox.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,9 @@ quickly.
476476
## Supervisor Configuration Delivery
477477

478478
The gateway and supervisor must implement the same internal supervisor protocol
479-
revision. The gateway includes a configuration bootstrap when it accepts a
479+
revision. Peers built before the handshake existed report revision zero and are
480+
accepted for one release with a warning and a counter, because sandboxes keep
481+
their supervisor binary until they are recreated. The gateway includes a configuration bootstrap when it accepts a
480482
`ConnectSupervisor` session and can send complete component replacements on the
481483
same stream after policy, settings, or provider state changes.
482484
Bootstrap construction does not gate the session while polling remains

‎crates/openshell-core/src/proto/mod.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,11 @@ pub fn all_workspaces_selector() -> WorkspaceSelector {
101101
/// Bump this when either peer can no longer honor the previous stream
102102
/// semantics.
103103
pub const SUPERVISOR_PROTOCOL_REVISION: u32 = 1;
104+
105+
/// Revision implied by peers built before the handshake existed. Proto3 leaves
106+
/// the field unset, so such peers report zero.
107+
///
108+
/// Sandboxes keep their supervisor binary until they are recreated, so a
109+
/// gateway upgrade must keep serving them for one release. Remove this
110+
/// allowance once every supported release sends an explicit revision.
111+
pub const LEGACY_SUPERVISOR_PROTOCOL_REVISION: u32 = 0;

‎crates/openshell-server/src/config_delivery.rs‎

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -541,14 +541,8 @@ mod tests {
541541
use std::sync::atomic::{AtomicUsize, Ordering};
542542

543543
use super::*;
544-
use crate::grpc::{OpenShellService, test_support::test_server_state};
545-
use openshell_core::proto::{
546-
GatewayMessage, ObjectMeta, SandboxSpec, SupervisorHello, SupervisorMessage,
547-
gateway_message, open_shell_client::OpenShellClient, open_shell_server::OpenShellServer,
548-
supervisor_message,
549-
};
550-
use tokio::sync::mpsc;
551-
use tokio_stream::wrappers::{ReceiverStream, TcpListenerStream};
544+
use crate::grpc::test_support::{connect_supervisor_stream, test_server_state};
545+
use openshell_core::proto::{GatewayMessage, ObjectMeta, SandboxSpec, gateway_message};
552546

553547
fn key(sandbox_id: &str, component: ConfigComponentKind) -> DeliveryKey {
554548
DeliveryKey {
@@ -727,35 +721,15 @@ mod tests {
727721
.await
728722
.unwrap();
729723

730-
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
731-
let address = listener.local_addr().unwrap();
732-
let server = tokio::spawn(
733-
tonic::transport::Server::builder()
734-
.add_service(OpenShellServer::new(OpenShellService::new(Arc::clone(
735-
&state,
736-
))))
737-
.serve_with_incoming(TcpListenerStream::new(listener)),
738-
);
739-
let mut client = OpenShellClient::connect(format!("http://{address}"))
740-
.await
741-
.unwrap();
742-
let (tx, rx) = mpsc::channel(4);
743-
tx.send(SupervisorMessage {
744-
payload: Some(supervisor_message::Payload::Hello(SupervisorHello {
745-
sandbox_id: "sandbox".into(),
746-
instance_id: "instance".into(),
747-
protocol_revision: openshell_core::proto::SUPERVISOR_PROTOCOL_REVISION,
748-
})),
749-
})
724+
let mut harness = connect_supervisor_stream(
725+
&state,
726+
"sandbox",
727+
openshell_core::proto::SUPERVISOR_PROTOCOL_REVISION,
728+
)
750729
.await
751730
.unwrap();
752-
let mut stream = client
753-
.connect_supervisor(ReceiverStream::new(rx))
754-
.await
755-
.unwrap()
756-
.into_inner();
757731

758-
let first = tokio::time::timeout(Duration::from_secs(5), stream.message())
732+
let first = tokio::time::timeout(Duration::from_secs(5), harness.inbound.message())
759733
.await
760734
.unwrap()
761735
.unwrap()
@@ -766,7 +740,7 @@ mod tests {
766740
));
767741

768742
publish_sandbox_components(&state, "sandbox", ConfigComponents::SANDBOX_CONFIG);
769-
let update = tokio::time::timeout(Duration::from_secs(5), stream.message())
743+
let update = tokio::time::timeout(Duration::from_secs(5), harness.inbound.message())
770744
.await
771745
.unwrap()
772746
.unwrap()
@@ -777,9 +751,6 @@ mod tests {
777751
payload: Some(gateway_message::Payload::ConfigUpdate(_))
778752
}
779753
));
780-
781-
drop(tx);
782-
server.abort();
783754
}
784755

785756
#[test]

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,8 +818,74 @@ pub mod test_support {
818818
use crate::supervisor_session::SupervisorSessionRegistry;
819819
use crate::tracing_bus::TracingLogBus;
820820
use openshell_core::Config;
821+
use openshell_core::proto::open_shell_client::OpenShellClient;
822+
use openshell_core::proto::open_shell_server::OpenShellServer;
823+
use openshell_core::proto::{
824+
GatewayMessage, SupervisorHello, SupervisorMessage, supervisor_message,
825+
};
826+
use tokio::sync::mpsc;
827+
use tokio_stream::wrappers::{ReceiverStream, TcpListenerStream};
821828
use tonic::Request;
822829

830+
/// A live `ConnectSupervisor` stream against an in-process gateway.
831+
pub struct SupervisorStreamHarness {
832+
server: tokio::task::JoinHandle<Result<(), tonic::transport::Error>>,
833+
/// Held so the supervisor side of the stream stays open.
834+
_outbound: mpsc::Sender<SupervisorMessage>,
835+
pub inbound: tonic::Streaming<GatewayMessage>,
836+
}
837+
838+
impl Drop for SupervisorStreamHarness {
839+
fn drop(&mut self) {
840+
self.server.abort();
841+
}
842+
}
843+
844+
/// Serve `state` on loopback and open a supervisor stream whose hello
845+
/// carries the given protocol revision. Returns the gRPC status when the
846+
/// gateway rejects the stream before accepting it.
847+
pub async fn connect_supervisor_stream(
848+
state: &Arc<ServerState>,
849+
sandbox_id: &str,
850+
protocol_revision: u32,
851+
) -> Result<SupervisorStreamHarness, tonic::Status> {
852+
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
853+
let address = listener.local_addr().unwrap();
854+
let server = tokio::spawn(
855+
tonic::transport::Server::builder()
856+
.add_service(OpenShellServer::new(super::OpenShellService::new(
857+
Arc::clone(state),
858+
)))
859+
.serve_with_incoming(TcpListenerStream::new(listener)),
860+
);
861+
let mut client = OpenShellClient::connect(format!("http://{address}"))
862+
.await
863+
.unwrap();
864+
let (outbound, rx) = mpsc::channel(4);
865+
outbound
866+
.send(SupervisorMessage {
867+
payload: Some(supervisor_message::Payload::Hello(SupervisorHello {
868+
sandbox_id: sandbox_id.into(),
869+
instance_id: "instance".into(),
870+
protocol_revision,
871+
})),
872+
})
873+
.await
874+
.unwrap();
875+
let inbound = match client.connect_supervisor(ReceiverStream::new(rx)).await {
876+
Ok(response) => response.into_inner(),
877+
Err(status) => {
878+
server.abort();
879+
return Err(status);
880+
}
881+
};
882+
Ok(SupervisorStreamHarness {
883+
server,
884+
_outbound: outbound,
885+
inbound,
886+
})
887+
}
888+
823889
/// Wrap a proto message in a `Request` with a dev principal injected.
824890
///
825891
/// The dev principal matches the unauthenticated dev user: subject

‎crates/openshell-server/src/supervisor_session.rs‎

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ use tonic::{Request, Response, Status};
1515
use tracing::{debug, info, warn};
1616
use uuid::Uuid;
1717

18-
use openshell_core::proto::SUPERVISOR_PROTOCOL_REVISION;
1918
use openshell_core::proto::{
2019
ConfigUpdate, GatewayMessage, RelayFrame, RelayInit, RelayOpen, ReportMainProcessExitRequest,
2120
ReportMainProcessExitResponse, Sandbox, SandboxPhase, SessionAccepted, SshRelayTarget,
2221
SupervisorMessage, config_update, gateway_message, relay_open, supervisor_message,
2322
};
23+
use openshell_core::proto::{LEGACY_SUPERVISOR_PROTOCOL_REVISION, SUPERVISOR_PROTOCOL_REVISION};
2424
use openshell_core::transport_errors::is_expected_transport_close_status;
2525

2626
use crate::ServerState;
@@ -810,7 +810,7 @@ pub async fn handle_connect_supervisor(
810810
if sandbox_id.is_empty() {
811811
return Err(Status::invalid_argument("sandbox_id is required"));
812812
}
813-
validate_protocol_revision(hello.protocol_revision)?;
813+
validate_protocol_revision(&sandbox_id, hello.protocol_revision)?;
814814
if let Some(principal) = principal.as_ref() {
815815
crate::auth::guard::ensure_sandbox_principal_scope(principal, &sandbox_id)?;
816816
}
@@ -960,13 +960,20 @@ pub async fn handle_connect_supervisor(
960960
Ok(Response::new(stream))
961961
}
962962

963-
fn validate_protocol_revision(supervisor_revision: u32) -> Result<(), Status> {
964-
if supervisor_revision == SUPERVISOR_PROTOCOL_REVISION {
965-
Ok(())
966-
} else {
967-
Err(Status::failed_precondition(format!(
968-
"supervisor protocol revision mismatch: gateway requires {SUPERVISOR_PROTOCOL_REVISION}, supervisor offered {supervisor_revision}"
969-
)))
963+
fn validate_protocol_revision(sandbox_id: &str, supervisor_revision: u32) -> Result<(), Status> {
964+
match supervisor_revision {
965+
SUPERVISOR_PROTOCOL_REVISION => Ok(()),
966+
LEGACY_SUPERVISOR_PROTOCOL_REVISION => {
967+
counter!("openshell_supervisor_protocol_legacy_sessions_total").increment(1);
968+
warn!(
969+
sandbox_id = %sandbox_id,
970+
"supervisor session: supervisor predates the protocol handshake; recreate the sandbox before the next gateway upgrade"
971+
);
972+
Ok(())
973+
}
974+
other => Err(Status::failed_precondition(format!(
975+
"supervisor protocol revision mismatch: gateway requires {SUPERVISOR_PROTOCOL_REVISION}, supervisor offered {other}"
976+
))),
970977
}
971978
}
972979

@@ -1246,9 +1253,77 @@ mod tests {
12461253
}
12471254

12481255
#[test]
1249-
fn supervisor_protocol_revision_must_match_exactly() {
1250-
assert!(validate_protocol_revision(SUPERVISOR_PROTOCOL_REVISION).is_ok());
1251-
let error = validate_protocol_revision(SUPERVISOR_PROTOCOL_REVISION + 1).unwrap_err();
1256+
fn supervisor_protocol_revision_accepts_current_and_legacy_peers() {
1257+
assert!(validate_protocol_revision("sb-1", SUPERVISOR_PROTOCOL_REVISION).is_ok());
1258+
assert!(validate_protocol_revision("sb-1", LEGACY_SUPERVISOR_PROTOCOL_REVISION).is_ok());
1259+
}
1260+
1261+
async fn state_with_sandbox(sandbox_id: &str) -> Arc<ServerState> {
1262+
let state = crate::grpc::test_support::test_server_state().await;
1263+
state
1264+
.store
1265+
.put_message(&sandbox_record(sandbox_id, sandbox_id))
1266+
.await
1267+
.unwrap();
1268+
state
1269+
}
1270+
1271+
async fn first_gateway_message(
1272+
harness: &mut crate::grpc::test_support::SupervisorStreamHarness,
1273+
) -> GatewayMessage {
1274+
tokio::time::timeout(Duration::from_secs(5), harness.inbound.message())
1275+
.await
1276+
.expect("gateway response before timeout")
1277+
.expect("stream open")
1278+
.expect("gateway message")
1279+
}
1280+
1281+
#[tokio::test]
1282+
async fn legacy_supervisor_without_protocol_revision_is_accepted() {
1283+
let state = state_with_sandbox("sb-legacy").await;
1284+
let mut harness = crate::grpc::test_support::connect_supervisor_stream(
1285+
&state,
1286+
"sb-legacy",
1287+
LEGACY_SUPERVISOR_PROTOCOL_REVISION,
1288+
)
1289+
.await
1290+
.expect("legacy supervisor must connect");
1291+
1292+
let Some(gateway_message::Payload::SessionAccepted(accepted)) =
1293+
first_gateway_message(&mut harness).await.payload
1294+
else {
1295+
panic!("expected SessionAccepted");
1296+
};
1297+
assert_eq!(accepted.protocol_revision, SUPERVISOR_PROTOCOL_REVISION);
1298+
assert!(
1299+
state
1300+
.supervisor_sessions
1301+
.is_current_session("sb-legacy", &accepted.session_id)
1302+
);
1303+
}
1304+
1305+
#[tokio::test]
1306+
async fn unknown_supervisor_protocol_revision_is_rejected() {
1307+
let state = state_with_sandbox("sb-future").await;
1308+
let Err(status) = crate::grpc::test_support::connect_supervisor_stream(
1309+
&state,
1310+
"sb-future",
1311+
SUPERVISOR_PROTOCOL_REVISION + 1,
1312+
)
1313+
.await
1314+
else {
1315+
panic!("mismatched revision must be rejected");
1316+
};
1317+
1318+
assert_eq!(status.code(), tonic::Code::FailedPrecondition);
1319+
assert!(status.message().contains("revision mismatch"));
1320+
assert!(state.supervisor_sessions.connected_sandbox_ids().is_empty());
1321+
}
1322+
1323+
#[test]
1324+
fn supervisor_protocol_revision_rejects_unknown_peers() {
1325+
let error =
1326+
validate_protocol_revision("sb-1", SUPERVISOR_PROTOCOL_REVISION + 1).unwrap_err();
12521327
assert_eq!(error.code(), tonic::Code::FailedPrecondition);
12531328
assert!(error.message().contains("revision mismatch"));
12541329
}

‎crates/openshell-supervisor-process/src/supervisor_session.rs‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ use std::sync::Arc;
1717
use std::sync::atomic::{AtomicBool, Ordering};
1818
use std::time::Duration;
1919

20-
use openshell_core::proto::SUPERVISOR_PROTOCOL_REVISION;
2120
use openshell_core::proto::open_shell_client::OpenShellClient;
2221
use openshell_core::proto::{
2322
FinalizeMainProcessExitRequest, GatewayMessage, RelayFrame, RelayInit, RelayOpen,
2423
RelayOpenResult, ReportMainProcessExitRequest, SupervisorHeartbeat, SupervisorHello,
2524
SupervisorMessage, TcpRelayTarget, gateway_message, relay_open, supervisor_message,
2625
};
26+
use openshell_core::proto::{LEGACY_SUPERVISOR_PROTOCOL_REVISION, SUPERVISOR_PROTOCOL_REVISION};
2727
use openshell_ocsf::{
2828
ActivityId, ConnectionInfo, Endpoint, EventContext, NetworkActivityBuilder, OcsfEvent,
2929
SeverityId, StatusId, ocsf_emit,
@@ -447,13 +447,18 @@ async fn run_single_session(
447447
fn validate_gateway_protocol_revision(
448448
gateway_revision: u32,
449449
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
450-
if gateway_revision == SUPERVISOR_PROTOCOL_REVISION {
451-
Ok(())
452-
} else {
453-
Err(format!(
454-
"supervisor protocol revision mismatch: supervisor requires {SUPERVISOR_PROTOCOL_REVISION}, gateway offered {gateway_revision}"
450+
match gateway_revision {
451+
SUPERVISOR_PROTOCOL_REVISION => Ok(()),
452+
LEGACY_SUPERVISOR_PROTOCOL_REVISION => {
453+
warn!(
454+
"supervisor session: gateway predates the protocol handshake; upgrade the gateway before pinning newer supervisor images"
455+
);
456+
Ok(())
457+
}
458+
other => Err(format!(
459+
"supervisor protocol revision mismatch: supervisor requires {SUPERVISOR_PROTOCOL_REVISION}, gateway offered {other}"
455460
)
456-
.into())
461+
.into()),
457462
}
458463
}
459464

@@ -863,8 +868,13 @@ mod target_tests {
863868
use super::*;
864869

865870
#[test]
866-
fn gateway_protocol_revision_must_match_exactly() {
871+
fn gateway_protocol_revision_accepts_current_and_legacy_peers() {
867872
assert!(validate_gateway_protocol_revision(SUPERVISOR_PROTOCOL_REVISION).is_ok());
873+
assert!(validate_gateway_protocol_revision(LEGACY_SUPERVISOR_PROTOCOL_REVISION).is_ok());
874+
}
875+
876+
#[test]
877+
fn gateway_protocol_revision_rejects_unknown_peers() {
868878
let error = validate_gateway_protocol_revision(SUPERVISOR_PROTOCOL_REVISION + 1)
869879
.expect_err("version skew must be rejected");
870880
assert!(error.to_string().contains("revision mismatch"));

‎docs/reference/gateway-config.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ disable_tls = false
108108
default_image = "ghcr.io/nvidia/openshell-community/sandboxes/base:latest"
109109
# Defaults to the gateway version. Custom builds must match the gateway's
110110
# internal supervisor protocol revision; mismatched peers reject the session.
111+
# Supervisors from releases before the handshake existed still connect, with
112+
# a gateway warning, until the next release.
111113
# supervisor_image = "ghcr.io/nvidia/openshell/supervisor:<version>"
112114
client_tls_secret_name = "openshell-client-tls"
113115
service_account_name = "openshell-sandbox"

‎proto/openshell.proto‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,6 +2517,7 @@ message SupervisorHello {
25172517
// Supervisor instance ID (e.g. boot id or process epoch).
25182518
string instance_id = 2;
25192519
// Exact internal stream protocol revision implemented by this supervisor.
2520+
// Zero identifies a supervisor built before the handshake existed.
25202521
uint32 protocol_revision = 3;
25212522
}
25222523

‎skills/debug-openshell-cluster/SKILL.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ The target deployment flow is:
1616
1. Operator starts or deploys the gateway with system packages, systemd, or Helm. The CLI does not start, stop, or destroy gateway services.
1717
2. Operator configures the compute driver.
1818
3. Operator provides the CLI and supervisor authentication material required by the deployment mode: edge or OIDC user auth, optional CLI mTLS, and gateway-minted sandbox JWTs.
19-
20-
If supervisor sessions fail with a protocol revision mismatch, check that custom supervisor images match the gateway release. Gateway and supervisor require the same internal protocol revision; authentication success does not make mismatched versions compatible. See the published [gateway configuration reference](https://docs.nvidia.com/openshell/latest/reference/gateway-config).
2119
4. The CLI registers a reachable gateway endpoint with `openshell gateway add`.
2220
5. The gateway creates sandboxes through the selected compute driver.
2321

22+
If supervisor sessions fail with a protocol revision mismatch, check that custom supervisor images match the gateway release. Gateway and supervisor require the same internal protocol revision; authentication success does not make mismatched versions compatible. Supervisors that predate the handshake still connect for one release. The gateway logs a warning for each such session and counts them in the `openshell_supervisor_protocol_legacy_sessions_total` metric, so recreate those sandboxes before the next gateway upgrade. See the published [gateway configuration reference](https://docs.nvidia.com/openshell/latest/reference/gateway-config.md).
23+
2424
The `openshell-gateway` composition crate explicitly installs its compiled
2525
Docker, Podman, Kubernetes, and VM registrations at startup; `openshell-server`
2626
does not link compute-driver crates. Custom gateway binaries may include a

0 commit comments

Comments
 (0)