tonhowtf/omniget · critical

Failed to initialize torrent session: {}

Error message

Failed to initialize torrent session: {}

What it means

Wraps any failure from the `libtorrent` (rust libtorrent / Session) async session-creation race in `download`. The `select!` block races session construction (possibly with fallback option permutations) against the cancel channel; a non-OK result is remapped to this message with the underlying libtorrent error interpolated. It means no torrent session could be established, so the download never starts.

Solutions

  1. Read the wrapped `{}` payload — the inner libtorrent error identifies the real cause (filesystem, settings, or native library)
  2. Verify the output directory exists and is writable; clear stale `.pb`/resume state files for this torrent
  3. Check session options (ports, listen interfaces, encryption settings) passed via `make_opts` are valid for the environment
  4. Ensure the libtorrent native library matches the platform/arch of the build and retry after network stack changes

Example fix

// before
let session = init_session(&opts).await?;
// after
let session = match init_session(&opts).await {
    Ok(s) => s,
    Err(e) => {
        tracing::error!("torrent session init failed: {e:#}");
        std::fs::remove_dir_all(&stale_session_state).ok();
        init_session(&opts).await.context("retry after clearing session state")?
    }
};
Defensive patterns

Strategy: try-catch

Validate before calling

// before download: validate session prerequisites
fn validate_torrent_env(output_dir: &std::path::Path) -> Result<(), String> {
    if !output_dir.is_dir() { return Err(format!("missing dir: {}", output_dir.display())); }
    let probe = output_dir.join(".write_probe");
    std::fs::write(&probe, b"x").map_err(|e| e.to_string())?;
    std::fs::remove_file(&probe).map_err(|e| e.to_string())?;
    Ok(())
}

Try / catch

match result {
    Err(e) if e.to_string().starts_with("Failed to initialize torrent session") => {
        // e's inner payload carries the libtorrent cause
        tracing::error!("session init: {e:#}");
        clear_stale_session_state(&output_pb);
        retry_init_with_default_opts().await
    }
    other => other,
}

Prevention

When it happens

Trigger: `Session::new_with_opts(output_pb, make_opts(true, true))` (or its retry variants) returns Err — e.g. the output path's .pb/resume state is corrupt or unwritable, no usable network interfaces, libtorrent build/runtime mismatch, or invalid session settings.

Common situations: Unwritable or nonexistent download directory; corrupted libtorrent session state file from a crashed prior run; port/interface configuration that libtorrent rejects; musl/glibc mismatch in the bundled libtorrent native library.

Related errors


AI-assisted analysis of tonhowtf/omniget@8600b91f42 (2026-09-12). Data as JSON: /api/errors/96da57cd4fc01522. Report an issue: GitHub.

Appendix: source

Thrown at src-tauri/src/platforms/magnet/mod.rs:153

                            Err(e) => {
                                tracing::warn!(
                                    "[magnet] persistent DHT init failed ({}); retrying without DHT persistence",
                                    e
                                );
                                match Session::new_with_opts(output_pb.clone(), make_opts(true, false)).await {
                                    Ok(s) => Ok(s),
                                    Err(e2) => {
                                        tracing::warn!(
                                            "[magnet] DHT init failed ({}); retrying with DHT disabled (trackers/PEX only)",
                                            e2
                                        );
                                        Session::new_with_opts(output_pb, make_opts(true, true)).await
                                    }
                                }
                            }
                        }
                    } => {
                        result.map_err(|e| anyhow::anyhow!("Failed to initialize torrent session: {}", e))?
                    }
                    _ = cancel_rx.cancelled() => {
                        anyhow::bail!("Download cancelled during session creation");
                    }
                };
                *guard = Some(s.clone());
                *dir_guard = Some(output_dir.clone());
                s
            } else {
                tracing::info!("[magnet] reusing existing session");
                guard.as_ref().unwrap().clone()
            }
        };

        let add_torrent = if url.starts_with("magnet:")
            || url.starts_with("http://")
            || url.starts_with("https://")
        {

View on GitHub (pinned to 8600b91f42)