Skip to content

Replace the Arc<OnceLock<Handle>> broadcaster/behaviour construction cycle #622

Description

@emlautarom1

Summary

QBFT wiring has a construction cycle: Consensus::new needs a broadcaster; the real broadcaster is the qbft p2p Handle; Behaviour::new needs Arc<Consensus>. It is broken with an Arc<OnceLock<Handle>> filled ~60 lines later, self-flagged with TODO: the Arc<OnceLock<Handle>> pattern is awkward; explore alternatives.

Fragility:

  • Between construction and set, the Consensus is live and any broadcast panics on .expect("qbft p2p handle initialized before broadcast") — the invariant is enforced only by source ordering and a comment.
  • A duplicate set is mapped to the unrelated Error::BehaviourClosed.
  • The pattern is duplicated verbatim in the consensus crate's test harness (p2p.rs#L1227-L1256), which the app comment cites as precedent — a refactor must land in both.

For context, the only other set-later OnceLock in production (P2PContext::local_peer_id) degrades gracefully (getter returns Option), so this is the one true contortion.

Proposed change

Break the cycle with channel indirection instead of late binding: the broadcaster closure owns an mpsc::Sender<BroadcastRequest> created before either component; the p2p behaviour (or a small forwarding task started once the Handle exists) drains the receiver. Messages sent before the behaviour is up queue instead of panicking, the OnceLock, the expect, and the misleading error all disappear, and the test harness uses the same shape.

An alternative — splitting Consensus construction from transport attachment — is already hinted at by the start must be called exactly once doc note; acceptable if the channel shape doesn't fit.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

rustPull requests that update rust code

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions