nautechsystems/nautilus_trader · error

Failed to send query add_position to database message handle

Error message

Failed to send query add_position to database message handler: {e}

What it means

Thrown by `add_position` when the `DatabaseQuery::AddPosition` message (position id plus last event) cannot be sent over the mpsc channel because the database message-handler task was dropped. The position is not persisted. `position_last_event(position)` runs first, so SendError is the sole source of this message.

Source

Thrown at crates/infrastructure/src/sql/cache.rs:968

        self.tx.send(query).map_err(|e| {
            anyhow::anyhow!("Failed to send query add_order to database message handler: {e}")
        })
    }

    fn add_order_snapshot(&self, snapshot: &OrderSnapshot) -> anyhow::Result<()> {
        let query = DatabaseQuery::AddOrderSnapshot(snapshot.to_owned());
        self.tx.send(query).map_err(|e| {
            anyhow::anyhow!(
                "Failed to send query add_order_snapshot to database message handler: {e}"
            )
        })
    }

    fn add_position(&self, position: &Position) -> anyhow::Result<()> {
        let event = position_last_event(position)?;
        let query = DatabaseQuery::AddPosition(position.id, event);
        self.tx.send(query).map_err(|e| {
            anyhow::anyhow!("Failed to send query add_position to database message handler: {e}")
        })
    }

    fn add_position_snapshot(&self, snapshot: &PositionSnapshot) -> anyhow::Result<()> {
        let query = DatabaseQuery::AddPositionSnapshot(snapshot.to_owned());
        self.tx.send(query).map_err(|e| {
            anyhow::anyhow!(
                "Failed to send query add_position_snapshot to database message handler: {e}"
            )
        })
    }

    fn add_order_book(&self, _order_book: &OrderBook) -> anyhow::Result<()> {
        todo!()
    }

    fn add_quote(&self, quote: &QuoteTick) -> anyhow::Result<()> {
        let query = DatabaseQuery::AddQuote(quote.to_owned());

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Verify the adapter's message-handler task is running before writing positions.
  2. Inspect logs for receiver-task panics.
  3. Reconnect/restart the adapter and retry.
  4. Fix shutdown ordering so position persistence completes before teardown.
Defensive patterns

Strategy: try-catch

Validate before calling

if not cache_db_adapter.is_running:
    raise RuntimeError("Cannot add_position: adapter not running")

Try / catch

try:
    cache.add_position(position)
except RuntimeError as e:
    logger.error("Position persistence failed (writer stopped): %s", e)

Prevention

When it happens

Trigger: Calling `add_position` when the writer task exited or panicked — adapter stopped, connection task dropped, or process shutting down.

Common situations: Persisting position updates after adapter disconnect in live trading; panic in the SQL writer thread.

Related errors


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