Skip to content

fix(agent): keep the host's link when a second fctl-agent starts - #82

Merged
kalaspuff merged 1 commit into
mainfrom
fctl/37-agent-serve-lock
Oct 18, 2026
Merged

kalaspuff merged 1 commit into
mainfrom
fctl/37-agent-serve-lock

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Some hosts start fctl-agent serve again even when it may still be running:

  • a host that restarts does so for each running session;
  • a host whose link to the agent dies does so before it attaches again.

When the agent is still running, which is the usual case, the second supervisor connected to the socket to find that out. The running supervisor took that connection for a host link: it made it current in place of the host's, sent it a hello, and dropped it when it closed. When that probe came after the host had attached, nothing reached the host until it gave up on the silent link a minute later. The session's viewers saw the agent unreachable meanwhile. The second supervisor also logged ERROR fctl-agent stopped: another fctl-agent is already serving ….

Session 37's agent log shows the probe when fctl host restart ran there (#80). That time it came 155 ms before the host's attach, so it did no harm. But nothing orders the two: the host starts serve with a detached exec and attaches right after.

What changed

  • link::claim (crates/fctl-agent/src/link.rs): serve uses it to claim the socket. It takes an exclusive lock on ~/.fctl/agent.lock (File::try_lock), which the supervisor holds until it exits. A second serve that finds the lock held leaves at once, with fctl-agent is already serving …; this one exits at info level and exit status 0, without touching the socket.
  • Older supervisors: they hold no lock, so a supervisor that gets the lock still checks the socket by connecting, as before. bind reports that case as a typed AlreadyServing, which serve treats the same way.
  • When the lock can't be taken on a filesystem without flock, it warns and falls back to the socket check, so the agent still starts.
  • docs/architecture.md: a paragraph says why a restarted host starts fctl-agent again, and how the lock keeps the second one off the socket.

Compatibility

This changes fctl-agent only, and no protocol. Sandboxes get the new binary when a session starts or resumes. A host's recovery runs whatever binary the sandbox has, so running sessions keep the old behaviour until then. No driver reads the second serve's exit status: Apple container and Docker start it with exec -d, Lima in the background, and the process driver checks the socket itself first.

How I verified it

  • Unit tests in link.rs:
    • A second claim while the first is held is Taken, and the first listener accepts no connection. With the lock check disabled, this test fails on that assertion.
    • Once the first claim is dropped, the next one serves over the stale socket.
    • A supervisor from before the lock is found on the socket.
  • End to end in this sandbox: a script runs the old 0.5.0 binary and the new one in turn. It starts serve, attaches a host with fctl-agent attach, starts a second serve, then sends the host's ping.
    • Old: host link 2 replaces link 1, then host link 2 detached, the second serve exits 1 with the error, and the ping gets no pong.
    • New: the second serve exits 0 with the info line, the supervisor logs no second link, and the ping gets its pong.
  • Checks: cargo xtask check --quick passed (224 Rust tests, 101 web tests). An earlier run had one unrelated failure, driver::container::tests::pulling_the_default_image_falls_back_to_the_latest_published_one in fctl-host, which this change doesn't touch. It passed in the 12 runs that followed; it may be the known "text file busy" race when tests run scripts they have just written.

Fixes #80
Refs #1

Session: https://app.fctl.sh/s/ses_01M57Y5XSCHG3RXMSFMRW6YFFX

A host that restarts, or that finds its link to fctl-agent dead, starts
`fctl-agent serve` again in case the agent is gone. When it isn't, the
second supervisor connected to the socket to find that out, and the
running one took that connection for a host link: it replaced the host's
link, then closed. When that probe came after the host had attached, the
supervisor ignored the host until it gave up on the silent link a minute
later. The second supervisor also logged "ERROR fctl-agent stopped".

The supervisor now holds a lock on ~/.fctl/agent.lock while it runs, and
a second one that finds it held exits with an info line, without touching
the socket. Supervisors from before the lock are still found by
connecting.

Fixes #80
@kalaspuff
kalaspuff merged commit 450b4b1 into main Oct 18, 2026
4 checks passed
@kalaspuff
kalaspuff deleted the fctl/37-agent-serve-lock branch October 18, 2026 17:11
kalaspuff pushed a commit that referenced this pull request Oct 18, 2026
`link::tests::a_second_supervisor_leaves_the_socket_alone` (added in
#82) failed on #83's CI run, which changes only `web/biome.json`
([log](https://github.com/hyperstructure/fctl/actions/runs/39148722173/job/117737802507)).
It passed on #82's own run and on `main` after the merge. The failing
line was the last claim: once the first supervisor dropped its lock and
listener, the next `claim()` still found the socket taken.

The claim logic is fine; the test races other tests. fctl-agent's other
tests start `git` and other processes on other threads, and a process
between fork and exec holds copies of every file descriptor,
close-on-exec ones included. If one is starting when the test drops the
first supervisor's lock and listener, the lock stays held and the socket
keeps accepting for that moment, so `claim()` answers `Taken`.

The test now claims again every 20 ms for up to two seconds, and fails
if the next supervisor gets the socket without the lock.

How I verified it:

- A throwaway stress test (not committed) repeated the drop and the next
claim 3000 times while another thread started `true` in a loop. The old
single claim failed 198 times; the new loop never failed. Without the
spawning thread, the old claim never failed either.
- If the first supervisor keeps its lock, the test still fails after two
seconds: "the next supervisor should get the socket once the first is
gone".
- `cargo xtask check --quick` passes.

Session: https://app.fctl.sh/s/ses_01M57ZX7KDDYZ1FMZ1BRWKXXVV

Co-authored-by: fctl-sh[bot] <fctl-sh[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Recovering a session after a host restart can steal the agent's host link

1 participant