Skip to content

fix(gossip): swap publisher's sender on subscriber reconnect (#577 H2) - #657

Merged
thiras merged 2 commits into
mainfrom
fix/577-h2-publisher-reconnect
May 21, 2026
Merged

thiras merged 2 commits into
mainfrom
fix/577-h2-publisher-reconnect

Conversation

@thiras

@thiras thiras commented May 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Closes Protocol/gossip robustness & DoS hardening (peer-table cap, reconnect sender, frame padding, I/O classification) #577 H2. subscriber_task's reconnect arm used to discard the fresh GossipSender from topic.split() while publisher_task kept broadcasting through the senders captured at spawn. Once the iroh-gossip-side receiver for the original sender was gone, sender.broadcast(...) still returned Ok (the local mpsc::Sender was alive) but no message landed — manual decdn node announce and interval-driven publishes silently stopped after any stream reset.
  • Each topic now owns one Arc<ArcSwap<GossipSender>> slot shared by the subscriber and the publisher. Subscriber's reconnect stores the fresh sender; publisher reads through slot.load_full() per iteration so the swap is observed without restarting.
  • GossipSender is Clone (verified in iroh-gossip 0.98 — wraps an mpsc::Sender<Command>); arc-swap was already a workspace dep (used in the dispatch hot-reload path), added to crates/gossip/Cargo.toml.

Design notes

  • ArcSwap over tokio::sync::watch: watch's Sender lives in the subscriber task; if that task died the publisher would see a poisoned receiver and have to handle it. ArcSwap stays valid as long as any Arc is alive.
  • ArcSwap over RwLock<HashMap<...>>: per-topic slots are lock-free reads; a shared map would serialise publisher iteration across topics and contend with subscribers on every broadcast.

Test plan

  • publisher_sees_swapped_sender_after_reconnect (new) pins the architectural property the fix relies on: the publisher reads through the slot, so a store from another task is visible on the next read. Test uses a stand-in &'static str because GossipSender can't be easily faked without spinning up iroh_gossip::net::Gossip.
  • cargo nextest run -p decdn-gossip — 37/37 pass.
  • cargo nextest run --workspace — 931/931 pass.
  • cargo clippy --workspace --all-targets -- -D warnings — clean.
  • cargo fmt --check — clean.
  • pre-commit run — clean on changed files.

A true end-to-end reconnect-publish integration test would spin up two iroh_gossip::net::Gossip instances, force a stream reset, and verify the next publish lands. That's out of scope here — no existing harness, and the swap mechanism plus the visible single-line sender_slot.store(...) call at the reconnect site is the entire fix. The reconnect-arm doc comment names the regression test, so a future change that drops the store is immediately visible against the named contract.

Still open under #577: M3 trailing-byte padding cap, M1 probe handler I/O classification.

🤖 Generated with Claude Code

Closes the publisher/subscriber desync flagged in #577 H2. Previously
`subscriber_task`'s reconnect arm discarded the fresh `GossipSender`
from `topic.split()` and `publisher_task` kept broadcasting through
the senders captured at spawn. Once the iroh-gossip-side receiver for
the original sender was gone, `sender.broadcast(...)` still returned
`Ok` (the local `mpsc::Sender` was alive) but no message landed —
manual `decdn node announce` and interval-driven publishes silently
stopped after any stream reset.

Each topic now owns one `Arc<ArcSwap<GossipSender>>` slot shared by
the subscriber and the publisher:

  * The subscriber's reconnect arm stores the fresh sender into its
    slot before resuming.
  * The publisher's broadcast loop calls `slot.load_full()` per
    iteration so the swap is observed without a publisher restart.

`GossipSender` is `Clone` (verified in iroh-gossip 0.98 — wraps an
`mpsc::Sender<Command>`); `arc-swap` is already a workspace dep,
added to `crates/gossip/Cargo.toml`. ArcSwap was chosen over
`tokio::sync::watch` (subscriber-task lifetime would poison the
publisher's receiver) and `RwLock<HashMap<...>>` (would serialise
across topics on every broadcast).

A new `publisher_sees_swapped_sender_after_reconnect` test pins the
architectural property the fix relies on: the publisher reads
through the slot, so a `store` from another task is visible on the
next read. The test uses a stand-in `&'static str` value because
`GossipSender` can't be easily faked without spinning up a real
`iroh_gossip::net::Gossip` instance; a true end-to-end reconnect-
publish integration test would warrant its own follow-up PR — the
swap mechanism plus the visible single-line `store` call at the
reconnect site is the entire fix.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 21, 2026 22:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes a gossip publish regression where, after a subscriber reconnect (stream reset + re-subscribe), the publisher could keep broadcasting via a stale GossipSender whose iroh-gossip-side receiver had been dropped—causing manual and periodic announces to stop landing without errors.

Changes:

  • Introduces a per-topic shared Arc<ArcSwap<GossipSender>> sender slot so subscriber reconnects can swap in a fresh sender and the publisher observes it on the next broadcast iteration.
  • Updates subscriber reconnect logic to store() the newly-split sender into the shared slot.
  • Adds a focused unit test to lock in the “publisher reads through slot; subscriber swap is visible next iteration” architectural property.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.

File Description
crates/gossip/src/service.rs Shares per-topic sender state via ArcSwap and updates reconnect/publish loops; adds a regression-style unit test for the swap visibility contract.
crates/gossip/Cargo.toml Adds arc-swap as a crate dependency (already present in the workspace).
Cargo.lock Records the arc-swap dependency for decdn-gossip.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the arc-swap crate to synchronize GossipSender instances between subscriber and publisher tasks. By wrapping the sender in an ArcSwap slot, the publisher can now dynamically load the most recent sender on each iteration, ensuring it does not broadcast through stale senders after a stream reset and reconnection. A new unit test was added to verify this behavior. I have no feedback to provide as there are no review comments.

@github-actions

github-actions Bot commented May 21, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 90.04% (was 90.11%, ▼ 0.07%) — 17034/18918 lines

File Coverage Lines Δ
crates/gossip/src/service.rs 41.42% 140/338 ▼ 0.48%
crates/gossip/src/validation.rs 100.00% 252/252 ● 0.00%
History
Commit Total Δ vs base
ca68630 90.04% ▼ 0.07%
c2d6a4f (HEAD) 90.04% ▼ 0.07%

…est (#577 H2 review)

Follow-up on PR #657 review (type-design-analyzer + comment-analyzer +
silent-failure-hunter findings):

- Introduce private `TopicWiring` struct bundling
  (name, id, receiver, sender_slot). `subscriber_task` now takes the
  struct instead of 8 positional args, dropping the
  `#[allow(clippy::too_many_arguments)]` exemption and making the
  "subscriber and publisher share one slot per topic" invariant
  single-site: both vecs derive from the same `Arc::clone(&slot)` at
  `topic.split()` rather than parallel pushes that must agree by index.

- Add `RESUBSCRIBE_FAILED_LABEL = "resubscribe_failed"` and bump it via
  `metrics.inc_rejected(...)` in the reconnect Err arm alongside the
  existing `tracing::error!`. Pre-fix this path was log-only —
  diagnosable but not operationally observable. Operators can now alert
  on a node stuck in a reconnect loop without scraping logs. Label
  pinned in the `reject_labels_are_stable` test.

- Tighten the `publisher_sees_swapped_sender_after_reconnect` test
  doc-comment: drop the "diff sites are short and visible by
  inspection" line (over-promised; future edits push the sites apart)
  and the trailing "Sanity:" paragraph + `assert_eq!(*first, "initial")`
  (was testing `arc-swap`'s own contract, not deCDN code). Replace
  with a sober note that a true end-to-end test requires a live
  `Gossip` instance and is tracked separately.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@thiras
thiras merged commit 73c1418 into main May 21, 2026
21 checks passed
@thiras
thiras deleted the fix/577-h2-publisher-reconnect branch May 21, 2026 22:44
thiras added a commit that referenced this pull request May 21, 2026
…M1) (#659)

Closes #577 M3 + M1, the two remaining items from the original review (H2/H3 already shipped in #656/#657).

Four commits squashed:
- M3: validation.rs trailing-bytes cap (`MAX_TRAILING_BYTES = 4 KiB`, `OversizeTrailingBytes` variant + `oversize_trailing_bytes` label, boundary pair, sig-ordering test)
- M1: probe.rs `FrameError::Io → APP_ERR_NO_ERROR` (split out of MALFORMED), named const, truncation integration test
- ADR 013: §Application Error Codes table rewritten to match new Io→0x00 mapping (Copilot review)
- Forensic `tracing::warn!` at service-layer caller for `OversizeTrailingBytes` only, doc tightenings (5 review-followup items)

Detection-only on the M3 side: iroh-gossip 0.98 doesn't expose a per-subscription max-message knob, so the upstream 16 MiB allocation happens before validation. Tracking the deeper fix (per-ALPN frame ceiling) at #660.

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

Development

Successfully merging this pull request may close these issues.

Protocol/gossip robustness & DoS hardening (peer-table cap, reconnect sender, frame padding, I/O classification)

2 participants