Skip to content

fix(realtime): reject connect() promise when WebSocket errors before … - #186

Merged
ccampbell-aai merged 3 commits into
AssemblyAI:mainfrom
shobhitagnihotri69:fix/136-connect-rejects-on-error
Sep 21, 2026
Merged

ccampbell-aai merged 3 commits into
AssemblyAI:mainfrom
shobhitagnihotri69:fix/136-connect-rejects-on-error

Conversation

@shobhitagnihotri69

Copy link
Copy Markdown
Contributor

…### Summary

In RealtimeTranscriber.connect() (src/services/realtime/service.ts), the connection Promise executor only captured resolve and omitted reject.

If a connection failed before the server emitted SessionBegins (such as on network disconnect, DNS resolution failure, or server rejection), the underlying WebSocket emitted an error event which notified event listeners, but the connect() promise was left in a pending state forever. Callers awaiting connect() would hang indefinitely without entering their catch block.

Changes

  • Added a private connectReject?: (reason?: unknown) => void field on RealtimeTranscriber.
  • In connect(), captured the rejection handler alongside resolve.
  • In the pre-handshake socket error event handler, if connectReject is set, invoke it and clear the reference.
  • In the SessionBegins message handler, clear connectReject so subsequent runtime errors after handshake only emit to event listeners and do not attempt to settle the already-resolved promise.
  • Added unit tests:
    • Verifies connect() rejects with the error when WebSocket emits error prior to SessionBegins.
    • Verifies an already-resolved connect() does not reject on subsequent post-handshake socket errors.

Verification

  • Ran full test suite (npm test). All 26 tests passed.

@ccampbell-aai ccampbell-aai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey @shobhitagnihotri69 thanks for digging into this! This is a valid fix, but there a couple changes noted inline. Plus, please include a version bump and addition to the change log as seen in other closed PRs.

Please note that this fix is for a legacy client for an old (v2) api path.

Comment thread src/services/realtime/service.ts Outdated
Comment thread src/services/realtime/service.ts
@shobhitagnihotri69

Copy link
Copy Markdown
Contributor Author

Hi @ccampbell-aai Can you please look into this updates changes , I have tried to fix all the changes

…SessionBegins

Fixes AssemblyAI#136.

RealtimeTranscriber.connect() returned a Promise that only captured
resolve, not reject. When the underlying WebSocket emitted an onerror
event before the server sent a SessionBegins message (e.g. connection
refused, auth failure, network drop), the error was forwarded to any
registered error listener but the connect() promise was left in the
pending state forever. Callers awaiting connect() would hang
indefinitely.

Changes:
- Add a private  field to hold the rejection handler
  from the connect() Promise.
- In , call  if it is set (i.e. SessionBegins
  has not yet been received), then clear the field.
- In the SessionBegins handler, clear  so that any
  subsequent socket errors after a successful handshake only propagate
  to the error listener, not to the already-resolved connect() promise.

Tests:
- rejects connect() when WebSocket errors before SessionBegins
- does not reject an already-resolved connect() on subsequent errors
All 26 tests pass.
…e 136 reference

- Implement discardPendingSocket() on RealtimeTranscriber and invoke it when
  pre-session connection fails (in onerror and onclose) so that callers can
  safely retry connect() without encountering 'Already connected'.
- Remove references claiming to fix AssemblyAI#136 in comments and tests.
- Add unit test verifying reconnecting after a failed connect() attempt.
- Add unit test verifying discardPendingSocket() attaches an error sink.
@shobhitagnihotri69
shobhitagnihotri69 force-pushed the fix/136-connect-rejects-on-error branch from 1778862 to 3236c00 Compare September 20, 2026 02:08
@ccampbell-aai
ccampbell-aai merged commit f9aaf8b into AssemblyAI:main Sep 21, 2026
3 checks passed
@ccampbell-aai

Copy link
Copy Markdown
Contributor

@shobhitagnihotri69 thanks for making those changes! Great find and we appreciate the contribution!

@shobhitagnihotri69

Copy link
Copy Markdown
Contributor Author

Thank you very much for merging , I appreciate every merge, looking forward to contribute as much as possible @ccampbell-aai

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