Skip to content

Commit 3053b2f

Browse files
committed
fix(supervisor): bound optional bootstrap latency on reconnect
Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com>
1 parent fe2fa4e commit 3053b2f

5 files changed

Lines changed: 155 additions & 3 deletions

File tree

‎architecture/sandbox.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,10 @@ accepted for one release with a warning and a counter, because sandboxes keep
481481
their supervisor binary until they are recreated. The gateway includes a configuration bootstrap when it accepts a
482482
`ConnectSupervisor` session and can send complete component replacements on the
483483
same stream after policy, settings, or provider state changes.
484-
Bootstrap construction does not gate the session while polling remains
485-
authoritative. These payloads describe the latest effective state rather than
484+
While polling remains authoritative, optional bootstrap construction has a
485+
one-second budget. The gateway accepts the session without a bootstrap when
486+
that budget expires, so slow credential backends do not block relay reconnects.
487+
These payloads describe the latest effective state rather than
486488
the mutation that produced it. The gateway assigns ordering sequences within
487489
each session and component, while each snapshot retains its own content
488490
revision.

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

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ use crate::supervisor_session::SupervisorSessionRegistry;
2626
/// future envelope fields.
2727
pub const MAX_SUPERVISOR_CONFIG_MESSAGE_BYTES: usize = 3 * 1024 * 1024;
2828
const CONFIG_SNAPSHOT_BUILD_TIMEOUT: Duration = Duration::from_secs(45);
29+
// Stage 1 bootstrap is optional. Keep credential backend stalls well below
30+
// the 15-second relay session-wait budget while polling remains authoritative.
31+
const CONFIG_BOOTSTRAP_BUILD_TIMEOUT: Duration = Duration::from_secs(1);
2932
const MAX_ACTIVE_FANOUT_WORKERS: usize = 64;
3033
/// Concurrent snapshot builds allowed per pooled database connection. Builds
3134
/// are short bursts of small queries, so a little oversubscription keeps the
@@ -321,7 +324,7 @@ pub async fn build_config_bootstrap(
321324
sandbox: &Sandbox,
322325
) -> Result<ConfigBootstrap, Status> {
323326
tokio::time::timeout(
324-
CONFIG_SNAPSHOT_BUILD_TIMEOUT,
327+
CONFIG_BOOTSTRAP_BUILD_TIMEOUT,
325328
build_consistent_config_bootstrap(state, sandbox),
326329
)
327330
.await
@@ -773,4 +776,85 @@ mod tests {
773776
.provider_env_revision = 7;
774777
assert!(bootstrap_revisions_match(&bootstrap));
775778
}
779+
780+
#[tokio::test]
781+
async fn stalled_credentials_do_not_block_session_acceptance() {
782+
use openshell_core::proto::{CredentialHandle, Provider};
783+
784+
let state = test_server_state().await;
785+
state
786+
.store
787+
.put_message(&Provider {
788+
metadata: Some(ObjectMeta {
789+
id: "provider".into(),
790+
name: "provider".into(),
791+
workspace: "default".into(),
792+
..Default::default()
793+
}),
794+
r#type: "github".into(),
795+
credential_handles: HashMap::from([(
796+
"GITHUB_TOKEN".into(),
797+
CredentialHandle {
798+
driver: "test-static".into(),
799+
handle: "blocked".into(),
800+
..Default::default()
801+
},
802+
)]),
803+
..Default::default()
804+
})
805+
.await
806+
.unwrap();
807+
state
808+
.store
809+
.put_message(&Sandbox {
810+
metadata: Some(ObjectMeta {
811+
id: "sandbox".into(),
812+
name: "sandbox".into(),
813+
workspace: "default".into(),
814+
..Default::default()
815+
}),
816+
spec: Some(SandboxSpec {
817+
providers: vec!["provider".into()],
818+
..Default::default()
819+
}),
820+
..Default::default()
821+
})
822+
.await
823+
.unwrap();
824+
let (resolve_hit, _release_resolve) = state.credentials.gate_next_resolve();
825+
tokio::time::timeout(Duration::from_secs(10), async {
826+
let connect = connect_supervisor_stream(
827+
&state,
828+
"sandbox",
829+
openshell_core::proto::SUPERVISOR_PROTOCOL_REVISION,
830+
);
831+
let (response, hit) = tokio::join!(connect, resolve_hit);
832+
hit.expect("bootstrap must reach the stalled credential driver");
833+
let mut harness = response.unwrap();
834+
let first = harness.inbound.message().await.unwrap().unwrap();
835+
let Some(gateway_message::Payload::SessionAccepted(accepted)) = first.payload else {
836+
panic!("expected session acceptance");
837+
};
838+
assert!(accepted.bootstrap.is_none());
839+
assert!(
840+
state
841+
.supervisor_sessions
842+
.is_current_session("sandbox", &accepted.session_id)
843+
);
844+
// Relay control remains usable while credential resolution is stalled.
845+
let (_, relay) = state
846+
.supervisor_sessions
847+
.open_relay("sandbox", Duration::from_secs(1))
848+
.await
849+
.unwrap();
850+
let message = harness.inbound.message().await.unwrap().unwrap();
851+
assert!(matches!(
852+
message.payload,
853+
Some(gateway_message::Payload::RelayOpen(_))
854+
));
855+
drop(relay);
856+
})
857+
.await
858+
.expect("optional bootstrap must not consume the relay reconnect budget");
859+
}
776860
}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ pub trait CredentialDriver: std::fmt::Debug + Send + Sync {
9292
#[cfg(test)]
9393
fn fail_next_delete(&self) {}
9494

95+
#[cfg(test)]
96+
fn gate_next_resolve(
97+
&self,
98+
) -> Option<(
99+
tokio::sync::oneshot::Receiver<()>,
100+
tokio::sync::oneshot::Sender<()>,
101+
)> {
102+
None
103+
}
104+
95105
#[cfg(test)]
96106
fn gate_next_store(
97107
&self,
@@ -268,6 +278,19 @@ impl CredentialRuntime {
268278
.expect("test credential driver supports store gating")
269279
}
270280

281+
#[cfg(test)]
282+
pub(crate) fn gate_next_resolve(
283+
&self,
284+
) -> (
285+
tokio::sync::oneshot::Receiver<()>,
286+
tokio::sync::oneshot::Sender<()>,
287+
) {
288+
self.drivers
289+
.get(&self.registry.storage_owner_name())
290+
.and_then(|driver| driver.gate_next_resolve())
291+
.expect("test credential driver supports resolve gating")
292+
}
293+
271294
pub async fn store_provider_credentials(
272295
&self,
273296
provider_name: &str,
@@ -1711,6 +1734,13 @@ struct TestStaticCredentialDriver {
17111734
fail_next_store: std::sync::atomic::AtomicBool,
17121735
fail_next_delete: std::sync::atomic::AtomicBool,
17131736
#[cfg(test)]
1737+
resolve_gate: std::sync::Mutex<
1738+
Option<(
1739+
tokio::sync::oneshot::Sender<()>,
1740+
tokio::sync::oneshot::Receiver<()>,
1741+
)>,
1742+
>,
1743+
#[cfg(test)]
17141744
store_gate: std::sync::Mutex<
17151745
Option<(
17161746
tokio::sync::oneshot::Sender<()>,
@@ -1729,6 +1759,8 @@ impl TestStaticCredentialDriver {
17291759
fail_next_store: std::sync::atomic::AtomicBool::new(false),
17301760
fail_next_delete: std::sync::atomic::AtomicBool::new(false),
17311761
#[cfg(test)]
1762+
resolve_gate: std::sync::Mutex::new(None),
1763+
#[cfg(test)]
17321764
store_gate: std::sync::Mutex::new(None),
17331765
}
17341766
}
@@ -1806,6 +1838,17 @@ impl CredentialDriver for TestStaticCredentialDriver {
18061838
requests: Vec<ResolveCredentialRequest>,
18071839
) -> Result<Vec<ResolvedCredential>, Status> {
18081840
let mut responses = Vec::with_capacity(requests.len());
1841+
#[cfg(test)]
1842+
let gate = self
1843+
.resolve_gate
1844+
.lock()
1845+
.ok()
1846+
.and_then(|mut gate| gate.take());
1847+
#[cfg(test)]
1848+
if let Some((hit, release)) = gate {
1849+
let _ = hit.send(());
1850+
let _ = release.await;
1851+
}
18091852
for request in requests {
18101853
let handle = Self::handle_from_request(&request.request_id, request.handle)?;
18111854
let value = self
@@ -1836,6 +1879,19 @@ impl CredentialDriver for TestStaticCredentialDriver {
18361879
.store(true, std::sync::atomic::Ordering::SeqCst);
18371880
}
18381881

1882+
#[cfg(test)]
1883+
fn gate_next_resolve(
1884+
&self,
1885+
) -> Option<(
1886+
tokio::sync::oneshot::Receiver<()>,
1887+
tokio::sync::oneshot::Sender<()>,
1888+
)> {
1889+
let (hit_tx, hit_rx) = tokio::sync::oneshot::channel();
1890+
let (release_tx, release_rx) = tokio::sync::oneshot::channel();
1891+
*self.resolve_gate.lock().ok()? = Some((hit_tx, release_rx));
1892+
Some((hit_rx, release_tx))
1893+
}
1894+
18391895
#[cfg(test)]
18401896
fn fail_next_delete(&self) {
18411897
self.fail_next_delete

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8078,6 +8078,10 @@ mod tests {
80788078
.await
80798079
.expect("store sandbox with invalid legacy spec");
80808080

8081+
backfill_legacy_policy_history(&state)
8082+
.await
8083+
.expect("global override must allow startup with an invalid dormant spec");
8084+
80818085
let response = handle_get_sandbox_config(
80828086
&state,
80838087
with_sandbox(

‎docs/reference/gateway-config.mdx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ Gateway CLI flag > gateway OPENSHELL_* env var > TOML file > built-in defa
2020

2121
`name` assigns an operator-facing identity to the gateway installation. Set it with `[openshell.gateway].name`, `--name`, or `OPENSHELL_GATEWAY_NAME`. It defaults to `openshell`; the Helm chart defaults it to the chart fullname so all replicas in one installation share a name. Chart fullnames are only unique within their Kubernetes namespace, so set `server.name` explicitly when one collector receives telemetry from multiple namespaces or clusters. This identity is independent of client-side gateway aliases, TLS names, and `gateway_jwt.gateway_id`.
2222

23+
## Supervisor connections and policy recovery
24+
25+
During supervisor reconnects, the gateway waits at most one second for the optional configuration bootstrap. If a credential backend stalls, the gateway accepts the session without that bootstrap so SSH, exec, and service connections can resume. Supervisor polling continues to supply configuration.
26+
27+
On startup, the gateway repairs missing legacy policy history and skips invalid stored policies. A valid global policy can still override an invalid local policy; otherwise, that sandbox's configuration reads continue to report the validation failure.
28+
2329
## Package-Managed Locations
2430

2531
Package-managed gateways do not require a TOML file. Create one at the package's optional config location when you need to override built-in defaults. Set `OPENSHELL_GATEWAY_CONFIG` in the launch environment to use a different file.

0 commit comments

Comments
 (0)