python
runtime_error
ai_generated
true
asyncio.exceptions.InvalidStateError: Future is already resolved
ID: python/asyncio-future-invalid-state
80%Fix Rate
84%Confidence
0Evidence
2025-12-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
Root Cause
Attempting to set a result or exception on a Future that has already been resolved (completed or cancelled).
generic中文
尝试在已解决(已完成或已取消)的 Future 上设置结果或异常。
Workarounds
-
90% success Check if the future is done before setting result
if not future.done(): future.set_result(value) -
85% success Use asyncio.Future with proper lifecycle management
future = asyncio.get_event_loop().create_future() # ensure only one set_result call
Dead Ends
Common approaches that don't work:
-
Checking if the future is done before setting result
50% fail
If done, setting result is still invalid; this may cause the error if not handled properly.
-
Using a new Future for each operation
40% fail
This may lead to resource leaks if old futures are not cleaned up.