Skip to main content

spawn_host_with_channel

Function spawn_host_with_channel 

Source
pub fn spawn_host_with_channel<B: Body>(
    opts: HostOptions,
    shutdown: CancellationToken,
    body: B,
    tx: Sender<B::Cmd>,
    rx: Receiver<B::Cmd>,
) -> Result<HostHandle<B::Cmd>, SpawnError>
Expand description

Spawn a dedicated Commonware-runtime thread hosting body, using a caller-supplied (Sender, Receiver) pair instead of one spawn_host creates internally (INV-H6).

This is the entry point a multi-shard host needs: every shard’s Sender must exist, and be cloned into every OTHER shard’s Body, before any shard’s dedicated thread starts — a chicken-and-egg spawn_host’s internally-created channel cannot resolve on its own. The caller builds every channel up front (e.g. one mpsc::channel per shard), clones each Sender into whichever peer Bodys need it, then calls this once per shard with that shard’s own pair — tx is consumed into the returned HostHandle, exactly as it would be if spawn_host had built it.

opts.command_backlog is ignored here (the channel already exists); everything else — the ready handshake, the command loop, the post-cancel drain, Body::on_drain_complete, and the returned HostHandle’s drop-tx-then-join Drop (INV-H1) — is identical to spawn_host.

§Errors

Returns SpawnError::Open if Body::open reports a failure (INV-H2), or SpawnError::RuntimeStart if the dedicated thread panics before it can report either outcome.

§Panics

Panics if the OS refuses to spawn the dedicated thread (resource exhaustion).