fix: stop the home screen's tmux scan in a hidden tab (release review)

The poll had no hidden-tab gate, and the local scan's 5 s cache sits under the
8 s interval, so a single tab never hit it: every tick was a full socket sweep
(1.1 s and 772 forks on a box with 212 sockets) for a list nobody was reading.

`_foreignPollTick()` is now the one gate for the first load, the timer and the
return to a visible tab. The `visibilitychange` listener is stored and removed
in `stopForeignPolling()`, since this runs on every trip to the home screen.

Checked in real chromium with document.hidden stubbed: 2 polls in 9.5 s visible,
0 in 17.5 s hidden, 1 on return, and a tab opened in the background makes none
until it is shown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-11-06 08:41:15 +01:00
1 parent 37dfb73b1e
commit 9714ee79e8
3 files changed
+108 -9

No files matched your search

+22 -5
View File
@@ -21,6 +21,11 @@
* tab keeps sweeping tmux sockets forever. `startForeignPolling` /
* `stopForeignPolling` are called from `showWelcome`/`hideWelcome`.
*
* A tab hidden while it shows the home screen skips its ticks as well. The TTL sits
* under the poll interval, so one tab never hits the cache: every tick is a full
* sweep, one fork or more per socket, for a list nobody is reading. It catches up
* on `visibilitychange`, and the cache is what bounds a flurry of tab switches.
*
* ⚠️ Docker and remote locations are NOT in that poll. Each costs a `docker exec`
* or a full ssh handshake per target, each with a ~12s timeout, so riding them
* along on an 8s tick stacks connections on a slow host, the one cost the
@@ -78,11 +83,14 @@ Object.assign(CodemanApp.prototype, {
*/
startForeignPolling() {
if (this._foreignPollTimer || this._foreignRefused) return;
void this.loadForeignSessions();
this._foreignPollTimer = setInterval(() => {
// Local only: see @fileoverview. The wide scan never rides the timer.
void this.loadForeignSessions();
}, this._foreignPollMs || FOREIGN_POLL_FALLBACK_MS);
this._foreignPollTick();
this._foreignPollTimer = setInterval(
() => this._foreignPollTick(),
this._foreignPollMs || FOREIGN_POLL_FALLBACK_MS
);
// Stored, unlike a poller made once: this runs on every trip to the home screen.
this._foreignOnVisible = () => this._foreignPollTick();
document.addEventListener('visibilitychange', this._foreignOnVisible);
},
/** Stop polling. MUST run on leaving the home screen, see @fileoverview. */
@@ -90,6 +98,15 @@ Object.assign(CodemanApp.prototype, {
if (!this._foreignPollTimer) return;
clearInterval(this._foreignPollTimer);
this._foreignPollTimer = null;
document.removeEventListener('visibilitychange', this._foreignOnVisible);
this._foreignOnVisible = null;
},
/** One poll, and the one gate every poll passes: a hidden tab skips it, see @fileoverview. */
_foreignPollTick() {
if (document.hidden) return;
// Local only: see @fileoverview. The wide scan never rides the timer.
void this.loadForeignSessions();
},
/** Whether the user asked for the expensive docker/remote scan too. */