Skip to content

fix: handle stream end properly in readChunk to avoid hanging on deferred-length uploads(Fixes #779) #780

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

arnsen44
Copy link

@arnsen44 arnsen44 commented Jun 17, 2025

What this PR does
Fixes a bug where uploads using Readable streams with uploadLengthDeferred: true would hang if the total stream size is an exact multiple of chunkSize.

Related Issue
Fixes #779

Root cause
When using tus-js-client with:

uploadLengthDeferred: true

a stream source (Readable stream in Node.js)

and a file whose size is an exact multiple of the chunk size (e.g. 14 bytes file with 7-byte chunks),

the upload hangs after the last chunk and never completes.

When the file size is an exact multiple of the chunk size, readChunk() finishes reading the last full chunk and assumes more data might still come. But since there's no more data, only 'end' fires — not 'readable' — and the Promise never resolves.

In contrast:

When fileSize % chunkSize ≠ 0, the last read() returns a smaller chunk.

After this, the internal loop breaks as expected.

Changes
Adds an 'end' event listener in readChunk() to resolve the read with an empty buffer when stream ends without triggering 'readable'.

Ensures upload completes correctly for edge cases involving deferred length and chunk alignment.

Adds a test to cover this scenario.

✅ Testing
New test: ✅ should accept Readable streams with deferred size when chunk size divides the upload size

Existing tests: ✅ All pass
BrowserStack tests failed due to missing credentials in CI. All other tests are passing and this failure is unrelated to the code changes.

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
1 participant