Skip to content

Conversation

@raine
Copy link

@raine raine commented Jan 30, 2026

Fixes #4040.

Problem

The rt() function creates a new Tokio runtime on each call, returns its Handle, and immediately drops the runtime. This causes panics during sync operations:

thread panicked at: A Tokio 1.x context was found, but it is being shutdown.

Solution

Use OnceLock to create a single static runtime that lives for the program's lifetime. Also use enable_all() to enable both time and IO features needed for sync operations.

Changes

// Before: runtime dropped immediately after getting handle
fn rt() -> tokio::runtime::Handle {
    tokio::runtime::Builder::new_current_thread()
        .build()
        .unwrap()
        .handle()
        .clone()
}

// After: static runtime lives for program lifetime
use std::sync::OnceLock;

static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();

fn rt() -> &'static tokio::runtime::Runtime {
    RUNTIME.get_or_init(|| {
        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap()
    })
}

Note: PR #4042 adds .enable_time() which addresses the "timers are disabled" error but doesn't fix the underlying runtime lifetime issue.

The runtime was being dropped immediately after getting its Handle,
causing panics during async sync operations. Use a static OnceLock
to keep the runtime alive for the program's lifetime.

Also enables all Tokio features (time, io) needed for sync.
@raine raine closed this Jan 30, 2026
@djmitche
Copy link
Collaborator

Oops, I just saw this after merging #4042. This looks like a better fix! Why close it?

@djmitche djmitche reopened this Jan 31, 2026
@djmitche djmitche self-requested a review January 31, 2026 14:36
@djmitche
Copy link
Collaborator

I will rebase and adopt this! (and figure out how to test this more effectively)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash when syncing against taskchampion-sync-server

2 participants