Skip to content

Flaky: server-auto-open.test.ts races for its port and process.exit(1)s under load #2602

Closed

Description

@cliffhall

The problem

clients/web/src/test/integration/server/server-auto-open.test.ts gets its port from findFreePort(). That helper binds :0, reads the port, closes the socket, and returns the number. startHonoServer then binds that port later. Between the close and the bind, any other process on the machine can take the port.

When that happens, httpServer.on("error") in clients/web/server/server.ts:166 sees EADDRINUSE and calls process.exit(1). Under vitest that surfaces as process.exit unexpectedly called with "1", plus a failed keeps serving when the browser cannot be opened, and it fails npm run local:gate.

Reproduce

This depends on load: it needs several worktrees running suites at the same time, which is the normal setup here. It happened once in a full local:gate while I was working on #2437 (gate7.log, coverage stage). Run alone, the file passed 5/5.

Error: process.exit unexpectedly called with "1"
 ❯ Server.<anonymous> server/server.ts:171:15
 ❯ emitErrorNT node:net:1970:8
AssertionError: expected "vi.fn()" to be called with arguments: [ StringContaining{…} ]
  142|       expect(warn).toHaveBeenCalledWith(WARNING, expect.any(Error));

Expected

The test owns its port from the moment it picks it, so the suite can't go red because of what other processes are doing.

Possible fix

Have the server bind port: 0 itself and read back the port it actually got, the same way the harnesses in integration/mcp/remote/ do. That needs startHonoServer to accept 0 and report its bound port, if it doesn't already. Failing that, retry the start on EADDRINUSE inside the test. Either way this is a fix to the test, not to server.ts's handling of a port that really is taken (#1090).

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingv2Issues and PRs for v2

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions