partysocket: a URL provider that rejects with undefined gives an unhandled rejection and stops the reconnection

Open Beginner friendly
#434 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
networking

Research direction

The bug is in the .catch() of _connect() in packages/partysocket/src/ws.ts (around lines 585-588), which reads err.message from a rejected URL or protocols provider. Run the repro in the issue against partysocket 1.3.0 to confirm the TypeError and the stalled CONNECTING state. Done means a provider rejecting with undefined or null emits an error event and schedules the next attempt, with no unhandled rejection, and the partysocket test suite passes.

Written by the indexing model from the issue text.

Description

The .catch() of _connect() reads err.message. When the URL provider (or the protocols provider) rejects with undefined or null, for example with reject() and no argument, that line throws a TypeError inside the .catch(). The result is an unhandled rejection, no error event, and no next attempt: the socket stays in CONNECTING.

https://github.com/cloudflare/partykit/blob/partysocket%401.3.0/packages/partysocket/src/ws.ts#L585-L588

Repro

The packages: [email protected]. Install them with npm i [email protected].

// npm install, then: node repro.ts (Node.js 22.18 or later runs TypeScript directly)
import WebSocket from "partysocket/ws";

process.on("unhandledRejection", (reason) => {
  console.log("unhandledRejection:", String(reason));
});

let calls = 0;
const ws = new WebSocket(
  () => {
    calls++;
    // for example, reject() in a token fetcher
    return Promise.reject(undefined);
  },
  undefined,
  { minReconnectionDelay: 50, maxReconnectionDelay: 50 },
);
let errors = 0;
ws.addEventListener("error", () => errors++);

setTimeout(() => {
  console.log("provider calls in 500 ms:", calls);
  console.log("error events:", errors);
  console.log("readyState:", ws.readyState);
  ws.close();
}, 500);

node repro.ts (Node.js 22.18 or later runs TypeScript directly)

Actual

unhandledRejection: TypeError: Cannot read properties of undefined (reading 'message')
provider calls in 500 ms: 1
error events: 0
readyState: 0

Expected

An error event, and a new attempt after the reconnection delay, as for a provider that rejects with an Error. No unhandled rejection.

A fix

A fix with a new test is on the branch fix/partysocket-provider-rejects-nullish of my fork, at 54a3d36. On a GitHub Actions runner, the new test fails before the change and passes after it, and the whole partysocket suite passes. This repository accepts pull requests only from collaborators, so I did not open a pull request. I can open one if you allow it, or you can take the commit.

I found this with AI assistance (Claude) during a review of WebSocket client libraries. The repro above was run with partysocket 1.3.0 on Node.js 24.22.2 on Linux (a GitHub Actions runner), and the output above is from that run. The fix was also prepared with AI assistance (Claude).

Dominant language
TypeScript
Stars
1.3k
Forks
86
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/partykit

All issues in cloudflare/partykit

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.