Repository navigation
fix: make worker backpressure cancellation-safe #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
| 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, | ||
|
|
@@ -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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, Send Prompt for AI agentsIn `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() { | ||
|
|
@@ -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)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Synchronize the test with the sender reaching the full-queue retry state before setting cancellation. Prompt for AI agentsIn `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()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Full-queue drops go undetected When 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 agentsIn `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"); | ||
|
|
||
There was a problem hiding this comment.
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_chunkkeeps retryingTrySendError::Full;Ingest::dropjoins the reader before droppingchunk_rx, so the reader cannot finish and the caller remains stuck instead of receiving the pipeline error.Set the cancellation flag before
Ingest::dropjoins the reader sosend_chunkcan stop retrying.Also at
src/worker.rs:474,src/worker.rs:517.Prompt for AI agents