Skip to content

Commit

Permalink
fix: avoid possible panic when verifying
Browse files Browse the repository at this point in the history
The chunk iterator ends when either the channel is closed, or a slot is left
unfinished. Unfortunately, the reader was _always_ leaving a slot unfinished:
after the final block was read, the reader would get another slot, read zero
bytes to see the EOF, and drop the slot without filling it. This meant the
writer would see the chunk iterator end when the slot was dropped, which meant
the reader thread may not have gotten around to dropping the original file: the
writer thread was assuming a finished iterator meant the channel was closed,
but this was not the case.

So instead, check if we should expect we read the final block, and don't
reserve a slot in that case to check if there's no more to read.

There's a more complete fix coming, but this will fix most realistic errors.
  • Loading branch information
Dr-Emann committed Jan 1, 2025
1 parent 053bfbf commit 9a61172
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/applesauce/src/threads/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ impl Handler {
loop {
let _enter = block_span.enter();

// make sure we don't reserve a slot if we won't be sending a chunk
if total_read == expected_len {
let mut buf = [0];
let n = try_read_all(file, &mut buf)?;
total_read += u64::try_from(n).unwrap();
// Outside the loop, we'll error if we read more than expected_len
break;
}

let slot = {
let _enter = tracing::debug_span!("waiting for free slot").entered();
match tx.prepare_send() {
Expand Down

0 comments on commit 9a61172

Please sign in to comment.