# asyncio 异常：Future 已解决

- **ID:** `python/asyncio-future-invalid-state`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |

## 解决方案

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

## 无效尝试

- **Checking if the future is done before setting result** — If done, setting result is still invalid; this may cause the error if not handled properly. (50% 失败率)
- **Using a new Future for each operation** — This may lead to resource leaks if old futures are not cleaned up. (40% 失败率)
