broker: Never block reading a requester's stdin #13

Merged
dev merged 1 commits from fix-stdin-blocking into main 2026-11-28 18:09:18 +00:00
4 changed files with 50 additions and 6 deletions

No files matched your search

+2 -1
View File
@@ -102,7 +102,8 @@ and `slop-approve`. How the code does it is in
- **One connection per request**, carrying in order:
1. the request;
2. the operation's stdin, only after approval and only for operations that read it (`ssh`
carries its stdin in the request instead, below);
carries its stdin in the request instead, below). The broker drops stdin an operation
doesn't read, and treats a requester sending more than 1 MiB as gone;
3. its stdout and stderr;
4. a final message with the exit status and any structured result.
+1 -1
View File
@@ -32,7 +32,7 @@ impl Output {
pub struct Io {
/// Chunks of the requester's stdin, which it sends after approval for operations that ask for
/// it; closed at EOF.
pub stdin: mpsc::Receiver<Vec<u8>>,
pub stdin: mpsc::UnboundedReceiver<Vec<u8>>,
pub out: Output,
/// Cancelled when the requester disconnects.
pub cancel: CancellationToken,
+19 -4
View File
@@ -23,6 +23,10 @@ use crate::proto::{ApprovedBy, ClientMessage, PROTOCOL_VERSION, Request, ServerM
use crate::queue::{NewRequest, Outcome, Queue, new_id};
use crate::targets::Targets;
/// The most stdin a requester may send. Operations that read it have their own, smaller limits;
/// a requester sending more than this is treated as gone.
const MAX_STDIN: usize = 1 << 20;
pub struct Broker {
pub config: Config,
pub queue: Queue,
@@ -123,18 +127,25 @@ pub async fn handle<S: AsyncRead + AsyncWrite + Send + 'static>(broker: Arc<Brok
};
// After the request, the requester only sends stdin; anything else, or the connection
// closing, means it's gone.
// closing, means it's gone. The reader never waits on the operation, so it always notices:
// stdin is buffered up to a limit, and dropped once the operation can't take it.
let cancel = CancellationToken::new();
let (stdin_tx, stdin_rx) = mpsc::channel(16);
let (stdin_tx, stdin_rx) = mpsc::unbounded_channel();
let reader = {
let cancel = cancel.clone();
tokio::spawn(async move {
let mut stdin_tx = Some(stdin_tx);
let (mut stdin_tx, mut total) = (Some(stdin_tx), 0);
while let Some(Ok(frame)) = source.next().await {
match decode::<ClientMessage>(&frame) {
Ok(ClientMessage::Stdin(data)) => {
if let Some(tx) = &stdin_tx {
let _ = tx.send(data).await;
total += data.len();
if total > MAX_STDIN {
break;
}
if tx.send(data).is_err() {
stdin_tx = None;
}
}
}
Ok(ClientMessage::StdinEnd) => stdin_tx = None,
@@ -182,6 +193,10 @@ async fn process(broker: &Broker, requester: &str, request: Request, io: &mut Io
}
};
if !parsed.stdin {
io.stdin.close();
}
// Built-in operations only read the broker's own state, so they aren't logged.
let builtin = parsed.op.builtin();
let log = |event| {
+28
View File
@@ -362,6 +362,34 @@ async fn disconnecting_kills_the_process() {
assert!(!alive, "process {pid} survived its requester");
}
#[tokio::test]
async fn stdin_an_operation_doesnt_read_doesnt_hide_a_disconnect() {
let directory = tempfile::tempdir().unwrap();
let broker = broker(directory.path(), json!({ "ssh": { "action": "auto" } }));
let mut client = Client::start(&broker, ssh_with(&["true"], Some(b"block\n"))).await;
client.next(&mut String::new()).await;
for _ in 0..64 {
client.send(ClientMessage::Stdin(b"ignored\n".to_vec())).await;
}
let pid_file = directory.path().join("pid");
let mut pid = None;
for _ in 0..50 {
tokio::time::sleep(Duration::from_millis(50)).await;
pid = std::fs::read_to_string(&pid_file).ok().and_then(|p| p.trim().parse::<i32>().ok());
if pid.is_some() {
break;
}
}
let pid = pid.expect("fake ssh started");
drop(client);
tokio::time::sleep(Duration::from_millis(500)).await;
let alive = kill(Pid::from_raw(pid), None).is_ok();
assert!(!alive, "process {pid} survived its requester");
}
#[tokio::test]
async fn queries_run_a_fixed_quoted_command_without_stdin() {
let directory = tempfile::tempdir().unwrap();