Skip to content

fix: upload stalls with deferred length when size is multiple of chunkSize - #866

Open
Yanhu007 wants to merge 1 commit into
tus:mainfrom
Yanhu007:fix/deferred-length-stall
Open

fix: upload stalls with deferred length when size is multiple of chunkSize#866
Yanhu007 wants to merge 1 commit into
tus:mainfrom
Yanhu007:fix/deferred-length-stall

Conversation

@Yanhu007

Copy link
Copy Markdown

Fixes #779

Problem

When using uploadLengthDeferred: true with a Node.js stream whose total size is exactly divisible by chunkSize, the upload hangs forever after the last chunk.

Root cause: After consuming the last aligned chunk, readChunk() is called again. The stream has no more data and emits end, but readChunk only listens for readable events — the promise never resolves.

Timeline

  1. slice(0, 7) → reads 7 bytes, done: false
  2. slice(7, 14) → reads 7 bytes, stream buffer depleted, done: false
  3. slice(14, 21) → buffer empty, readChunk() called → hangs (stream emits end, not readable)

Fix

  • Add end event listener in readChunk() that resolves with an empty buffer
  • Add readableEnded guard at entry for already-ended streams
  • Clean up all three listeners (error, readable, end) on resolution

…kSize

When using uploadLengthDeferred with a Node.js stream whose total
size is exactly divisible by chunkSize, readChunk() hangs forever.

After the last aligned chunk is consumed, readChunk is called again
but the stream has no more data. It only listens for 'readable'
events, but the stream emits 'end' instead. Since there is no
'end' listener, the returned promise never resolves and the upload
stalls silently.

Fix:
- Listen for 'end' in readChunk and resolve with an empty buffer
- Add a readableEnded guard at the top for already-ended streams
- Clean up all listeners on resolution to prevent leaks

Fixes tus#779
@Acconut

Acconut commented Apr 12, 2026

Copy link
Copy Markdown
Member

Thank you for this report and providing a fix as well. Could you add a unit test covering this case so we'll catch any future regression?

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.

Upload stalls when using deferred length and stream size is exactly divisible by chunkSize

2 participants