Skip to content

browser-sync-service tests leak a reconnect timer, causing a cross-file flake in full-suite runs #2684

Closed

Description

@malibio

Problem

bun run test (full suite) intermittently fails with 1 failed test file / 1 failed test, reported as an unhandled error originating in BrowserSyncService.scheduleReconnect:

 ❯ BrowserSyncService.scheduleReconnect src/lib/services/browser-sync-service.ts:364:29
    364|     this.reconnectTimeout = setTimeout(() => {
 ❯ BrowserSyncService.connect src/lib/services/browser-sync-service.ts:181:12
 ❯ src/lib/services/browser-sync-service.ts:366:12

 Test Files  1 failed | 246 passed (247)
      Tests  1 failed | 5084 passed (5085)

src/tests/services/browser-sync-service.test.ts passes 47/47 when run in isolation (bun run test -- browser-sync-service), so this is a cross-file timing leak, not a genuine assertion failure.

Root cause

BrowserSyncService retries reconnection indefinitely by design (scheduleReconnect, browser-sync-service.ts:356-367) — connect()'s catch block (:180-181) arms a setTimeout that calls connect() again.

Two things combine to let that timer escape the test that created it:

  1. The top-level afterEach (browser-sync-service.test.ts:100-106) resets stores and mocks but never calls browserSyncService.disconnect() — which is the only thing that runs clearTimeout(this.reconnectTimeout) (browser-sync-service.ts:393). A timer armed during a test therefore survives teardown.
  2. The Reconnection Logic describe block installs fake timers (:947-949), but one test inside it swaps back to real timers and restores fake ones only in a finally (:1029-1035). A reconnect armed during that real-timer window is a real setTimeout with a >=1000ms backoff delay.

So a real timer can outlive its test, fire connect() while an unrelated test file is running, and surface as an unhandled error attributed to whichever file happens to be executing then. Whether it lands inside the suite's remaining runtime depends on scheduling — hence the intermittency and the shifting attribution.

Suggested fix

Call browserSyncService.disconnect() in the top-level afterEach so no reconnect timer can outlive its test. Worth also tightening the real-timer window at :1029-1035 so a reconnect armed there is cleared before fake timers are restored.

Acceptance criteria

  • browser-sync-service.test.ts cleans up any armed reconnect timer in teardown
  • Full-suite bun run test runs clean across repeated runs (no stray scheduleReconnect unhandled error)
  • browser-sync-service.test.ts still passes 47/47 in isolation

Notes

Found while establishing the test baseline for #1698. Pre-existing on main (commit 8961f7e) and unrelated to that work.

Activity

  1. added
    bug
    on Sep 17, 2026
  2. mstomar125 commented on Sep 17, 2026

    @mstomar125
    Contributor

    Frontend baseline (unfixed, single run): 5085 passed (5085), 247 test files passed (247). Duration 107.88s. Investigating the cross-file reconnect-timer flake per the issue's root-cause diagnosis; will run the suite repeatedly to try to reproduce before/after the fix.

  3. self-assigned this
    on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugui

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions