Skip to content

Conversation

@akirk
Copy link
Member

@akirk akirk commented Jan 29, 2026

Motivation for the change, related issues

The tab coordinator was causing an infinite loop of broadcast messages, where checkForExistingTabs was being called repeatedly. This resulted in continuous ping messages being sent between BroadcastChannel instances, even within a single tab.

The issue had two contributing factors:

  1. Multiple BroadcastChannel instances (one in tab-coordinator.ts, another in useTabTracking hook) could trigger each other
  2. An unstable useCallback dependency in useTabTracking caused the effect to re-run on every render

Implementation details

tab-coordinator.ts:

  • Added mutex lock (isCheckingTabs) to prevent concurrent calls to checkForExistingTabs
  • Added rate limiting (lastCheckTime + 1 second minimum interval) to prevent rapid successive calls
  • Reset these guards in destroyTabCoordinator for proper cleanup

use-tab-tracking.ts:

  • Fixed unstable useCallback dependency by storing options.onWorkerLost in a ref
  • Changed checkWorkerLost callback to have empty dependencies [] instead of [options]

Testing Instructions (or ideally a Blueprint)

  1. Open Personal WordPress Playground
  2. Open browser DevTools console
  3. Paste this to monitor broadcast messages:
const originalPostMessage = BroadcastChannel.prototype.postMessage;
BroadcastChannel.prototype.postMessage = function(message) {
  console.log('%c📤 SEND', 'color: #4CAF50; font-weight: bold', this.name, message);
  return originalPostMessage.call(this, message);
};
  1. Verify that ping messages are NOT being sent continuously (should only see initial pings on load, then silence)
  2. Open a second tab with the same site - verify tabs detect each other properly

cc @fellyph since you also observed this!

Prevent infinite loop where checkForExistingTabs was being called
repeatedly, causing continuous ping messages between BroadcastChannel
instances in the same tab.

Changes:
- Add mutex lock and rate limiting (1s min interval) to checkForExistingTabs
- Fix unstable useCallback dependency in useTabTracking hook
@akirk akirk merged commit 67aa97a into trunk Jan 29, 2026
35 checks passed
@akirk akirk deleted the fix/personal-wp-tab-coordinator-loop branch January 29, 2026 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants