Repository navigation
fix(agent): keep the host's link when a second fctl-agent starts - #82
Merged
Merged
Conversation
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
This was referenced Oct 18, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some hosts start
fctl-agent serveagain even when it may still be running: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 restartran 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 startsservewith a detached exec and attaches right after.What changed
link::claim(crates/fctl-agent/src/link.rs):serveuses 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 secondservethat finds the lock held leaves at once, withfctl-agent is already serving …; this one exitsat info level and exit status 0, without touching the socket.bindreports that case as a typedAlreadyServing, whichservetreats the same way.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 withexec -d, Lima in the background, and the process driver checks the socket itself first.How I verified it
link.rs:Taken, and the first listener accepts no connection. With the lock check disabled, this test fails on that assertion.serve, attaches a host withfctl-agent attach, starts a secondserve, then sends the host'sping.host link 2 replaces link 1, thenhost link 2 detached, the second serve exits 1 with the error, and the ping gets nopong.pong.cargo xtask check --quickpassed (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_oneinfctl-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