tonhowtf/omniget · critical

Failed to initialize torrent session

Error message

Failed to initialize torrent session: {}

What it means

During magnet/torrent download, the libtorrent Session is created inside a tokio::select! racing session initialization against the cancel token. When the initialization future completes with an error (session creation via Session::new_with_opts failed), this wrapper error is raised, embedding the underlying libtorrent error via `{}`.

Solutions

  1. Read the wrapped inner error (`{}` payload) — it names the actual libtorrent failure and drives the real fix.
  2. Check that the output path (output_pb) exists and is writable, and that no other process holds the same listen port.
  3. Reuse a single shared Session instead of creating one per download to avoid resource exhaustion.
  4. Verify the libtorrent dependency/version matches the Session API used (new_with_opts) and is correctly built for the target platform.
  5. Retry session creation with backoff if the failure is transient (e.g. temporary port contention).
Defensive patterns

Strategy: retry

Validate before calling

// Rust: probe writability of the output dir and session preconditions before download
let out = std::path::Path::new(&opts.output_dir);
if !out.exists() { std::fs::create_dir_all(out)?; }
let probe = out.join(".omniget_probe");
std::fs::write(&probe, b"ok").map_err(|e| anyhow!("Output dir not writable: {}", e))?;
let _ = std::fs::remove_file(&probe);

Try / catch

match download(url, opts).await {
    Err(e) if e.to_string().contains("Failed to initialize torrent session") => {
        // transient session bootstrap failure; retry after delay, then surface inner error
        tokio::time::sleep(Duration::from_secs(3)).await;
        download(url, opts).await
    }
    other => other,
}

Prevention

When it happens

Trigger: Calling download for a magnet/.torrent URL when Session::new_with_opts fails — e.g. libtorrent cannot allocate the session, invalid session options, DHT/listen-port setup failure, or missing/broken libtorrent runtime.

Common situations: Corrupt or incompatible libtorrent build; port already bound by another session/instance; sandboxed environment blocking the listen socket; out-of-memory during session bootstrap; too many concurrent downloads each creating sessions.

Related errors


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

Appendix: source

Thrown at src-tauri/omniget-core/src/platforms/magnet.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)