Skip to content

fix(voice-call): flush media-stream upgrade rejects before destroy - #141237

Open
hugenshen wants to merge 1 commit into
openclaw:mainfrom
hugenshen:fix/voice-call-media-stream-upgrade-flush
Open

hugenshen wants to merge 1 commit into
openclaw:mainfrom
hugenshen:fix/voice-call-media-stream-upgrade-flush

Conversation

@hugenshen

@hugenshen hugenshen commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Voice-call media-stream upgrade rejects (shutdown and connection-cap) could destroy the HTTP socket before rejection bytes flushed, so clients saw a hard disconnect instead of HTTP 503. Peer reset during that write could also become uncaughtException.

Why This Change Was Made

rejectMediaStreamUpgrade attaches socket.once("error", () => {}) then reuses rejectWebSocketUpgrade, which calls socket.end(response, () => destroy()). Cap and shutdown rejects share that owner.

User Impact

Before: Capacity/shutdown upgrade rejects could reset the client with no HTTP status, and a reset peer during write could crash the process.

After: Clients receive HTTP/1.1 503 with the reject body; peer-reset errors during flush are swallowed.

Evidence

  • Production: extensions/voice-call/src/media-stream.ts rejectMediaStreamUpgrade
  • Tests: extensions/voice-call/src/media-stream.test.ts — real HTTP 503, injected-hold flush on a real TCP socket, and RST during pending socket.end

Real behavior proof

  • Behavior or issue addressed: Media-stream upgrade rejects could drop the HTTP status on buffered/reset sockets.
  • Canonical reachability path: MediaStreamHandler.handleUpgrade → rejectMediaStreamUpgrade → rejectWebSocketUpgrade against a live loopback upgrade.
  • Boundary crossed: Real node:http / net.connect transport. Buffering is injected by holding socket.end on that real socket, then releasing so the 503 bytes flush. RST destroys the client only after the server has entered socket.end.
  • Shared helper / provider constraint check: Reused openclaw/plugin-sdk/websocket-runtime rejectWebSocketUpgrade.
  • Real environment tested: Local Vitest extension-voice-call lane, loopback TCP. No carrier/video.
  • Exact steps or command run after this patch:
$ git diff --check
$ node scripts/run-vitest.mjs extensions/voice-call/src/media-stream.test.ts --reporter=verbose -t "returns HTTP 503 over a real upgrade"
$ node scripts/run-vitest.mjs extensions/voice-call/src/media-stream.test.ts --reporter=verbose -t "flushes a buffered 503 over real transport"
$ node scripts/run-vitest.mjs extensions/voice-call/src/media-stream.test.ts --reporter=verbose -t "peer resets during a 503 reject"
  • Evidence after fix:
✓ |extension-voice-call| ... > returns HTTP 503 over a real upgrade when the connection cap is reached 13ms

stdout | ... > flushes a buffered 503 over real transport after an injected hold
[media-stream buffered transport proof] pending_before_release=true status=503 body=Too many media stream connections
 ✓ ... > flushes a buffered 503 over real transport after an injected hold 65ms

stdout | ... > does not surface uncaught errors when the peer resets during a 503 reject
[media-stream rst proof] rejection_started=true uncaught=0
 ✓ ... > does not surface uncaught errors when the peer resets during a 503 reject 65ms
  • Observed result after fix: Real transport returns HTTP 503. Holding socket.end keeps the reject pending until release, then the client reads the 503 body. Destroying the client at socket.end does not emit uncaughtException.

  • What was not tested: Live Twilio/Telnyx media-stream against a full carrier call.

  • Fix classification: Root cause fix

  • AI-assisted (Cursor)

  • I understand what the code does

  • Change is focused and does not mix unrelated concerns

@clawsweeper

clawsweeper Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@hugenshen
hugenshen marked this pull request as ready for review September 7, 2026 13:13
@openclaw-barnacle openclaw-barnacle Bot added channel: voice-call Channel integration: voice-call size: S labels Sep 7, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 7, 2026
@clawsweeper

clawsweeper Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

This PR flushes voice-call capacity and shutdown HTTP 503 responses before closing rejected WebSocket connections.

Example: A second media-stream connection arrives when maxConnections is 1 and one connection is already open.

  • Before: Immediate socket destruction can discard the HTTP rejection.
  • After: The client receives HTTP 503 and Too many media stream connections before closure.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) A focused, useful repair with sufficient bounded transport evidence and no introduced correctness findings; proof remains an in-process transport run.
Proof confidence 🦐 gold shrimp (3/6) Sufficient (logs): The supplied logs exercise the production MediaStreamHandler rejection owner with real Node HTTP/TCP transport and an injected write hold, then show HTTP 503 and the body after release; this satisfies the bounded transport-reliability proof path, although the disconnect case does not establish that ECONNRESET occurred.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Rejected voice-call media connections can close before clients receive their HTTP 503 response.
Reason: The change restores delivery of an existing rejection response without adding configuration, public API, or changing admission decisions.

Merge readiness

⛔ Needs changes before merge - 1 item remains

Keep this PR: current main retains the premature socket destruction, and the related merged fix covers a different rejection path. No introduced correctness defect was found; GitHub confirms a merge conflict.

Priority: P2
Reviewed head: 6f3ec86883e4c15d4c86d705a36865eb1ea4780a

Before merge

  • Complete next step - Resolve the conflict with current main, preserve its media-stream refactors, and validate the resulting rejection paths.

Findings

None.

Tests

  • Missing end-to-end proof: The supplied peer-disconnect run does not demonstrate an actual server socket error or a base-versus-head failure of the error guard.
Agent review details

How this fits together

The voice-call webhook server dispatches recognized media-stream upgrades to MediaStreamHandler, which accepts connections or returns an HTTP rejection through the shared SDK transport helper.

flowchart TD
A[Media stream client] --> B[Voice call webhook server]
B --> C[MediaStreamHandler]
C --> D[Accepted WebSocket]
C --> E[Capacity or shutdown rejection]
E --> F[Shared SDK rejection helper]
F --> G[Flush HTTP 503 then close]
Loading

Technical review

Best possible solution:

Retain the shared rejection helper and error ownership while resolving the conflict without reverting current-main media-stream refactors.

Do we have a high-confidence way to reproduce the issue?

Current-main source exposes the write-then-destroy path, and the contributor supplies after-fix loopback transport output; no target code was executed during this read-only review.

Is this the best way to solve the issue?

Reusing the existing SDK flush-before-destroy owner is a focused repair that preserves rejection status, bodies, admission limits, and successful upgrade handling.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 0ce08d269ff8.

Provenance checked

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

PR surface

Source +9, Tests +282. Total +291 across 3 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 1 25 16 +9
Tests 2 438 156 +282
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 463 172 +291

Labels

Label changes:

No label changes.

Label justifications:

  • P2: This repairs bounded voice-call rejection behavior during capacity exhaustion and shutdown.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Strengthen the error-guard regression by observing a server socket error during the pending rejection and showing that removing the guard fails the same case.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (10 earlier review cycles; latest 8 shown)
  • reviewed 2026-09-07T17:09:32.299Z sha 0358c59d1e589492bc1deafec000cf2643f713cd :: needs real behavior proof before merge. :: [P2] Wait for rejection handling before resetting the test client
  • reviewed 2026-09-07T17:18:29.742Z sha 0358c59d1e589492bc1deafec000cf2643f713cd :: needs real behavior proof before merge. :: [P2] Wait for rejection handling before resetting the test client
  • reviewed 2026-09-07T17:55:33.405Z sha a1f08f4e80a790301df2dd342aa9800c706f05af :: needs maintainer review before merge. :: none
  • reviewed 2026-09-15T01:54:41.763Z sha 6f3ec86 :: needs maintainer review before merge. :: none
  • reviewed September 23, 2026, 3:31 AM ET / 07:31 UTC (Revision 7) sha unknown :: needs maintainer review before merge. :: none
  • reviewed September 23, 2026, 3:31 AM ET / 07:31 UTC (Revision 8) sha unknown :: needs maintainer review before merge. :: none
  • reviewed September 23, 2026, 3:31 AM ET / 07:31 UTC (Revision 9) sha unknown :: needs maintainer review before merge. :: none
  • reviewed 2026-10-05T05:20:39.203Z sha 6f3ec86 :: needs changes before merge. :: none

Reviewed November 1, 2026, 6:32 PM ET / 23:32 UTC (Revision 11).

@hugenshen
hugenshen force-pushed the fix/voice-call-media-stream-upgrade-flush branch 2 times, most recently from c5411d5 to 0358c59 Compare September 7, 2026 17:03
@clawsweeper clawsweeper Bot removed the merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. label Sep 7, 2026
@hugenshen

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@hugenshen
hugenshen force-pushed the fix/voice-call-media-stream-upgrade-flush branch from 0358c59 to a1f08f4 Compare September 7, 2026 17:50
@hugenshen

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 7, 2026
@vincentkoc vincentkoc self-assigned this Sep 9, 2026
Reject media-stream WebSocket upgrades through shared rejectWebSocketUpgrade
so 503 bodies flush before socket destroy, and split oversized upgrade tests
into media-stream.upgrade.test.ts to satisfy oxlint max-lines.
@hugenshen
hugenshen force-pushed the fix/voice-call-media-stream-upgrade-flush branch from a1f08f4 to 6f3ec86 Compare September 15, 2026 01:48
@openclaw-barnacle

Copy link
Copy Markdown

This assigned pull request has been automatically marked as stale after being open for 27 days.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Oct 5, 2026
@hugenshen

Copy link
Copy Markdown
Contributor Author

@vincentkoc friendly bump — ClawSweeper has this at proof: sufficient / ready for maintainer look.

No open contributor proof blockers. Could you review/merge when you have bandwidth? Marked stale recently.

@openclaw-barnacle openclaw-barnacle Bot removed the stale Marked as stale due to inactivity label Oct 10, 2026
@openclaw-barnacle

Copy link
Copy Markdown

This assigned pull request has been automatically marked as stale after being open for 27 days.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Oct 10, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Nov 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: voice-call Channel integration: voice-call P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: L stale Marked as stale due to inactivity status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants