Skip to content

Commit 511ec2e

Browse files
committed
fix(windows/lib.rs): use a single-threaded Tokio runtime to avoid new EnterGuard ordering issues
1 parent c945688 commit 511ec2e

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ static GLOBAL_CLIENT: OnceLock<Mutex<Option<McpClient>>> = OnceLock::new();
2323
/// Get or create the global Tokio runtime
2424
fn get_runtime() -> &'static tokio::runtime::Runtime {
2525
GLOBAL_RUNTIME.get_or_init(|| {
26-
tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime")
26+
// Create a simple single-threaded runtime to avoid Windows issues
27+
tokio::runtime::Builder::new_current_thread()
28+
.enable_all()
29+
.build()
30+
.expect("Failed to create Tokio runtime")
2731
})
2832
}
2933

@@ -151,11 +155,10 @@ pub extern "C" fn mcp_connect(
151155
};
152156

153157
let use_sse = legacy_sse != 0;
154-
let runtime = get_runtime();
155158

156159
let (result, maybe_service) = if use_sse {
157160
// Use SSE transport (legacy) with optional custom headers
158-
runtime.block_on(async {
161+
get_runtime().block_on(async {
159162
// Create HTTP client with optional custom headers
160163
let mut client_builder = reqwest::Client::builder();
161164
if let Some(ref headers_map) = headers_map {
@@ -241,7 +244,7 @@ pub extern "C" fn mcp_connect(
241244
})
242245
} else {
243246
// Use streamable HTTP transport (default) with optional custom headers
244-
runtime.block_on(async {
247+
get_runtime().block_on(async {
245248
// For Streamable HTTP, we need to extract the Authorization header specifically
246249
// since it has a dedicated field, and we'll use a custom HTTP client for other headers
247250
let auth_header_value = headers_map.as_ref().and_then(|m| m.get("Authorization")).map(|s| s.clone());
@@ -369,8 +372,7 @@ pub extern "C" fn mcp_list_tools(_client_ptr: *mut McpClient) -> *mut c_char {
369372
}
370373
};
371374

372-
let runtime = get_runtime();
373-
let result = runtime.block_on(async {
375+
let result = get_runtime().block_on(async {
374376
let service_guard = client.service.lock().unwrap();
375377
let service = match service_guard.as_ref() {
376378
Some(s) => s,
@@ -464,8 +466,7 @@ pub extern "C" fn mcp_call_tool(
464466
}
465467
};
466468

467-
let runtime = get_runtime();
468-
let result = runtime.block_on(async {
469+
let result = get_runtime().block_on(async {
469470
let service_guard = client.service.lock().unwrap();
470471
let service = match service_guard.as_ref() {
471472
Some(s) => s,

0 commit comments

Comments
 (0)