Skip to content

fix(serve): rebuild the transport only when the paired set changes - #668

Open
matthieu403 wants to merge 1 commit into
outlmd:mainfrom
matthieu403:fix/serve-rebuild-loop
Open

matthieu403 wants to merge 1 commit into
outlmd:mainfrom
matthieu403:fix/serve-rebuild-loop

Conversation

@matthieu403

@matthieu403 matthieu403 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

Two paired outl serve daemons rebuild their transport every few seconds, forever, and a device that has just joined cannot finish its first sync while they do (#630).
The supervisor rebuilds on every write to peers.json, and the transport writes there itself: each inbound connection records the address the peer connected from.
A rebuild binds a new port, the other daemon records it, and that write rebuilds it in turn.

The supervisor now rebuilds only when the set of paired node ids differs from the one the transport was built with.
The mtime stays as the cheap gate for re-reading the file.
A pairing, a removal and a peer learned from membership gossip still cycle the transport; a refreshed address does not, and does not need to, since the catch-up loop reloads peers.json on every tick.

It is one file of behaviour, crates/outl-cli/src/cmd/sync_supervisor.rs.
The rest of the diff is the test module moving to sync_supervisor_tests.rs: the three new tests took the file past the 600-line ratchet.

How to verify

cargo test -p outl-cli
cargo clippy -p outl-cli --all-targets -- -D warnings
cargo fmt --all -- --check
scripts/check-file-size.sh

All pass. a_refreshed_peer_address_does_not_cycle_the_transport fails when the set comparison is taken out.
I did not run the whole workspace test suite.

With the real binary, the scenario of #630 (A and B on a fast link, C new and empty behind 5 Mbit/s and 20 ms, three outl serve, no Internet):

0.13.9 this branch (debug build)
Ops received by C in 2 min, B running 0 37,520 (all of them)
Transport rebuilds on A in 2 min, B running 36 0
Transport rebuilds on A in the 2 min after B stops 12 (now with C) 0

B and C each rebuilt once on this branch, when membership gossip told each about the other. That is a new paired device, so it is the rebuild the watch exists for.

Related issues / docs

Closes #630

Updated docs: docs/cli.md (the outl serve section), crates/outl-cli/CLAUDE.md.

  • User-facing change: the docs page that owns it is updated, and CHANGELOG.md [Unreleased] has an entry.

Anything reviewers should look at carefully

  • The rebuild on a new peer is kept as it was. Whether a running transport could pick a new peer up without a rebuild is a different question, and I left it alone.
  • A peers.json that cannot be read or parsed is not treated as a change, same as an unreadable mtime today. The baseline is left where it was, so the file is read again on the next tick (1 s).
  • No CRDT, wire or format change.

Out of scope for this PR

This PR has been co-written with Claude Code.


Prove it works
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@avelino avelino left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. The root cause is right: refresh_peer_direct_addr rewrites peers.json on every inbound connection, and a rebuild binds a new port that the other daemon then records, so the two keep rebuilding each other. Rebuilding only when the set of paired node ids changes breaks that loop.

I checked the points I was worried about. paired comes from transport.peers(), which is the same PeersStore::list() the file read gives, so the two sets can't drift apart and cause a rebuild loop. The baseline is taken before the build and the mtime is read before the file, so a write in between is not lost. Removal and a vanished file still cycle the transport. The catch-up loop does reload peers.json each tick, so skipping the rebuild for an address change is safe. I ran the 11 sync_supervisor tests, and with the set comparison disabled a_refreshed_peer_address_does_not_cycle_the_transport fails, so it does pin the fix. fmt, clippy and the file-size check are clean.

Nits, none blocking:

  • The "unreadable peers.json is not a change" branch has no test.
  • A few tests rely on sleep(50ms) for the mtime to move. Fine on ext4, APFS and tmpfs, but only one of them asserts it moved.

Before merge it needs a rebase: CHANGELOG.md conflicts in ### Fixed. Thanks for the careful repro table in the description.

`outl serve` rebuilt its transport on every write to `peers.json`.
The transport writes there itself: each inbound connection records
the address the peer connected from. A rebuild binds a new port, so
two paired daemons recorded each other's new port and rebuilt in
turn, every few seconds, without end. Each rebuild closes every
connection, which kept a third device on a slow link from finishing
its first sync.

The supervisor still watches the file's mtime, but only to re-read
it: it rebuilds when the set of paired node ids is no longer the one
the transport was built with (a pairing, a removal, a peer learned
from membership gossip). An address needs no rebuild, the catch-up
loop reloads `peers.json` on every tick.

The tests move to `sync_supervisor_tests.rs` to keep the file under
the 600-line ratchet.

Fixes outlmd#630
@matthieu403
matthieu403 force-pushed the fix/serve-rebuild-loop branch from e72998c to a84a6b9 Compare December 2, 2026 21:24
@matthieu403

Copy link
Copy Markdown
Contributor Author

Rebased on main. Thanks for the review.

This branch has not been deployed

No deployments
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.

Two paired outl serve daemons rebuild their transport every few seconds, so a new device never finishes its first sync

2 participants