python runtime_error ai_generated true

asyncio 异常:Future 已解决

asyncio.exceptions.InvalidStateError: Future is already resolved

ID: python/asyncio-future-invalid-state

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2025-12-01首次发现

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active

根因分析

尝试在已解决(已完成或已取消)的 Future 上设置结果或异常。

English

Attempting to set a result or exception on a Future that has already been resolved (completed or cancelled).

generic

解决方案

  1. 90% 成功率 Check if the future is done before setting result
    if not future.done():
        future.set_result(value)
  2. 85% 成功率 Use asyncio.Future with proper lifecycle management
    future = asyncio.get_event_loop().create_future()
    # ensure only one set_result call

无效尝试

常见但无效的做法:

  1. Checking if the future is done before setting result 50% 失败

    If done, setting result is still invalid; this may cause the error if not handled properly.

  2. Using a new Future for each operation 40% 失败

    This may lead to resource leaks if old futures are not cleaned up.