Skip to content

Fix #1901: tell "Tower not running" from "Tower not answering"; tower start never stops a busy Tower - #1976

Merged
waleedkadous merged 21 commits into
mainfrom
builder/bugfix-1901
Nov 25, 2026
Merged

waleedkadous merged 21 commits into
mainfrom
builder/bugfix-1901

Conversation

@waleedkadous

@waleedkadous waleedkadous commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Under heavy load (load average 31 to 43), afx send, afx status and other commands printed Tower is not running. Start it with: afx tower start while Tower was up and answering /health on a retry seconds later. That wording invites starting a second Tower. Worse, two commands acted on the false negative: afx workspace start went on to kill the live Tower (below), and afx workspace stop cleared the runtime state of builders that were still running.

Fixes #1901

Root Cause

Reproduced first, with the discarded error captured:

  • Under load (measured on this machine, load about 44): 59 of 300 probes that mirror the SDK request failed, and every failure was TimeoutError at the 10 s abort. curl showed connect always completing in under 8 ms while time-to-first-byte reached 29 s. Tower was listening; its event loop was starved past the timeout. There was no accept-queue or connect failure.
  • Without load (local servers on ephemeral ports), each failure shape went through the SDK's own error mapping. Refused, never-answering and resetting endpoints all became isRunning() === false.

TowerClient.isRunning() returns a bare boolean, so every /health failure (refused, timeout, reset, error status, unreadable body) collapsed into "not running" in every caller. The ECONNREFUSED message check that could have told them apart was dead under Node's fetch, because the code lives on err.cause. AIR #1950 (afx send --retry, merged meanwhile) fixed that check and added connectionRefused, and this PR builds on it. One gap remained: over localhost, Node's AggregateError copies the code of the first attempt only. Tower binds IPv4, so that first attempt is always a refused ::1, which would mask a 127.0.0.1 that failed another way.

Fix

  • SDK: TowerClient.probeHealth() classifies one /health probe as running, not-running (connection refused) or unresponsive (with the reason).
    • A localhost refusal counts when at least one attempt was refused and every attempt failed before reaching a server (EHOSTUNREACH, ENETUNREACH, EADDRNOTAVAIL). So a broken-IPv6 ::1 plus a refused 127.0.0.1 still reads as down, while a refused ::1 cannot mask a 127.0.0.1 that was reached and then reset.
    • Generic failures keep their cause code (fetch failed: ECONNRESET).
    • New requestTimeoutMs option (default 10 000), used by the stalled-server test.
    • isRunning() now delegates to probeHealth().
  • One shared CLI gate: lib/tower-check.ts checkTower().
    • Only a refused connection says "Tower is not running. Start it with: …".
    • Anything else is retried once after 1 s, then reported as Tower did not answer /health (<error>) after N ms; it appears to be up, retry, with no advice to start Tower.
    • A caller with its own retry loop passes retries: 0, so the two do not stack.
  • Routed through checkTower():
    • send, interrupt, reset, self-refresh, tower sweep-husks, workspace add-architect and workspace remove-architect.
    • status: its human output shows "not answering" with the reason. status --json keeps tower.running as it was (false for down and busy alike, so existing scripts read the same) and adds tower.state (running / not-running / unresponsive) and tower.error (the reason when unresponsive, otherwise null).
    • workspace start and workspace stop now exit with the message when Tower is unresponsive.
  • Adjacent fixes, same false negative:
    • workspace start used to treat it as "start one" and fall into the kill below.
    • workspace stop used to clear runtime state of builders that were still running. It also aborts, clearing nothing, when the health probe passes but Tower never answers the deactivation request itself (from review). deactivateWorkspace() now passes status and connectionRefused through.
    • rename, shell and open printed "not running" for any failed request; now they do only for a refused connection. open reports the API error for anything else and no longer prints "not running" for HTTP errors either.
  • send --retry N owns the retrying. With --retry the gate probes once per attempt, so an unanswering Tower gets N + 1 probes, not the 2(N + 1) it would get if the gate's own retry stacked inside each attempt. Without --retry, the gate's single retry applies (two probes).
  • ide.ts needed no change. It uses http.request, so err.code is reliable, and it already reports timeouts separately.

Merged with Spec 1940 (named architects, PR #1957)

Main moved during review, and PR #1957 changed send.ts and the SDK client too. Both PRs change what send does when something fails. When a <workspace>:architect:<name> send comes back NOT_FOUND, theirs asks the address book, to tell an older Tower that cannot parse the form. I read the merged code as one piece:

  • An unresponsive /health stops at the gate, before any send or address-book question, and reads as unresponsive.
  • An unanswered send is a timeout and asks the address book nothing, because only a NOT_FOUND triggers that question.
  • The gap: a send that came back NOT_FOUND followed by an address-book check Tower never answered was reported as the bare not-found. Nobody could judge it either way.
    • getAddressBook() now marks a failure with no HTTP answer as unanswered.
    • send then says Tower did not answer the check that tells a missing address from an older Tower, and that it appears to be up and should be retried.
    • An answered failure of that check still leaves the not-found, as Spec 1940 tested.

Behaviour change: afx tower start no longer replaces a hung Tower by itself

Before, towerStart probed /api/status with a 2 s timeout. If that missed and the port was in use, it decided the holder was a zombie, SIGTERMed every PID on the port and started a new Tower. Under load that PID is the live Tower, and running afx tower start (which the old error message told people to do) killed it.

Now towerStart uses the same probe. A refused port starts as before, and a healthy Tower gets "already running" as before. A holder that accepted the connection but did not answer is left alone: start exits non-zero, prints the PID and command line so the reader can see whether it is a Tower, says to wait and retry if the machine is busy, and says to run afx tower stop, then afx tower start, to replace it deliberately. Stopping a Tower is now always a human's decision. afx tower stop is unchanged: it kills the PIDs listening on the port without a health probe, so it still stops a hung Tower.

Testing

  • bugfix-1901-tower-check (injected fetch through the real CLI client):
    • a refused connection gives "not running" with one probe;
    • a reset or timeout followed by success passes silently;
    • two failures give "did not answer" with the underlying error and no suggestion to start Tower.
  • bugfix-1901-tower-start-unresponsive: a real child process that accepts and never answers, on an ephemeral port, so the tests never reach Tower on 4100.
    • Start signals nothing, exits non-zero, and names the PID and command line.
    • A refused port starts normally.
    • Stop still kills the stalled holder.
    • The first test fails on the pre-fix tower.ts, where start SIGTERMed the holder.
  • bugfix-1901-workspace-start-stop:
    • workspace start on an unresponsive Tower exits and never calls towerStart, and a refused connection still starts Tower.
    • workspace stop aborts without clearRuntime() when /health or the deactivation request goes unanswered, and an answered error still falls back to local cleanup.
    • Removing the start or stop guard fails its test.
  • bugfix-1901-send-unresponsive (the real command and SDK client over a scripted Tower, the harness from Spec 1940's test):
    • a three-part address against an unanswering /health, an unanswering address-book check, and an unanswering send each report unresponsive or timeout, never "older Tower" and never a bare not-found;
    • total probes are pinned: two without --retry, N + 1 with --retry N.
    • The address-book and probe-count tests fail without their changes.
  • bugfix-1901-status-json: tower is {running, state, error} for running, not-running and unresponsive, with error present as null when there is no error. It fails without the change.
  • SDK probeHealth cases: a refused ::1 does not mask a reset on 127.0.0.1 (this one fails without the rule), and an unreachable ::1 plus a refused 127.0.0.1 reads as not running.
  • End to end with the built CLI, isolated HOME, stalled holder on a spare port:
    • afx tower start --port P exited 1 after 21 s with the message, and the holder survived;
    • afx tower stop --port P killed it.
  • Existing tests: Tower mocks gained a one-line probeHealth mapped from their isRunning mock. The afx tower start: surface the owner-guard refusal in the CLI instead of the generic 30s timeout (split from #1690 item 1) #1691 and windows-launch towerStart tests now stub the probe. They used to fake a refusal by stubbing node:http, and with the probe on fetch they would have reached the live Tower.
  • porch checks (fix phase): build passed (13.1 s) and the full packages/codev suite passed (123.6 s), with the 3-worker cap from PR Fix #1956: cap packages/codev vitest at 3 workers (singleFork was dead since Vitest 4) #1965.
  • Full local suite on the merged head (after merging PR [Spec 1940] Stage 1: named architects across workspaces on one Tower #1957, with this round's changes, npm test in packages/codev, load 15.8): 432 files passed and 14 skipped; 9071 tests passed and 116 skipped; 170 s.
  • Windows workflow (Tower lifecycle code):
  • The local tests check ran with porch.checks.tests.timeout_ms = 900000, set first in this worktree's .codev/config.local.json per the architect's instruction and since made the workspace setting (main-42). It only lengthens the limit.

Review

  • codex: REQUEST_CHANGES. The workspace stop deactivation-timeout path; fixed with a test.
  • claude: APPROVE. Fixed in place: the broken-IPv6 refusal case, a silent open exit, and tests for the start/stop guards.
  • Follow-ups I suggested:

Size: 283 insertions and 96 deletions in source; 631 insertions in tests.

Under load a live Tower accepts the connection at once but its starved
event loop misses the 10 s request timeout. isRunning() collapsed that
(and resets) to false, so commands said "Tower is not running. Start it
with: afx tower start" while Tower was up.

- sdk: TowerClient.probeHealth() classifies one /health probe as
  running / not-running (connection refused) / unresponsive (with the
  reason). A localhost refusal counts only when every address refused.
  Generic failures keep the cause code (fetch failed: ECONNRESET).
  requestTimeoutMs option (default 10 000).
- cli: lib/tower-check.ts checkTower() is the one gate: refused reads
  "not running"; anything else is retried once after 1 s, then reported
  with the underlying error and no advice to start Tower.
- send, interrupt, reset, self-refresh, sweep-husks, add/remove-architect
  and status go through it. workspace start/stop stop on an unresponsive
  Tower instead of starting a second one or clearing live runtime state.
- rename, shell and open say "not running" only for a refused connection.
- afx tower start no longer SIGTERMs a port holder that did not answer:
  it exits non-zero naming the PID and command line, and leaves replacing
  it to afx tower stop. Stop is unchanged and still stops a hung Tower.
…nning"; start never stops a stalled holder

- bugfix-1901-tower-check: injected fetch through the real CLI client.
  Refused gives "not running"; a reset or timeout followed by success
  passes silently; two failures give "did not answer" with the error and
  no suggestion to start Tower.
- bugfix-1901-tower-start-unresponsive: a real child process that accepts
  and never answers, on an ephemeral port. Start signals nothing, exits
  non-zero and names the PID and command line; a refused port starts
  normally; stop still kills the stalled holder. The first test fails on
  the pre-fix tower.ts (start SIGTERMed the holder).
- sdk probeHealth cases, including a refused ::1 not masking another
  failure on 127.0.0.1 (fails without the every-address rule).
- Existing Tower mocks gain probeHealth mapped from their isRunning mock.
  The #1691 and windows-launch towerStart tests stub the probe, which
  otherwise would reach the live Tower on 4100.
…d deactivation; IPv6-unreachable still reads as refused

From PR review (CMAP round 1):
- workspace stop guarded only the health probe. A healthy probe followed
  by a deactivation request Tower never answered fell into the legacy
  cleanup and clearRuntime(), wiping the registry of builders that may
  still be running. deactivateWorkspace() now passes status and
  connectionRefused through, and stop aborts with "nothing was cleared"
  when Tower took the request and never answered. Answered errors still
  fall back to local cleanup.
- A localhost refusal counts when at least one attempt was refused and
  every attempt failed before reaching a server (EHOSTUNREACH,
  ENETUNREACH, EADDRNOTAVAIL). On a machine with broken IPv6, an
  unreachable ::1 plus a refused 127.0.0.1 is a down Tower, so
  workspace start starts one instead of exiting. A reached-then-reset
  127.0.0.1 is still not masked.
- open always reports the API error line instead of exiting silently
  when a failed request carries no error text.
…broken-IPv6 refusal

- bugfix-1901-workspace-start-stop:
  - start on an unresponsive Tower exits and never calls towerStart;
  - a refused connection still starts Tower;
  - stop aborts without clearRuntime() when /health is not answered and
    when the deactivation request is not answered;
  - an answered error still falls back to local cleanup.
  The start and stop guards each fail their test when removed.
- sdk: an unreachable ::1 plus a refused 127.0.0.1 reads as not-running.
- Thread: CMAP round 1 notes.
…r as a not-found; status --json tells busy from down

Architect round, after merging PR #1957 (Spec 1940, named architects):
- A <workspace>:architect:<name> send that came back NOT_FOUND asks the
  address book, to tell an older Tower. When Tower did not answer that
  check at all, the bare not-found was reported, though nobody could
  judge it. getAddressBook() now marks a failure with no HTTP answer as
  unanswered, and send says the check went unanswered and to retry. An
  answered failure of the check still leaves the not-found.
- checkTower() takes a retries option (default 1). send --retry N passes
  0, so its budget owns the retrying: N + 1 health probes instead of
  2(N + 1). Without --retry the gate's single retry still applies.
- status --json: tower is {running, state, error}. running is unchanged
  for existing scripts; state is running, not-running or unresponsive;
  error is the reason when unresponsive, otherwise null.
… ownership; status --json state

- bugfix-1901-send-unresponsive: the real send command and SDK client
  over a scripted Tower.
  - A three-part address against an unanswered /health, an unanswered
    address-book check, and an unanswered send each read as unresponsive
    or a timeout, never "older Tower" and never a bare not-found.
  - Total probes pinned: 2 without --retry, N + 1 with --retry N.
- bugfix-1901-status-json: tower is {running, state, error} for running,
  not-running and unresponsive.
- Each fails without its change.
- Thread: architect-round notes.
…requestTimeoutMs

PR #1971 (merged from main) added requestBinary() with the module's fixed
REQUEST_TIMEOUT_MS. The requestTimeoutMs option this PR added is the
timeout for every request, and the other three request paths use it.
The default is unchanged (10 000 ms), so only a client that sets the
option sees a difference.
@waleedkadous
waleedkadous merged commit c29f09c into main Nov 25, 2026
8 checks passed
waleedkadous added a commit that referenced this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

afx: 'Tower is not running' reported under high load while Tower is up; the real probe error is discarded

1 participant