tracel-ai/burn · error

writer is set by ensure_connected

Error message

writer is set by ensure_connected

What it means

`RemoteService::flush` guarantees `self.writer` is `Some` by calling `ensure_connected()` immediately before; this expect asserts that invariant. Failing here means an internal invariant broke — connection establishment reported success (or panicked silently) without installing a writer — and is not an expected user-facing failure path.

Source

Thrown at crates/burn-remote/src/client/service.rs:658

        let _ = self.settings.set(connected.settings);
        let _ = self.device_count.set(connected.device_count);
        self.writer = Some(connected.writer);
    }

    /// Hand whatever's currently buffered to the writer task as one batch (the writer
    /// serializes it off the runner thread). No-op when the buffer is empty; otherwise opens
    /// the connection first if it isn't already up.
    pub fn flush(&mut self) {
        if self.batch.is_empty() {
            return;
        }
        self.ensure_connected();
        log::trace!("Flush session: {}", self.session_id);
        let batch = self.batch.take();
        let writer = self
            .writer
            .as_ref()
            .expect("writer is set by ensure_connected");
        writer.send(&self.executor, batch);
    }
}

impl Drop for RemoteService {
    fn drop(&mut self) {
        if self.closed {
            return;
        }
        self.closed = true;

        // If we never connected, there's no server-side session to close and no writer to
        // drain — whatever was buffered never had a connection to go out on, so just drop it.
        if self.writer.is_none() {
            return;
        }

        // Best-effort teardown: append Close to whatever's still buffered and let the

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. On wasm, make sure you awaited `RemoteDevice::connect_async(...)` before issuing tensor operations so the session is installed.
  2. Use an unmodified release of burn/burn-remote; if you forked, re-check that ensure_connected always sets `self.writer` before returning.
  3. Update to the latest burn version — if this reproduces on stock code it is a bug worth reporting with a minimal reproduction.
  4. Audit for concurrent access to the same RemoteService/RemoteDevice from multiple threads without synchronization.

Example fix

// before (wasm): operating before connection installed
let device = RemoteDevice::new(&endpoint);
let tensor = Tensor::<Remote<Wgsl>, 1>::from_data(...); // flush -> panic
// after
let device = RemoteDevice::connect_async(&endpoint).await; // installs writer
let tensor = Tensor::<Remote<Wgsl>, 1>::from_data(...);
Defensive patterns

Strategy: try-catch

Validate before calling

// On wasm, ensure the connection exists before any tensor op triggers flush:
// (pseudo-check available to callers)
async fn ensure_ready(device: &RemoteDevice) {
    // connect_async installs the writer; without it, ops on wasm fail
    let _ = RemoteDevice::connect_async(device.endpoint()).await;
}

Try / catch

// Wrap session start so the invariant panic is caught and reported:
let svc = std::panic::catch_unwind(|| service.flush());
if svc.is_err() {
    eprintln!("flush before connection installed; call connect first / update burn");
}

Prevention

When it happens

Trigger: Only reachable if `ensure_connected` fails to actually set `self.writer` (e.g. a bug in ensure_connected on wasm paths where connect is deferred, or a fork modified the connect logic); in a stock build it should be unreachable.

Common situations: Running a patched fork of burn-remote where ensure_connected was changed; wasm usage where the async connect (`RemoteDevice::connect_async`) was skipped or its result not installed via `wasm_install`; race conditions from custom concurrent callers of flush.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/3f5d20a32afa31cc. Report an issue: GitHub.