Skip to content

fix(ws): handle response stream errors on failed WS upgrade#116

Merged
pi0 merged 1 commit intomainfrom
fix/ws-upgrade-socket-error
Mar 25, 2026
Merged

fix(ws): handle response stream errors on failed WS upgrade#116
pi0 merged 1 commit intomainfrom
fix/ws-upgrade-socket-error

Conversation

@pi0
Copy link
Copy Markdown
Member

@pi0 pi0 commented Mar 25, 2026

Summary

  • Add res.on("error", onOutgoingError) before res.pipe(socket) in both ws-incoming.ts (ProxyServer) and ws.ts (proxyUpgrade) to catch upstream response stream errors during failed WebSocket upgrades
  • Guard against writing to already-destroyed sockets with !socket.destroyed && socket.writable checks
  • Separate the settled rejection logic from the writable guard in proxyUpgrade so the promise always rejects on non-upgrade responses

Fixes the crash described in upstream http-party/node-http-proxy#1439: when the target returns a non-upgrade HTTP response (e.g., 502) and the connection resets mid-body, the unhandled error event on the response stream crashes the process.

Test plan

  • Added regression test in test/ws.test.ts — proxyUpgrade with upstream that destroys socket mid-response
  • Added regression test in test/http-proxy.test.ts — ProxyServer.ws() with same scenario
  • Added test/ws-destroyed-socket.test.ts — tests for writing to destroyed sockets before upstream responds
  • Full test suite passes (2520 tests, 0 failures)

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 25, 2026

Warning

Rate limit exceeded

@pi0 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 26 minutes and 35 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 32c18bcb-1db0-40bb-a544-a5bb12901ca0

📥 Commits

Reviewing files that changed from the base of the PR and between b7075ab and 23cafec.

📒 Files selected for processing (5)
  • src/middleware/ws-incoming.ts
  • src/ws.ts
  • test/http-proxy.test.ts
  • test/ws-destroyed-socket.test.ts
  • test/ws.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/ws-upgrade-socket-error

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.12%. Comparing base (b7075ab) to head (23cafec).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #116   +/-   ##
=======================================
  Coverage   96.11%   96.12%           
=======================================
  Files           8        8           
  Lines         644      645    +1     
  Branches      244      243    -1     
=======================================
+ Hits          619      620    +1     
  Misses         24       24           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

When a WebSocket upgrade request hits a target that responds with a
non-upgrade HTTP response (e.g., 502) and the upstream connection resets
mid-body, the `res` stream emits an unhandled error that crashes the
process. Add `res.on("error", onOutgoingError)` before piping, and guard
against writing to already-destroyed sockets.

Ref: http-party/node-http-proxy#1439
@pi0 pi0 force-pushed the fix/ws-upgrade-socket-error branch from 7f63ba2 to 23cafec Compare March 25, 2026 20:06
@pi0 pi0 merged commit f30f90f into main Mar 25, 2026
7 checks passed
@pi0 pi0 deleted the fix/ws-upgrade-socket-error branch March 25, 2026 20:08
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.

1 participant