Skip to content

Conversation

fenos
Copy link
Collaborator

@fenos fenos commented Jul 29, 2025

This PR fixes 2 regressions:

  • Handle gracefully the request cancellation by aborting the context, which will allow the locks to be released immediately and available to be acquired by subsequent requests. Currently, this has been broken since the context was never cancelled when the request was aborted

  • Force closing the connection when the request was aborted, to prevent the client from needing to send the whole body before realising that the upload was cancelled (potentially from another request acquiring the lock)

CC: @Murderlon

Copy link

changeset-bot bot commented Jul 29, 2025

🦋 Changeset detected

Latest commit: 8ab3a55

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 Jul 29, 2025

Walkthrough

A new changeset file was added to document a patch update for the @tus/server package. The Server class's handler method was enhanced to gracefully handle request cancellations and errors in the Node.js runtime by listening for 'error' events and setting the connection to close when aborted.

Changes

Cohort / File(s) Change Summary
Changeset Metadata
.changeset/modern-birds-appear.md
Added a changeset file documenting a patch update for @tus/server, describing improved request cancellation handling.
Server Request Handling
packages/server/src/server.ts
Updated the handler method to listen for 'error' events on Node.js requests, abort the request context on errors, and set the Connection header to 'close' if aborted before returning the response; minor comment clarification in listen method.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 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 6e5474b and 8ab3a55.

📒 Files selected for processing (1)
  • packages/server/src/server.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/server/src/server.ts
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/handle-request-abort-gracefully

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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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 changed the title fix: handle request cancellation gracefully @tus/server: handle request cancellation gracefully Jul 29, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/server/src/server.ts (1)

288-298: Good implementation of connection closing for aborted requests.

The method properly sets the Connection: close header when requests are aborted, which aligns with the PR objective to prevent clients from sending entire request bodies for cancelled uploads.

Consider simplifying the method by removing the redundant abort check since the caller already verifies this condition:

 protected handleAbortedRequest(context: CancellationContext, resp: Response) {
-  const isAborted = context.signal.aborted
-  if (isAborted) {
-    // If the request was aborted, we should not send any response body.
-    // The server should just close the connection.
-    resp.headers.set('Connection', 'close')
-    return resp
-  }
-
+  // If the request was aborted, we should not send any response body.
+  // The server should just close the connection.
+  resp.headers.set('Connection', 'close')
   return resp
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 75fc616 and 6e5474b.

📒 Files selected for processing (2)
  • .changeset/modern-birds-appear.md (1 hunks)
  • packages/server/src/server.ts (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/server/src/server.ts (1)
packages/utils/src/models/Context.ts (1)
  • CancellationContext (21-25)
🔇 Additional comments (3)
packages/server/src/server.ts (2)

137-146: LGTM! Excellent implementation of graceful request cancellation handling.

The Node.js runtime-specific error handling properly addresses the PR objectives by listening for error events (disconnects, timeouts) and immediately aborting the context. This ensures locks are released promptly for subsequent requests.


226-235: Well-implemented abort handling after request processing.

The implementation correctly checks the abort status after the handler completes and delegates to handleAbortedRequest when needed. This prevents sending response bodies for cancelled requests and ensures proper connection management.

.changeset/modern-birds-appear.md (1)

1-6: LGTM! Changeset documentation is accurate and appropriately scoped.

The changeset correctly identifies this as a patch-level change for @tus/server with a clear description that aligns with the implementation changes.

@Murderlon Murderlon merged commit 7b719ea into main Jul 29, 2025
4 checks passed
@Murderlon Murderlon deleted the fix/handle-request-abort-gracefully branch July 29, 2025 15:01
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.

2 participants