{"record":{"id":"47ee65df4f0ceb5b","repo":"tonhowtf/omniget","slug":"failed-to-initialize-torrent-session","errorCode":null,"errorMessage":"Failed to initialize torrent session: {}","messagePattern":"Failed to initialize torrent session: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src-tauri/omniget-core/src/platforms/magnet.rs","lineNumber":153,"sourceCode":"                            Err(e) => {\n                                tracing::warn!(\n                                    \"[magnet] persistent DHT init failed ({}); retrying without DHT persistence\",\n                                    e\n                                );\n                                match Session::new_with_opts(output_pb.clone(), make_opts(true, false)).await {\n                                    Ok(s) => Ok(s),\n                                    Err(e2) => {\n                                        tracing::warn!(\n                                            \"[magnet] DHT init failed ({}); retrying with DHT disabled (trackers/PEX only)\",\n                                            e2\n                                        );\n                                        Session::new_with_opts(output_pb, make_opts(true, true)).await\n                                    }\n                                }\n                            }\n                        }\n                    } => {\n                        result.map_err(|e| anyhow::anyhow!(\"Failed to initialize torrent session: {}\", e))?\n                    }\n                    _ = cancel_rx.cancelled() => {\n                        anyhow::bail!(\"Download cancelled during session creation\");\n                    }\n                };\n                *guard = Some(s.clone());\n                *dir_guard = Some(output_dir.clone());\n                s\n            } else {\n                tracing::info!(\"[magnet] reusing existing session\");\n                guard.as_ref().unwrap().clone()\n            }\n        };\n\n        let add_torrent = if url.starts_with(\"magnet:\")\n            || url.starts_with(\"http://\")\n            || url.starts_with(\"https://\")\n        {","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/omniget-core/src/platforms/magnet.rs#L135-L171","documentation":"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 `{}`.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the wrapped inner error (`{}` payload) — it names the actual libtorrent failure and drives the real fix.","Check that the output path (output_pb) exists and is writable, and that no other process holds the same listen port.","Reuse a single shared Session instead of creating one per download to avoid resource exhaustion.","Verify the libtorrent dependency/version matches the Session API used (new_with_opts) and is correctly built for the target platform.","Retry session creation with backoff if the failure is transient (e.g. temporary port contention)."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Rust: probe writability of the output dir and session preconditions before download\nlet out = std::path::Path::new(&opts.output_dir);\nif !out.exists() { std::fs::create_dir_all(out)?; }\nlet probe = out.join(\".omniget_probe\");\nstd::fs::write(&probe, b\"ok\").map_err(|e| anyhow!(\"Output dir not writable: {}\", e))?;\nlet _ = std::fs::remove_file(&probe);","typeGuard":null,"tryCatchPattern":"match download(url, opts).await {\n    Err(e) if e.to_string().contains(\"Failed to initialize torrent session\") => {\n        // transient session bootstrap failure; retry after delay, then surface inner error\n        tokio::time::sleep(Duration::from_secs(3)).await;\n        download(url, opts).await\n    }\n    other => other,\n}","preventionTips":["Reuse one shared libtorrent Session across downloads instead of per-download sessions","Check listen port availability and avoid conflicts between app instances","Keep the libtorrent dependency version matched to the Session API","Always log the wrapped inner error — it names the real cause","Run downloads with a writable output directory confirmed at startup"],"tags":["torrent","libtorrent","session-init","async"],"backgroundTag":"module-init-failed","analyzedSha":"8600b91f4246848bac346874daa9e61c1fc5677a","analyzedAt":"2026-09-12T14:29:19.317Z","contentChangedAt":"2026-09-12T14:29:19.317Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}