Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(hosted-team): a server that loses the admin-socket bind race does not host (M5)

review2bac63cc1dc5
diff --git a/docs/hosted-team-relay.md b/docs/hosted-team-relay.md
index fbeef32fb..55fdb4569 100644
--- a/docs/hosted-team-relay.md
+++ b/docs/hosted-team-relay.md
@@ -372,8 +372,8 @@ The human `team status` reads `state`, `idle` and `lastError` together:
counts as corrupt.
- **`team.json` edited by hand while the service runs** is not re-read until the service restarts;
the next CLI write replaces it with the service's copy. Use the CLI.
-- **Two servers on one data directory:** the second one finds the admin socket answering and
- **does not host** (it logs "Hosted team relay: NOT started — another nodeterm server is already
+- **Two servers on one data directory:** the second one finds the admin socket answering, or loses
+ the race to bind it when both start at once, and **does not host** (it logs "Hosted team relay: NOT started — another nodeterm server is already
running on this data directory …").
- **"The nodeterm server is not running (no admin socket …)"**: the service is down, runs with
another data directory (pass `--data-dir`), or predates this feature and needs a restart after
diff --git a/src/core/relay/team-admin.test.ts b/src/core/relay/team-admin.test.ts
index 2a572ac43..2f1c15747 100644
--- a/src/core/relay/team-admin.test.ts
+++ b/src/core/relay/team-admin.test.ts
@@ -385,6 +385,21 @@ describe.skipIf(process.platform === 'win32')('team admin socket (unix socket, P
expect((await callTeamAdmin(dataDir, { cmd: 'status' })).ok).toBe(true)
})
+ it('M5: two servers racing the bind — the loser is BUSY (it must not host), never a plain error', async () => {
+ const dataDir = tmp()
+ // Both pass the stale-socket check (nothing is there yet) before either binds, so the second
+ // bind meets the first one's socket: EADDRINUSE.
+ const results = await Promise.allSettled([startTeamAdmin(dataDir, fakeService().svc), startTeamAdmin(dataDir, fakeService().svc)])
+ const won = results.filter((r): r is PromiseFulfilledResult<{ close(): Promise<void> }> => r.status === 'fulfilled')
+ const lost = results.filter((r): r is PromiseRejectedResult => r.status === 'rejected')
+ for (const w of won) closers.push(() => w.value.close())
+ expect(won).toHaveLength(1)
+ expect(lost).toHaveLength(1)
+ expect(lost[0].reason).toMatchObject({ code: 'E_ADMIN_SOCKET_BUSY', message: expect.stringMatching(/already/) })
+ // The winner is untouched and answers.
+ expect((await callTeamAdmin(dataDir, { cmd: 'status' })).ok).toBe(true)
+ })
+
it('refuses to remove something at the socket path that is not a socket', async () => {
const dataDir = tmp()
fs.mkdirSync(relayDir(dataDir), { recursive: true })
diff --git a/src/core/relay/team-admin.ts b/src/core/relay/team-admin.ts
index dd3b6b36c..374093445 100644
--- a/src/core/relay/team-admin.ts
+++ b/src/core/relay/team-admin.ts
@@ -388,9 +388,20 @@ export async function startTeamAdmin(dataDir: string, svc: HostedService): Promi
})
})
await new Promise<void>((resolve, reject) => {
- server.once('error', reject)
+ const fail = (err: NodeJS.ErrnoException): void => {
+ // Two servers starting on one data dir both find no socket in `clearStaleSocket`, and the
+ // second bind meets the first one's socket. That is the same verdict as a live socket found
+ // there: busy, so the caller does not host (two hosts on one key would both mint and both
+ // write team.json). Anything else stays the error it is.
+ reject(
+ err.code === 'EADDRINUSE'
+ ? new AdminSocketBusyError(`Another nodeterm server is already listening on ${sock} (two servers sharing one data directory?).`)
+ : err
+ )
+ }
+ server.once('error', fail)
server.listen(sock, () => {
- server.off('error', reject)
+ server.off('error', fail)
resolve()
})
})