Skip to content

fix(daemon): honor exclusive device leases on the serial write path (#1864) - #1871

Merged
zackees merged 2 commits into
mainfrom
agent/1864-lease-serial-write
Nov 3, 2026
Merged

zackees merged 2 commits into
mainfrom
agent/1864-lease-serial-write

Conversation

@zackees

@zackees zackees commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Why

#1864's first slice (#1869) made deploy and reset honor a foreign exclusive
device lease. The remaining mutating path — serial attach + write — still
reached the board: any client could attach /ws/serial-monitor, take the
writer lock, and talk to a board another workflow had reserved with
fbuild device lease <port> --exclusive. A validation holder could be
preempted by an unrelated fbuild monitor (or a Python SerialSession) that
held no lease at all.

This is #1864 criterion 1/2 ("require every mutating device path — deploy,
reset, serial write/RPC writer — to honor a foreign exclusive device lease";
"let the lease holder pass its lease identity through … subsequent serial/RPC
calls").

What changed

  • SerialClientMessage::Attach gains lease_id: Option<String>
    (skip_serializing_if = "Option::is_none"): the wire format is unchanged for
    clients that predate it, and an omitted lease means "present no lease" — the
    safe reading, which is refused on a reserved board.
  • handle_serial_ws admits the attach through
    DeviceManager::admit_operation(&port, lease_id) before
    serial_manager.open_port(), so a refused client never opens, resets, or
    reads the board. The refusal is a typed error frame carrying
    ForeignLeaseHeld's message (lease id, holder, --lease-id).
  • Every write frame is re-admitted before the bytes reach the port, so a
    lease acquired by another workflow after a session attached stops the next
    write with write_ack{success:false} instead of silently flashing over the
    holder.
  • fbuild monitor --lease-id <id> passes an existing lease through (the lease
    id is the bearer token, same model as fbuild deploy --lease-id).
  • docs/reference/cli.md: the workflow-lease section and the fbuild monitor
    example now cover the serial write path.

Tests

ws_serial_attach_is_refused_under_a_foreign_exclusive_lease (portable, no
tty, no platform gate — the refusal happens ahead of open_port): with a
registered test device and an exclusive lease held by client A,

  1. client B attaching with no lease gets an error frame containing
    "reserved by exclusive lease" + the lease id + --lease-id, and the port is
    never opened (has_clients == false);
  2. client A attaching with the lease id clears admission (its later error
    is "port … not open", not a lease refusal).

serial_ws_replies_follow_request_order (existing real-WS + real-pty test)
gains the mid-session case: after another workflow leases the pty port, the
attached session's next write returns
write_ack{success:false, bytes_written:0} naming that lease.

RED first — stubbing admit_operation to return Ok(()):
assertion left == right failed: {"bytes_written":3,"success":true,"type":"write_ack"}
for the pty test and a failed message assertion for the portable test; both
pass once restored.

No new host-platform debt: the ledger/dylint baseline are byte-identical to
main
(rows=44), and the research inventory only shifts line numbers.

Real output

cargo test -p fbuild-daemon --lib            test result: ok. 307 passed; 0 failed; 3 ignored
cargo test -p fbuild-serial --lib            test result: ok. 203 passed; 0 failed; 1 ignored
cargo test -p fbuild-cli --bin fbuild        test result: ok. 472 passed; 0 failed; 0 ignored
cargo clippy -p fbuild-daemon -p fbuild-cli -p fbuild-serial --all-targets -- -D warnings
                                             Finished `dev` profile (no warnings)
cargo fmt --all --check                      rc 0
ci/enforce_platform_boundary.py --print-totals   rows=44; dylint_rows=43
ci/platform_boundary_research.py --check         rc 0
ci/check_test_layout.py                          ok: 8 integration-test categories
cargo run -p fbuild-cli --bin fbuild -- monitor --help   shows --lease-id <LEASE_ID>

Not in this PR (still open on #1864)

  • Opt-in bounded wait/queue with deterministic ordering, cancellation, and
    holder/progress diagnostics (criterion 3).
  • Lease identity across re-enumeration/recovery reattach.
  • The Python facade (fbuild-python has its own ClientMessage) cannot present
    a lease yet — it is correctly refused rather than admitted, which is the
    safe default; a lease_id kwarg is a separate additive change.
  • Two real checkouts taking turns on one board (criterion 6) — needs an idle
    board; two board-bearing builds were running during this work.

A foreign exclusive lease now refuses /ws/serial-monitor attach before the
port is opened, and every write frame is re-admitted so a lease taken by
another workflow mid-session stops the bytes. `fbuild monitor` gains
--lease-id for the holder. The protocol field is optional and omitted by
older clients, which present no lease (the safe default).

See #1864.
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

…dger

The host-platform ledger is a shrink-only ratchet (44 rows; adding rows fails
`test_committed_exact_occurrence_ledger_matches_whole_tree`), so the coverage
is restructured instead of baselined:

- new portable test `ws_serial_attach_is_refused_under_a_foreign_exclusive_lease`
  asserts the refusal (no lease) and that the holder clears admission, using a
  registered test device and `open_if_needed: false` so no tty is needed and no
  platform gate is introduced;
- the existing linux/pty WebSocket test gains the mid-session lease-steal
  assertion (a write after another workflow leases the port must come back
  `write_ack{success:false}`), reusing the session it already holds.

Net: 0 new ledger rows, 0 new libc:: occurrences; the ledger and dylint
baseline are byte-identical to main.
@zackees
zackees added this pull request to the merge queue Nov 3, 2026
Merged via the queue into main with commit 9f182e4 Nov 3, 2026
31 checks passed
@zackees
zackees deleted the agent/1864-lease-serial-write branch November 3, 2026 09:14
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

1 participant