Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,22 @@ impl Drop for Ingest {
}
}

fn send_chunk(tx: &SyncSender<Chunk>, mut chunk: Chunk, cancel: &AtomicBool) -> bool {
loop {
if cancel.load(Ordering::Relaxed) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Worker shutdown hangs after early pipeline errors

When the bounded chunk queue is full and the pipeline exits early without setting cancellation, send_chunk keeps retrying TrySendError::Full; Ingest::drop joins the reader before dropping chunk_rx, so the reader cannot finish and the caller remains stuck instead of receiving the pipeline error.

Set the cancellation flag before Ingest::drop joins the reader so send_chunk can stop retrying.

Also at src/worker.rs:474, src/worker.rs:517.

Prompt for AI agents
In `src/worker.rs` at line 467:

**Worker shutdown hangs after early pipeline errors**

When the bounded chunk queue is full and the pipeline exits early without setting cancellation, `send_chunk` keeps retrying `TrySendError::Full`; `Ingest::drop` joins the reader before dropping `chunk_rx`, so the reader cannot finish and the caller remains stuck instead of receiving the pipeline error.

Set the cancellation flag before `Ingest::drop` joins the reader so `send_chunk` can stop retrying.

Also at `src/worker.rs:474`, `src/worker.rs:517`.

return false;
}
match tx.try_send(chunk) {
Ok(()) => return true,
Err(std::sync::mpsc::TrySendError::Full(returned)) => {
chunk = returned;
thread::sleep(Duration::from_millis(10));
}
Err(std::sync::mpsc::TrySendError::Disconnected(_)) => return false,
}
}
}

fn spawn_ingest(
mut out: ChildStdout,
err: ChildStderr,
Expand Down Expand Up @@ -490,15 +506,18 @@ fn spawn_ingest(
Ok(n) => n,
Err(e) => {
if !cancel.load(Ordering::Relaxed) {
let _ = tx
.send(Chunk::Failed(format!("could not read decoded audio: {e}")));
let _ = send_chunk(
&tx,
Chunk::Failed(format!("could not read decoded audio: {e}")),
&cancel,
);
}
return total;
}
};
let samples = bytes_to_f32(&bytes[..n - n % 4]);
total += samples.len();
if tx.send(Chunk::Samples(samples)).is_err() {
if !send_chunk(&tx, Chunk::Samples(samples), &cancel) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Read errors can hang worker shutdown

When stdout or decoder reading fails while the bounded queue is full and cancellation races with the reader's cancellation check, Chunk::Failed still uses blocking tx.send, which cannot observe cancellation; Ingest::drop joins the reader while the receiver remains alive, so shutdown hangs.

Send Chunk::Failed through send_chunk with the cancellation flag instead of blocking on tx.send.

Prompt for AI agents
In `src/worker.rs` at line 517:

**Read errors can hang worker shutdown**

When stdout or decoder reading fails while the bounded queue is full and cancellation races with the reader's cancellation check, `Chunk::Failed` still uses blocking `tx.send`, which cannot observe cancellation; `Ingest::drop` joins the reader while the receiver remains alive, so shutdown hangs.

Send `Chunk::Failed` through `send_chunk` with the cancellation flag instead of blocking on `tx.send`.

return total;
}
if n < bytes.len() {
Expand Down Expand Up @@ -983,6 +1002,26 @@ mod tests {
drop(handle);
}

#[test]
fn bounded_ingest_send_observes_cancellation() {
let (tx, rx) = mpsc::sync_channel(1);
assert!(tx.try_send(Chunk::Samples(vec![0.0])).is_ok());
let cancel = Arc::new(AtomicBool::new(false));
let worker_cancel = cancel.clone();
let (done_tx, done_rx) = mpsc::channel();

let worker = thread::spawn(move || {
let sent = send_chunk(&tx, Chunk::Samples(vec![1.0]), &worker_cancel);
done_tx.send(sent).unwrap();
});

thread::sleep(Duration::from_millis(30));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Cancellation test can skip backpressure

When the worker is not scheduled until after the 30 ms sleep, the worker can be descheduled before entering send_chunk, so the test sets cancellation before the sender observes the full queue. The initial cancellation check then returns false, letting the test pass even if cancellation during a blocked send is broken.

Synchronize the test with the sender reaching the full-queue retry state before setting cancellation.

Prompt for AI agents
In `src/worker.rs` at line 1015:

**Cancellation test can skip backpressure**

When the worker is not scheduled until after the 30 ms sleep, the worker can be descheduled before entering `send_chunk`, so the test sets cancellation before the sender observes the full queue. The initial cancellation check then returns `false`, letting the test pass even if cancellation during a blocked send is broken.

Synchronize the test with the sender reaching the full-queue retry state before setting cancellation.

cancel.store(true, Ordering::Relaxed);
assert!(!done_rx.recv_timeout(Duration::from_secs(1)).unwrap());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Full-queue drops go undetected

When send_chunk returns false immediately on TrySendError::Full, bounded_ingest_send_observes_cancellation still passes if send_chunk returns false immediately on a full queue: line 1011 records that result, and line 1017 only asserts it is false after cancellation. The test therefore does not establish that the sender waits for cancellation rather than dropping the chunk.

Make the test verify that the sender remains pending while the queue is full before setting cancellation, then verify that cancellation releases it.

Prompt for AI agents
In `src/worker.rs` at line 1017:

**Full-queue drops go undetected**

When `send_chunk` returns false immediately on `TrySendError::Full`, `bounded_ingest_send_observes_cancellation` still passes if `send_chunk` returns false immediately on a full queue: line 1011 records that result, and line 1017 only asserts it is false after cancellation. The test therefore does not establish that the sender waits for cancellation rather than dropping the chunk.

Make the test verify that the sender remains pending while the queue is full before setting cancellation, then verify that cancellation releases it.

worker.join().unwrap();
assert!(matches!(rx.try_recv(), Ok(Chunk::Samples(_))));
}

#[cfg(unix)]
fn fake_decoder(script: &str) -> Command {
let mut command = Command::new("sh");
Expand Down