Skip to content

@tus/server: fix promise rejection for stream conversion #753

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

Merged
merged 2 commits into from
Apr 14, 2025

Conversation

Murderlon
Copy link
Member

Fixes #747

@Murderlon Murderlon self-assigned this Apr 14, 2025
Copy link

changeset-bot bot commented Apr 14, 2025

🦋 Changeset detected

Latest commit: b6281a3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tus/server Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

coderabbitai bot commented Apr 14, 2025

Walkthrough

This pull request introduces changes to the @tus/server package to improve error handling during client disconnections. The modifications focus on converting a web stream to a Node.js stream without triggering unhandled promise rejections. The writeToStore method in the BaseHandler now accepts a ReadableStream | null and manages errors by attaching an error listener, while both the PatchHandler and PostHandler simplify how the request body is passed to writeToStore by removing conditional stream conversions.

Changes

Files Change Summary
.changeset/dull-fans-drop.md Documented patch for the @tus/server package addressing unhandled promise rejection during client disconnection.
packages/.../handlers/BaseHandler.ts Updated writeToStore: changed parameter from Readable to `ReadableStream
packages/.../handlers/PatchHandler.ts, packages/.../handlers/PostHandler.ts Simplified stream handling by directly passing req.body to writeToStore, removing conditional logic for converting streams.

Assessment against linked issues

Objective Addressed Explanation
Handle connection loss without crashing (ECONNRESET) [#747]

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e3a3491 and b6281a3.

📒 Files selected for processing (4)
  • .changeset/dull-fans-drop.md (1 hunks)
  • packages/server/src/handlers/BaseHandler.ts (5 hunks)
  • packages/server/src/handlers/PatchHandler.ts (1 hunks)
  • packages/server/src/handlers/PostHandler.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/server/src/handlers/PostHandler.ts (1)
packages/gcs-store/src/index.ts (1)
  • upload (172-180)
🔇 Additional comments (10)
.changeset/dull-fans-drop.md (1)

1-6: Descriptive changeset for the fix.

The changeset clearly documents the purpose of the patch: fixing the unhandled promise rejection that occurs when converting a web stream to a Node.js stream during client disconnections. This description aligns well with the code changes implemented in the PR.

packages/server/src/handlers/PatchHandler.ts (1)

99-99: Simplified request body handling.

The code has been simplified to directly pass req.body to the writeToStore method instead of conditionally creating a readable stream. This change aligns with the updated writeToStore method signature in BaseHandler.ts that now accepts a ReadableStream | null parameter, improving code consistency and simplifying the stream handling.

packages/server/src/handlers/PostHandler.ts (1)

132-132: Simplified request body handling.

The code has been simplified to directly pass req.body to the writeToStore method instead of conditionally creating a readable stream. This change aligns with the updated writeToStore method signature in BaseHandler.ts that now accepts a ReadableStream | null parameter, improving code consistency and simplifying the stream handling.

packages/server/src/handlers/BaseHandler.ts (7)

8-8: Import organization updated.

The import statement has been modified to import both PassThrough and Readable from 'node:stream' in a single import statement, which is a clean approach.


130-131: Improved parameter type for better error handling.

Changed parameter from data: Readable to webStream: ReadableStream | null, which better represents the incoming data type and allows for more explicit handling of stream conversion.


146-152: Key fix: Added error handling for stream conversion.

This is the core fix for the unhandled promise rejection issue. The code now:

  1. Creates a Node.js readable stream from the web stream, or an empty stream if null
  2. Adds an error event listener that silently ignores errors on the stream itself

This prevents unhandled promise rejections when clients disconnect during uploads, as the stream errors are now properly caught and ignored, while the proxy stream still handles errors appropriately.


156-157: Updated stream unpipe to use nodeStream.

The code has been updated to unpipe the nodeStream from the proxy when the request is aborted, which aligns with the new stream handling approach.


165-166: Updated error handling to use nodeStream.

The error handler for the proxy has been updated to unpipe nodeStream from the proxy, consistent with the new stream handling approach.


171-171: Updated event emission to use nodeStream.

The POST_RECEIVE event emission has been updated to use nodeStream instead of the previous data parameter, maintaining consistency with the new stream handling.


187-193: Updated stream pipeline to use nodeStream.

The stream pipeline has been updated to pipe nodeStream through the proxy and into the StreamLimiter, which completes the consistent use of the new stream handling approach throughout the method.

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Murderlon Murderlon merged commit aa1e221 into main Apr 14, 2025
4 checks passed
@Murderlon Murderlon deleted the readable-promise-rejection branch April 14, 2025 10:02
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.

Documentation exemple exit with ECONNRESET
1 participant