Skip to content

[Fix] 020-twilio-media-streams-node — fix async race dropping Twilio audio#97

Closed
github-actions[bot] wants to merge 1 commit intomainfrom
fix/020-twilio-media-streams-node-regression-2026-04-01
Closed

[Fix] 020-twilio-media-streams-node — fix async race dropping Twilio audio#97
github-actions[bot] wants to merge 1 commit intomainfrom
fix/020-twilio-media-streams-node-regression-2026-04-01

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 1, 2026

Summary

  • Root cause: express-ws does not await async WebSocket handlers. The original async (twilioWs) => { ... } handler awaited deepgram.listen.v1.createConnection() and waitForOpen() before registering twilioWs.on('message'), so early Twilio audio frames were silently dropped — no audio reached Deepgram, causing test timeouts.
  • Made the /media WS handler synchronous; Twilio event listeners register immediately
  • Deepgram connection setup runs in an async IIFE; incoming media is buffered in mediaQueue until the Deepgram socket is open, then flushed
  • Test now closes the WebSocket after sending stop (matches real Twilio behavior)

Test plan

  • CI runs npm test on this example with live Deepgram credentials
  • POST /voice returns TwiML with correct <Stream> URL
  • WebSocket pipeline streams audio → Deepgram returns transcripts containing expected words

🤖 Generated with Claude Code

…-media-streams-node

express-ws does not await async WebSocket handlers, so Twilio message
listeners were registered after the Deepgram connection was established,
causing early audio frames to be silently dropped. Moved Twilio WS
handlers before async Deepgram setup and buffer media until ready.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor Author

github-actions bot commented Apr 1, 2026

Code Review

Overall: CHANGES REQUESTED

Integration genuineness

N/A — bug fix PR.

Code quality

The fix correctly addresses the root cause (async handler race condition) with the same buffering pattern. However:

  • Still uses createConnection() instead of the current listen.v1.connect() API
  • Does not update TwiML generation to use the Twilio SDK
  • Still uses deprecated sendFinalize in the stop and close handlers

PR #95 addresses all of these and is the most complete fix for this issue.

Recommendation

Please close this PR in favor of #95.


Please address the items above. The fix agent will pick this up automatically.


Review by Lead on 2026-04-01

@github-actions github-actions bot added the status:fix-needed Tests failing — fix agent queued label Apr 1, 2026
@github-actions
Copy link
Copy Markdown
Contributor Author

github-actions bot commented Apr 1, 2026

Closing in favor of #95

Per review feedback, PR #95 is the most complete fix for this issue — addresses the async race condition, updates to SDK v5 API (listen.v1.connect(), sendCloseStream), and uses the Twilio TwiML builder. Closing this duplicate.


Fix by Lead on 2026-04-01

@github-actions github-actions bot closed this Apr 1, 2026
@github-actions github-actions bot removed the status:fix-needed Tests failing — fix agent queued label Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants