Skip to content

fix: make worker backpressure cancellation-safe - #11

Merged
programmersd21 merged 2 commits into
mainfrom
fix/cancellable-worker-backpressure
Nov 28, 2026
Merged

programmersd21 merged 2 commits into
mainfrom
fix/cancellable-worker-backpressure

Conversation

@programmersd21

@programmersd21 programmersd21 commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • replace blocking bounded-channel sends in the decoder reader with cancellation-aware backpressure
  • keep the reader responsive when the inference consumer stops draining a full queue
  • add a regression test that fills the queue and verifies cancellation releases the sender

Why

A reader blocked in SyncSender::send cannot observe cancellation. The worker then joins that reader during cleanup while the receiver is still alive, which can deadlock shutdown.

Verification

  • CI will run formatting, Clippy, tests and MSRV checks

Summary by Sourcery

Make worker backpressure cancellation-safe so blocked ingestion sends can exit promptly during shutdown.

Bug Fixes:

  • Make bounded worker output delivery observe cancellation so shutdown cannot deadlock when the consumer stops draining a full queue.

Tests:

  • Add a regression test confirming a blocked bounded send exits after cancellation.

@sourcery-ai

sourcery-ai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Replace blocking bounded-channel sends in decoder ingestion with cancellation-aware retries, including decoder read-error reporting, so shutdown can join the reader without deadlocking.

  • src/worker.rs L465-480, L509-513, L520

Tests

Add a regression test that fills the bounded queue and verifies cancellation releases the blocked sender without consuming the queued chunk.

  • src/worker.rs L1005-1024

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 50d6279f-46c4-4213-83a8-a6d09985b391
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sourcery assessment

Approved.

@programmersd21
programmersd21 merged commit ed21396 into main Nov 28, 2026
5 checks passed

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/worker.rs

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`.

Comment thread src/worker.rs
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`.

Comment thread src/worker.rs
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.

Comment thread src/worker.rs

thread::sleep(Duration::from_millis(30));
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.

@programmersd21
programmersd21 deleted the fix/cancellable-worker-backpressure branch November 28, 2026 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant