nautechsystems/nautilus_trader · error

Binance Spot public JSON stream pool shutdown began during c

Error message

Binance Spot public JSON stream pool shutdown began during connect

What it means

During connect() on the Binance Spot public JSON WebSocket stream pool, the pool's shutdown flag was set while the connect sequence was in progress. The method detects this race, rolls back by closing any connections it had opened, and fails with this error rather than returning a half-connected pool. It is a deliberate invalid-state guard, not a network failure.

Source

Thrown at crates/adapters/binance/src/spot/websocket/public_json/client.rs:204

            self.signal.store(false, Ordering::Release);
        }

        let (out_tx, out_rx) = tokio::sync::mpsc::unbounded_channel();
        *self.out_tx.lock() = Some(out_tx);
        *self.out_rx.lock() = Some(out_rx);

        let slot = self.create_connection(0).await?;
        let shutdown = {
            let mut slots = self.slots.lock();
            let shutdown = self.signal.load(Ordering::Acquire);
            slots.push(slot);
            shutdown
        };

        if shutdown {
            let rollback = self.close_connections().await;
            return Err(match rollback {
                Ok(()) => anyhow::anyhow!(
                    "Binance Spot public JSON stream pool shutdown began during connect"
                ),
                Err(e) => anyhow::anyhow!(
                    "Binance Spot public JSON stream pool shutdown began during connect; rollback failed: {e}"
                ),
            });
        }

        log::debug!(
            "Connected to Binance Spot public JSON stream pool: url={}",
            self.url
        );
        Ok(())
    }

    /// Closes all WebSocket connections and tasks.
    ///
    /// # Errors

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Serialize lifecycle calls: do not call connect() after or concurrently with shutdown(); await the shutdown before tearing down.
  2. Treat this error as terminal for the pool — create a new stream pool instance instead of retrying connect on the same one.
  3. If reconnect logic is in use, check the shutdown flag before each reconnect attempt and bail out when the pool is shutting down.
  4. If the rollback-failed variant appears, also investigate why close_connections() errored (stuck sockets/tasks) during teardown.

Example fix

// before
pool.connect().await?; // may race shutdown
// after
if pool.is_shutdown() {
    return Ok(()); // pool terminated; do not reconnect
}
pool.connect().await?;
Defensive patterns

Strategy: type-guard

Validate before calling

// before connecting, check lifecycle state
fn can_connect(shutdown: std::sync::atomic::AtomicBool) -> bool {
    !shutdown.load(std::sync::atomic::Ordering::Acquire)
}

Type guard

fn pool_is_live(pool: &StreamPool) -> bool {
    !pool.is_shutdown()
}

Try / catch

match pool.connect().await {
    Ok(()) => { /* stream */ }
    Err(e) if e.to_string().contains("shutdown began during connect") => {
        // pool terminated: do not retry this instance; recreate pool if streaming is still wanted
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling connect() on the public JSON stream pool concurrently with (or immediately before) shutdown() / pool teardown — the shutdown flag flips between the connect start and the shutdown check at client.rs:204, including the rollback-failed variant when close_connections() also errors.

Common situations: Stop/teardown of the node or adapter racing a (re)connect attempt from another task; caller initiating connect after initiating shutdown on a reconnect path; a shutdown triggered by an unrelated fault arriving while a reconnect loop is dialing.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/b2861578860bb748. Report an issue: GitHub.