gitbutlerapp/gitbutler · error

Failed to create tokio runtime

Error message

Failed to create tokio runtime: {err}

What it means

ci_checks_for_ref spawns a dedicated std::thread to run async GitLab CI queries, and inside that thread creates a fresh tokio Runtime. If tokio cannot build a runtime (typically an invalid/feature-limited build, exhausted resources, or a runtime-incompatible environment) this anyhow error is returned. It surfaces before any GitLab API call is attempted.

Solutions

  1. Rebuild with tokio enabled with required features (rt-multi-thread, macros): check Cargo feature flags
  2. Check system thread/memory limits (ulimit -u, cgroup limits) and raise them
  3. Refactor to use an existing shared tokio runtime or a runtime handle instead of constructing a new Runtime per thread

Example fix

// before
let runtime = tokio::runtime::Runtime::new()
    .map_err(|err| anyhow::anyhow!("Failed to create tokio runtime: {err}"))?;
// after
let runtime = HANDLE.get_or_try_init(tokio::runtime::Runtime::new)?; // shared lazily-created runtime
Defensive patterns

Strategy: retry

Try / catch

match fetch_and_refresh_cache(...).await {
    Err(e) if e.to_string().contains("Failed to create tokio runtime") => {
        eprintln!("tokio runtime unavailable: {e:#}");
        Err(e)
    }
    other => other,
}

Prevention

When it happens

Trigger: Calling ci_checks_for_ref (via fetch_and_refresh_cache) for a GitLab reference when tokio::runtime::Runtime::new() fails — e.g. binary built without needed tokio features, extreme resource limits (thread/memory exhaustion), or incompatible runtime environment.

Common situations: Restricted containers with low thread limits; misbuilt binaries (tokio features disabled); heavily loaded CI machines where thread creation fails.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/1ce012f4da3ed9bc. Report an issue: GitHub.

Appendix: source

Thrown at crates/but-forge/src/ci.rs:145

                        })
                        .collect()
                })
            })
        }
        ForgeName::GitLab => {
            let preferred_account = preferred_forge_user
                .as_ref()
                .and_then(|user| user.gitlab().cloned());

            // Clone owned data for thread
            let project_id = but_gitlab::GitLabProjectId::new(owner, repo);
            let storage = storage.clone();
            let reference = reference.to_string();
            let reference_for_checks = reference.clone();

            let pipelines = std::thread::spawn(move || -> anyhow::Result<_> {
                let runtime = tokio::runtime::Runtime::new()
                    .map_err(|err| anyhow::anyhow!("Failed to create tokio runtime: {err}"))?;
                runtime.block_on(but_gitlab::checks::list_pipeline_jobs_for_ref(
                    preferred_account.as_ref(),
                    project_id,
                    &reference,
                    &storage,
                ))
            })
            .join()
            .map_err(|e| anyhow::anyhow!("Failed to join thread: {e:?}"))??;
            Ok(Some(
                pipelines
                    .into_iter()
                    .map(|pipeline| {
                        let mut ci_check = CiCheck::from(pipeline);
                        ci_check.reference = reference_for_checks.to_string();
                        ci_check
                    })
                    .collect(),

View on GitHub (pinned to 58e5313667)