Skip to content

fix: use static tokio runtime to prevent shutdown during sync#4044

Merged
djmitche merged 2 commits intoGothenburgBitFactory:developfrom
raine:fix-tokio-runtime
Feb 1, 2026
Merged

fix: use static tokio runtime to prevent shutdown during sync#4044
djmitche merged 2 commits intoGothenburgBitFactory:developfrom
raine:fix-tokio-runtime

Conversation

@raine
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
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
Copy Markdown
Collaborator

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

@djmitche djmitche marked this pull request as ready for review February 1, 2026 22:32
@djmitche
Copy link
Copy Markdown
Collaborator

djmitche commented Feb 1, 2026

I confirmed that with this change applied my local GCP sync works fine.

Copy link
Copy Markdown
Collaborator

@djmitche djmitche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@djmitche djmitche merged commit 87a05de into GothenburgBitFactory:develop Feb 1, 2026
16 checks passed
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