rust runtime_error ai_generated true

线程'tokio-runtime-worker'在'不允许阻塞的上下文中无法丢弃运行时'处panic

thread 'tokio-runtime-worker' panicked at 'Cannot drop a runtime in a context where blocking is not allowed'

ID: rust/tokio-runtime-shutdown-timeout

其他格式: JSON · Markdown 中文 · English
82%修复率
87%置信度
1证据数
2024-01-15首次发现

版本兼容性

版本状态引入弃用备注
tokio 1.25.0 active
tokio 1.30.0 active
tokio 1.35.0 active

根因分析

在异步上下文(例如future或task)中尝试丢弃Tokio运行时,而该上下文禁止阻塞操作,通常是由于运行时嵌套或关闭不正确。

English

Attempting to drop a Tokio runtime inside an asynchronous context (e.g., inside a future or task) where blocking is prohibited, often due to incorrect runtime nesting or shutdown.

generic

官方文档

https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html

解决方案

  1. 确保运行时在任何异步上下文之外被丢弃,例如将其存储在结构体中,并在同步作用域中丢弃。
  2. 使用`tokio::runtime::Runtime::shutdown_timeout`在丢弃前优雅关闭。

无效尝试

常见但无效的做法:

  1. 90% 失败

    Forgetting the runtime leaks resources and doesn't fix the underlying issue; the panic still occurs on drop.

  2. 85% 失败

    The panic is caused by the drop logic, not by ownership; Arc doesn't prevent the drop from happening.

  3. 75% 失败

    `block_on` itself blocks, which may trigger the same panic if called from an async context.