Skip to content

fix(broker-v2): client_v2 hardening — deadline + retry_after_ms + RAII cleanup - #520

Merged
zackees merged 4 commits into
mainfrom
fix/broker-v2-hardening-rp517-518-519
Jun 20, 2026
Merged

zackees merged 4 commits into
mainfrom
fix/broker-v2-hardening-rp517-518-519

Conversation

@zackees

@zackees zackees commented Jun 20, 2026

Copy link
Copy Markdown
Owner

Closes #517, closes #518, closes #519.

Bundled into one PR because they touch a single file (`broker::client_v2`) and gate the downstream consumer's broker-v2 burn-down (zackees/zccache#842).

#517 — Hello round-trip had no read deadline

A v2 broker that accepts the dial then stalls (deadlock, GC pause, ENOSPC log write, hung backend resolver) would hang the caller forever. Mirror v1's `AsyncBrokerSession::adopt` pattern: helper thread + `mpsc::channel` + `recv_timeout`. New surface:

  • `DEFAULT_HELLO_DEADLINE` (3s, matches v1)
  • `pub fn connect_with_deadline(program, version, Duration)`
  • existing `connect` delegates to `connect_with_deadline(DEFAULT_HELLO_DEADLINE)`
  • private `connect_unbounded` is the actual work, called inside the helper thread

On deadline returns `BrokerV2Error::Io(ErrorKind::TimedOut)` so caller classification routes correctly. Test: `connect_with_deadline_fires_on_stalling_broker` spawns an accept-then-sleep broker and asserts the deadline fires within budget.

#518 — `BrokerV2Error::Refused` buried `retry_after_ms`

Previously: `Refused { reason, details: Box }`. Source-incompatible with v1's `BrokerClientError::Refused { code, reason, retry_after_ms }` — RateLimited callers couldn't honor broker-supplied backoff without threading the boxed prost payload back out.

Promote `retry_after_ms: u64` (matches the proto wire type) to a top-level field; keep `details` so future scalar / nested fields stay accessible without another API break. Test: `refused_exposes_retry_after_ms_top_level` asserts both top-level and `details.retry_after_ms` carry the broker's value.

#519 — `spawn_stub_broker` test helper leaked Unix socket on panic

Any panic between bind and the final explicit `remove_file` left a stale `.sock` file. Next test run got `EADDRINUSE` on bind or `ECONNREFUSED` on connect to the dead socket — masking the real failure.

Wrap the socket path in a `SocketCleanup` RAII guard that removes the file on `Drop` — fires on panic and on normal scope exit alike. The explicit `remove_file` is now redundant; removed.

Test plan

  • All four `broker::client_v2::tests` pass on Windows MSVC (2 existing + 2 new)
  • Downstream zccache compiles + 17 broker tests pass against this branch via local git file:// pin
  • CI workflow green across all matrix entries

Coordinated with

🤖 Generated with Claude Code

…I cleanup

Three independent bugs surfaced by the zccache#842 audit, all in
`broker::client_v2`. Bundled because they touch one file and gate the
zccache consumer's broker-v2 burn-down.

#517 — Hello round-trip had no read deadline. A v2 broker that
accepts the dial then stalls (deadlock, GC pause, ENOSPC log write,
hung backend resolver) would hang the caller forever. Mirror v1's
`AsyncBrokerSession::adopt` pattern: helper thread + mpsc::channel
+ recv_timeout. New surface:

  - `DEFAULT_HELLO_DEADLINE` (3s, matches v1)
  - `pub fn connect_with_deadline(program, version, Duration)`
  - existing `connect` delegates to `connect_with_deadline(DEFAULT_HELLO_DEADLINE)`
  - private `connect_unbounded` is the actual work, called inside the
    helper thread

On deadline returns `BrokerV2Error::Io(ErrorKind::TimedOut)` so
caller classification routes correctly. Test:
`connect_with_deadline_fires_on_stalling_broker` spawns an
accept-then-sleep broker and asserts the deadline fires within
budget.

#518 — `BrokerV2Error::Refused` buried `retry_after_ms` inside
`details: Box<Refused>`, breaking source-compat with v1's
`BrokerClientError::Refused { code, reason, retry_after_ms }`.
Callers couldn't honor broker-supplied backoff without threading
the boxed prost payload back out. Promote `retry_after_ms: u64`
(matches the proto wire type) to a top-level field; keep `details`
so future scalar / nested fields stay accessible without another
API break. Test: `refused_exposes_retry_after_ms_top_level` asserts
both `retry_after_ms` (top-level) and `details.retry_after_ms`
carry the broker's value.

#519 — `spawn_stub_broker` test helper leaked the Unix socket file
on any panic between bind and the final explicit `remove_file`.
Next test run hit `EADDRINUSE` on bind or `ECONNREFUSED` on connect
to the dead socket, masking the real failure. Wrap the socket path
in a `SocketCleanup` RAII guard that removes the file on Drop —
fires on panic and on normal scope exit alike. The explicit
`remove_file` is now redundant; removed.

All four `broker::client_v2::tests` pass (2 existing + 2 new) on
Windows MSVC.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@codecov

codecov Bot commented Jun 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.03797% with 22 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/running-process/src/broker/client_v2.rs 93.03% 22 Missing ⚠️

📢 Thoughts on this report? Let us know!

zackees and others added 2 commits June 20, 2026 10:19
Round 2 audit (downstream zccache#842) brainstormed 10 adversarial
tests; landing the 4 highest-value here.

  1. connect_rejects_hello_reply_with_missing_result_oneof
     Broker replies with HelloReply { result: None } (proto3 default).
     Must surface as BrokerV2Error::MissingResult, not silently mis-route
     as a successful session.

  2. connect_returns_err_on_premature_disconnect
     Broker accepts then immediately drops the stream. Must return a
     typed transport error (Framing/Io/Dial) and not hang past deadline.
     Exercises the helper-thread + recv_timeout path under realistic
     broker faults.

  3. connect_rejects_invalid_program_names_before_dial
     7 malformed program names (NUL injection, path traversal,
     uppercase, backslash, space, over-long, empty) all must be rejected
     as BrokerV2Error::PipeName before Stream::connect runs. Proves
     v2_program_pipe's validate_service_name is the front gate.

  4. refused_with_u64_max_retry_after_ms_round_trips
     u64::MAX in retry_after_ms survives the wire round-trip and the
     caller-side Duration::from_millis() does not overflow. Pins the
     contract for callers that compute backoff from the field.

Two new stub-broker helpers added (`spawn_missing_result_broker`,
`spawn_drop_on_accept_broker`) following the same SocketCleanup +
mpsc::channel ready-signal pattern.

All 8 client_v2 tests pass on Windows MSVC.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…cept stub

Stress round (downstream zccache#842): pins the contract that the
helper-thread + recv_timeout pattern scales to concurrent callers.
8 client threads dial simultaneously against a stub broker that
accepts and replies Negotiated to each. All must succeed within 5s.

If the round-1 fix #517 ever regresses (e.g. helper thread serializes
on a global mutex or channel), this test fails fast.

New helper: `spawn_multi_accept_stub_broker(socket_path, count)`
follows the same SocketCleanup + mpsc::channel ready-signal pattern
as the single-shot stub, but loops `count` times on accept.

All 9 client_v2 tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Trivial fix for -D warnings lint on `fix/broker-v2-hardening-rp517-518-519`:
inline the `if let Ok(session) = r` inside the for loop with
`results.iter().flatten()` per clippy::manual_flatten.

The cargo-fuzz failure on the same run is a transient network timeout
downloading nightly toolchain — not a code bug, retries on its own.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
zackees added a commit to forge-ci/zccache that referenced this pull request Jun 20, 2026
…ks CI (#851)

Main CI has been red since the cross-repo dev pin in zccache#842 landed a
\`file:///C:/Users/niteris/dev/zccache/.extern-repos/running-process\`
patch-pin into the workspace Cargo.toml. CI runners can't see that path,
so every Cargo build that resolves running-process aborts with:

  Updating git repository \`file:///C:/Users/niteris/dev/zccache/.extern-repos/running-process\`
  Unable to update file:///C:/Users/niteris/dev/zccache/.extern-repos/running-process#baf2d60b

Visible on the latest main push (PR #850's merge run 27879118164):
Integration / Perf Guard / Linux / macOS / Windows all failed at "Build
integration test binaries" with that exact error.

Fix: replace the file:// pin with the equivalent public git+rev on the
running-process \`fix/broker-v2-hardening-rp517-518-519\` branch (PR
zackees/running-process#520). The pinned commit ca0a7ad2 is the current
tip after the clippy manual_flatten fix landed (so PR #520 itself is
also unblocked on the lint front).

Iteration cycle is unchanged for the maintainer's local clone — bump
the rev to the new branch tip after pushing upstream.

Reverts to the published \`running-process = "4.5.6+"\` line once
upstream PR #520 merges and the next release ships.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees merged commit 502040a into main Jun 20, 2026
98 of 101 checks passed
@zackees
zackees deleted the fix/broker-v2-hardening-rp517-518-519 branch June 20, 2026 18:07
zackees added a commit that referenced this pull request Jun 20, 2026
…v2 extensions (#524)

Auto-Release will publish this version to crates.io + PyPI on push to main.

## Since 4.5.5

**Broker v2 client hardening (#520)** — closes #517, #518, #519
- \`DEFAULT_HELLO_DEADLINE\` + \`connect_with_deadline\` (3s; mirrors v1)
- \`BrokerV2Error::Refused\` promotes \`retry_after_ms\` to top-level
- \`SocketCleanup\` RAII guard on \`spawn_stub_broker\` test helper

**ServiceDefinition v2 schema extension (#521)**
- v2 envelope now carries v1's launcher/isolation field set
  (binary_path, isolation, explicit_instance, per_version_binary_dir,
  min_version, version_allow_list, labels) — unblocks consumer-side
  v2 migrations
- New \`BrokerIsolation\` enum mirrors v1's
- 3 new round-trip tests (every new field + every enum variant +
  explicit_instance survives all isolation modes)

**ServiceDefinition v2 consumer I/O (#522)**
- \`protocol_v2::io::ServiceDefinitionBuilder\` mirrors v1's builder API
- \`write_service_definition_v2\`, \`service_definition_dir_v2\`,
  \`SERVICE_DEF_V2_EXTENSION\` (\`.servicedef.v2\`)
- 11 new tests including full builder→install→decode round-trip

Unblocks downstream forge-ci/zccache#782 slices 21-25 (consumer-side
v1 → v2 migration burn-down) and forge-ci/zccache#840 (bincode wire
retirement, which independently needs these v2 surfaces to be stable
in a published release before zccache can drop its git+rev patch pin).

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant