Skip to content

Flaky test: ssm/session.test.ts handshake-flush assertion races a fixed 5ms setTimeout #61

Closed

Description

@scttfrdmn

Observed once during a full-suite run (npx vitest run) while working on #55; passes in isolation and passed 3/3 subsequent full runs, so it is load-dependent rather than deterministic.

× SsmSession > queues input sent BEFORE handshake completes, then flushes on ready

Cause

src/ssm/session.test.ts:153 waits for an async flush with a wall-clock sleep:

ws.push(await agentFrame({ payloadType: PayloadType.HandshakeComplete, payload: enc.encode("{}") }));
await new Promise((r) => setTimeout(r, 5));
const inputs = ws.sent.map(...).filter((m) => m.payloadType === PayloadType.Output);
expect(inputs.length).toBeGreaterThan(0);

5 ms is enough on an idle machine and not enough when 26 other test files are running in parallel workers. The same pattern appears at :163 and elsewhere in the file, so this is the first one to lose the race, not the only one that can.

Why it matters beyond the noise

A flake here is worse than an ordinary flake because the assertion is about input being queued rather than dropped — the terminal's correctness property. A test that intermittently fails for timing reasons trains everyone to re-run it, which is exactly how a real regression in the flush path would get waved through.

Fix

Replace the sleep with a condition wait — poll for the expected frame with a generous ceiling, so the test's duration tracks the machine rather than its correctness:

async function waitFor(pred: () => boolean, ms = 1000) {
  const t0 = Date.now();
  while (!pred()) {
    if (Date.now() - t0 > ms) throw new Error("timed out waiting for condition");
    await new Promise((r) => setTimeout(r, 1));
  }
}

Then await waitFor(() => outputFrames(ws).length > 0). Applies to every setTimeout(r, 5) in the file.

Found incidentally; not caused by #55 (reproduced on unmodified main, and the file is untouched by that work).

Activity

  1. added
    bug
    on Aug 1, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions