Skip to content

fix(channel): tear down orphaned WSClient on connect timeout (fixes #197) - #203

Open
Xuxchloris wants to merge 1 commit into
larksuite:mainfrom
Xuxchloris:fix/channel-connect-timeout-cleanup
Open

fix(channel): tear down orphaned WSClient on connect timeout (fixes #197)#203
Xuxchloris wants to merge 1 commit into
larksuite:mainfrom
Xuxchloris:fix/channel-connect-timeout-cleanup

Conversation

@Xuxchloris

Copy link
Copy Markdown

Fixes #197

Problem

When the WebSocket handshake does not complete within connect()'s timeout, the channel rejects with not_connected but leaves the still-handshaking WSClient running in the background:

  • it keeps retrying (sockets/timers leak on every timeout),
  • its late failures can surface as unhandled exceptions ("WebSocket was closed before the connection was established") that crash the process — the reporter observed 1133 occurrences over 4 weeks and a hard crash during a network blip.

There is also a second, independent unhandled-exception path inside ws-client: aborting a CONNECTING socket (handshake watchdog or close()) calls removeAllListeners() first and then terminate()/close(), and the ws library emits a late error on a terminated CONNECTING socket — with every listener removed that becomes an uncaughtException.

Changes

  • channel/channel.ts: on connect timeout, force-close the orphaned WSClient before rejecting, so it cannot leak or keep retrying after the attempt is abandoned.
  • channel/types.ts: new connectTimeoutMs option (default 15000, previously hardcoded) so callers can tune the window.
  • ws-client/index.ts: keep a no-op error listener after removeAllListeners() in both the handshake watchdog and close(), so aborting a CONNECTING socket stays silent instead of crashing the process.
  • channel/__tests__/connect-timeout.test.ts: new tests asserting connect() rejects with not_connected and force-closes the orphaned client, and that a retry starts a fresh client.
  • docs (channel.md / channel.zh.md) updated for the new option.

Verification

  • npx jest channel — 27 suites, 276 tests passing (including the 2 new tests).
  • All changed code paths are unit-tested; no behavior change for successful connects or runtime auto-reconnect (the client is only closed when connect() itself gives up).

@CLAassistant

CLAassistant commented Aug 14, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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

Labels

None yet

Projects

None yet

2 participants