Repository navigation
fix: make worker backpressure cancellation-safe - #11
Conversation
Reviewer's GuideImplementationReplace blocking bounded-channel sends in decoder ingestion with cancellation-aware retries, including decoder read-error reporting, so shutdown can join the reader without deadlocking.
TestsAdd a regression test that fills the bounded queue and verifies cancellation releases the blocked sender without consuming the queued chunk.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/worker.rs" line_range="467" />
<code_context>
+fn send_chunk(tx: &SyncSender<Chunk>, mut chunk: Chunk, cancel: &AtomicBool) -> bool {
+ loop {
+ if cancel.load(Ordering::Relaxed) {
+ return false;
+ }
</code_context>
<issue_to_address>
**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`.
</issue_to_address>
### Comment 2
<location path="src/worker.rs" line_range="517" />
<code_context>
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) {
return total;
}
</code_context>
<issue_to_address>
**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`.
</issue_to_address>
### Comment 3
<location path="src/worker.rs" line_range="1015" />
<code_context>
+ done_tx.send(sent).unwrap();
+ });
+
+ thread::sleep(Duration::from_millis(30));
+ cancel.store(true, Ordering::Relaxed);
+ assert!(!done_rx.recv_timeout(Duration::from_secs(1)).unwrap());
</code_context>
<issue_to_address>
**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.
</issue_to_address>
### Comment 4
<location path="src/worker.rs" line_range="1017" />
<code_context>
+
+ thread::sleep(Duration::from_millis(30));
+ cancel.store(true, Ordering::Relaxed);
+ assert!(!done_rx.recv_timeout(Duration::from_secs(1)).unwrap());
+ worker.join().unwrap();
+ assert!(matches!(rx.try_recv(), Ok(Chunk::Samples(_))));
</code_context>
<issue_to_address>
**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.
</issue_to_address>Sourcery assessment
Approval pending. 4 findings to address first.
Blocking findings: src/worker.rs:467, src/worker.rs:517, src/worker.rs:1015, src/worker.rs:1017
|
|
||
| fn send_chunk(tx: &SyncSender<Chunk>, mut chunk: Chunk, cancel: &AtomicBool) -> bool { | ||
| loop { | ||
| if cancel.load(Ordering::Relaxed) { |
There was a problem hiding this comment.
🟡 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`.| 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.
🟡 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`.| done_tx.send(sent).unwrap(); | ||
| }); | ||
|
|
||
| thread::sleep(Duration::from_millis(30)); |
There was a problem hiding this comment.
🟡 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.|
|
||
| thread::sleep(Duration::from_millis(30)); | ||
| cancel.store(true, Ordering::Relaxed); | ||
| assert!(!done_rx.recv_timeout(Duration::from_secs(1)).unwrap()); |
There was a problem hiding this comment.
🟡 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.
Summary
Why
A reader blocked in
SyncSender::sendcannot observe cancellation. The worker then joins that reader during cleanup while the receiver is still alive, which can deadlock shutdown.Verification
Summary by Sourcery
Make worker backpressure cancellation-safe so blocked ingestion sends can exit promptly during shutdown.
Bug Fixes:
Tests: