Bound exec-server process event reordering (#31576)

## Why

A malicious exec-server can send out-of-order process notifications
faster than missing events arrive. The client retained every future
event in a per-session reorder map, so many tiny events or a few large
output chunks could grow orchestrator memory without bound.

## What changed

- cap each session's pending process-event reorder state at 256 events
and 1 MiB
- reject individual process events larger than 1 MiB
- release byte accounting as events publish or a session fails
- fail and detach only the affected process session when a limit is
exceeded
- let the next expected event drain a full buffer, so the limits apply
to retained future state
- apply the same bounded insertion path during reconnect recovery while
safely handling dense tail output, missing exit events, newer live
notifications, and conflicting `Closed` sequences

Sequence distance is intentionally not capped because it does not affect
allocation; the event-count and byte limits are the resource-exhaustion
boundary.

## Tests

Focused exec-server tests cover oversized output, full count/byte
buffers, gap-closing delivery, dense recovery interleaved with newer
notifications, missing exit reconstruction, and conflicting terminal
sequences.

## Scope

This is limited to per-session process-event reorder state. It does not
introduce a scheduler, quota framework, or new public configuration.
This commit is contained in:
jif authored and GitHub committed 2026-07-09 09:56:14 +01:00
1 parent e8eb60092a
commit e398a99edf
3 files changed
+302 -58

No files matched your search

@@ -1,5 +1,7 @@
use std::time::Duration;
use pretty_assertions::assert_eq;
use super::*;
use crate::protocol::ExecOutputStream;
use crate::protocol::ProcessOutputChunk;
@@ -57,14 +59,152 @@ fn recovery_does_not_retry_other_registry_conflicts() {
assert!(!is_retryable_recovery_error(&error));
}
#[test]
fn process_event_reorder_rejects_oversized_output() {
let state = SessionState::new(/*recoverable*/ true);
let error = state
.publish_ordered_event(ExecProcessEvent::Output(ProcessOutputChunk {
seq: 1,
stream: ExecOutputStream::Stdout,
chunk: vec![0; super::super::MAX_PENDING_PROCESS_EVENT_BYTES + 1].into(),
}))
.expect_err("oversized pending process output should be rejected");
assert!(error.contains("bytes"));
}
#[test]
fn process_event_reorder_accepts_gap_closing_event_at_limits() {
let state = SessionState::new(/*recoverable*/ true);
let chunk_size =
super::super::MAX_PENDING_PROCESS_EVENT_BYTES / super::super::MAX_PENDING_PROCESS_EVENTS;
let last_seq = super::super::MAX_PENDING_PROCESS_EVENTS as u64 + 1;
for seq in 2..=last_seq {
assert!(
!state
.publish_ordered_event(ExecProcessEvent::Output(ProcessOutputChunk {
seq,
stream: ExecOutputStream::Stdout,
chunk: vec![0; chunk_size].into(),
}))
.expect("future output should fit within reorder limits")
);
}
assert!(
!state
.publish_ordered_event(ExecProcessEvent::Output(ProcessOutputChunk {
seq: 1,
stream: ExecOutputStream::Stdout,
chunk: b"x".to_vec().into(),
}))
.expect("gap-closing output should drain the reorder buffer")
);
let ordered_events = state
.ordered_events
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
assert_eq!(
(
ordered_events.last_published_seq,
ordered_events.pending.len(),
ordered_events.pending_bytes,
),
(last_seq, 0, 0)
);
}
#[test]
fn recovery_handles_dense_tail_output_and_newer_notification() {
let state = SessionState::new(/*recoverable*/ true);
let last_seq = super::super::MAX_PENDING_PROCESS_EVENTS as u64 + 2;
let live_seq = last_seq + 1;
assert!(
!state
.publish_ordered_event(ExecProcessEvent::Output(ProcessOutputChunk {
seq: live_seq,
stream: ExecOutputStream::Stdout,
chunk: b"live".to_vec().into(),
}))
.expect("live output should remain bounded while recovery fills the gap")
);
let chunks = (2..=last_seq)
.map(|seq| ProcessOutputChunk {
seq,
stream: ExecOutputStream::Stdout,
chunk: b"x".to_vec().into(),
})
.collect();
assert!(
!state
.recover_events(ReadResponse {
chunks,
next_seq: last_seq + 1,
exited: true,
exit_code: Some(17),
closed: false,
failure: None,
sandbox_denied: false,
})
.expect("dense retained output should recover")
);
let ordered_events = state
.ordered_events
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
assert_eq!(
(
ordered_events.last_published_seq,
ordered_events.pending.len(),
ordered_events.pending_bytes,
),
(live_seq, 0, 0)
);
}
#[test]
fn recovery_rejects_output_at_closed_sequence() {
let state = SessionState::new(/*recoverable*/ true);
let error = state
.recover_events(ReadResponse {
chunks: vec![ProcessOutputChunk {
seq: 1,
stream: ExecOutputStream::Stdout,
chunk: b"output".to_vec().into(),
}],
next_seq: 2,
exited: false,
exit_code: None,
closed: true,
failure: None,
sandbox_denied: false,
})
.expect_err("output should not occupy the closed sequence");
assert!(
error
.to_string()
.contains("conflicts with recovered output")
);
}
#[tokio::test]
async fn recovery_adds_sandbox_denial_to_pending_exit_event() {
let state = SessionState::new(/*recoverable*/ true);
assert!(!state.publish_ordered_event(ExecProcessEvent::Exited {
seq: 2,
exit_code: 1,
sandbox_denied: None,
}));
assert!(
!state
.publish_ordered_event(ExecProcessEvent::Exited {
seq: 2,
exit_code: 1,
sandbox_denied: None,
})
.expect("pending exit should fit within reorder limits")
);
state
.recover_events(ReadResponse {