Skip to content

Commit 3fb2c05

Browse files
CherishCaiclaudeMoonshotAI Kimi-K2.5
committed
fix: use thread_local Tokio runtime to avoid concurrency risks
Replace global OnceLock<Runtime> with thread_local! { OnceCell<Runtime> }. Each OS thread now has its own Tokio current-thread runtime, preventing contention when multiple Python threads call sync client methods concurrently. Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: MoonshotAI Kimi-K2.5 <support@moonshot.cn>
1 parent e890ddb commit 3fb2c05

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

src/lib.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
use pyo3::prelude::*;
2-
use std::sync::OnceLock;
2+
use std::cell::OnceCell;
33

4-
/// Global Tokio runtime for blocking operations.
5-
static RT: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
6-
7-
/// Get or initialize the global Tokio runtime.
8-
pub fn get_runtime() -> &'static tokio::runtime::Runtime {
9-
RT.get_or_init(|| {
10-
tokio::runtime::Builder::new_current_thread()
11-
.enable_all()
12-
.build()
13-
.expect("Failed to create Tokio runtime")
14-
})
4+
// Thread-local Tokio runtime for blocking operations.
5+
// Each OS thread has its own runtime to avoid contention between Python threads.
6+
thread_local! {
7+
static RT: OnceCell<tokio::runtime::Runtime> = const { OnceCell::new() };
158
}
169

17-
/// Block on a future using the global Tokio runtime.
10+
/// Block on a future using the thread-local Tokio runtime.
1811
pub fn block_on<F>(future: F) -> F::Output
1912
where
2013
F: std::future::Future,
2114
{
22-
get_runtime().block_on(future)
15+
RT.with(|rt| {
16+
let runtime = rt.get_or_init(|| {
17+
tokio::runtime::Builder::new_current_thread()
18+
.enable_all()
19+
.build()
20+
.expect("Failed to create Tokio current-thread runtime")
21+
});
22+
runtime.block_on(future)
23+
})
2324
}
2425

2526
/// Formats the sum of two numbers as string.

0 commit comments

Comments
 (0)