Skip to content

Fix: reconnect() not working after maxRetries exhausted#322

Merged
threepointone merged 1 commit intomainfrom
fix-reconnect
Feb 9, 2026
Merged

Fix: reconnect() not working after maxRetries exhausted#322
threepointone merged 1 commit intomainfrom
fix-reconnect

Conversation

@threepointone
Copy link
Collaborator

Summary

Fixes cloudflare/partykit#252 — calling ws.reconnect() after maxRetries has been exhausted now correctly resets and initiates a new connection.

Problem

In _connect(), when maxRetries is reached, the method acquires _connectLock = true but returns early without releasing it. This leaves the lock permanently held, so any subsequent call to reconnect() (which resets _retryCount and calls _connect()) silently bails out at the lock check — making it impossible to manually reconnect after automatic retries are exhausted.

This is a common scenario when a user wants to force a reconnection on window focus after the tab has been inactive and retries have been spent.

Fix

Release _connectLock before the early return in the maxRetries guard inside _connect():

 if (this._retryCount >= maxRetries) {
   this._debug("max retries reached", this._retryCount, ">=", maxRetries);
+  this._connectLock = false;
   return;
 }

Test plan

  • Added a test that connects to an unreachable URL with maxRetries: 0, waits for retries to exhaust, then calls reconnect() with a valid URL and verifies the connection opens successfully.
  • All existing 38 tests in reconnecting.test.ts continue to pass.

Fix a bug where reconnect() could not start a new connection after maxRetries was exhausted because _connectLock was not released in _connect(). Set _connectLock = false on the early return when max retries are reached. Added a test (reconnecting.test.ts) that reproduces the issue and verifies reconnect() works after retries are exhausted, and included a changeset entry documenting the patch.
@changeset-bot
Copy link

changeset-bot bot commented Feb 9, 2026

🦋 Changeset detected

Latest commit: 5383192

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
partysocket Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@threepointone threepointone merged commit 3ed3bf0 into main Feb 9, 2026
4 of 5 checks passed
@threepointone threepointone deleted the fix-reconnect branch February 9, 2026 03:27
This was referenced Feb 9, 2026
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.

partysocket: Unable to force .reconnect() after failed connection

1 participant