Skip to content

partysocket: the default reconnection delay has no jitter, but the README says it is random #441

Open

Description

@mark1russell7

The README gives the default minReconnectionDelay as 1000 + Math.random() * 4000. Since #397 (1.1.19) the default is a fixed 3000, so that the import makes no random value. So all clients with the default options that lose the same server wait the same times (3000 ms, 3900 ms, ...) and reconnect at the same moments.

https://github.com/cloudflare/partykit/blob/partysocket%401.3.0/packages/partysocket/src/ws.ts#L126-L136
https://github.com/cloudflare/partykit/blob/partysocket%401.3.0/packages/partysocket/README.md?plain=1#L169-L182

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)
//
// The README says that the default minReconnectionDelay is 1000 + Math.random() * 4000. Three sockets with the
// default options lose the same server at the same time. This prints the wait of each socket between its first
// and its second connection attempt.
import WebSocket from "partysocket/ws";

// A server that refuses each connection at once.
class RefusedWebSocket extends EventTarget {
  readyState = 0;
  constructor() {
    super();
    setTimeout(() => {
      this.readyState = 3;
      this.dispatchEvent(new Event("error"));
    }, 0);
  }
  send() {}
  close() {}
}

const sockets = [1, 2, 3].map((i) => {
  const attempts: number[] = [];
  const ws = new WebSocket(
    () => {
      attempts.push(Date.now());
      return "ws://example.test/";
    },
    undefined,
    { WebSocket: RefusedWebSocket },
  );
  return { i, ws, attempts };
});

setTimeout(() => {
  for (const { i, ws, attempts } of sockets) {
    console.log(`socket ${i}: wait before the 2nd attempt: ${attempts[1] - attempts[0]} ms`);
    ws.close();
  }
}, 5500);

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

Actual

socket 1: wait before the 2nd attempt: 3005 ms
socket 2: wait before the 2nd attempt: 3005 ms
socket 3: wait before the 2nd attempt: 3005 ms

Expected

As in the README: a random delay for each socket, from 1000 to 5000 ms. The random value can be chosen when the socket first needs it, not at import, so the fix of #397 stays.

A fix

A fix with a new test is on the branch fix/partysocket-random-default-delay of my fork, at 8c74423. 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).

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions