Calling an async command from an active event loop raises RuntimeError and leaves an unawaited coroutine
評估
研究方向
從 fire/core.py 中的 _CallAndUpdateTrace 開始,issue 指出 coroutine 會在此處傳遞給 loop.run_until_complete;將這條路徑與 fire/fire_test.py 中現有的 testFireAsyncio 測試進行比較。新增一個迴歸測試,在作用中的事件迴圈內呼叫 fire.Fire,並確認它會產生文件所述的行為,且不會發出未等待 coroutine 的警告。issue 建議在 docs/guide.md 中記錄支援的行為。
由索引模型根據 Issue 內容生成。
描述
Bug
Calling an async command through fire.Fire from code that already has a running asyncio event loop raises RuntimeError: This event loop is already running and emits a warning that the command coroutine was never awaited. The same command works when called from synchronous code.
Reproduction
On master at 716bbc23d7eca949fdb682172283c8d18f742cb6, run:
import asyncio
import fire
async def target():
return 42
async def main():
fire.Fire(target, command=[])
asyncio.run(main())
Observed output:
RuntimeError: This event loop is already running
RuntimeWarning: coroutine 'target' was never awaited
As a control, fire.Fire(target, command=[]) from synchronous code prints and returns 42. The existing fire/fire_test.py::FireTest::testFireAsyncio test also passes; it runs outside an active event loop.
Expected behavior
Fire should either support this context or reject it with a clear, intentional error before creating an unawaited coroutine.
Likely cause and suggested fix
In fire/core.py, _CallAndUpdateTrace calls loop.run_until_complete(fn(*varargs, **kwargs)) after asyncio.get_running_loop() succeeds. A loop that is already running cannot be driven again by run_until_complete in the same thread. The smallest safe change is to detect this case before constructing the coroutine and return a clear error explaining that synchronous Fire cannot run an async command from an active loop. If support for this context is intended, an async entrypoint would need to await the command. A regression test belongs in fire/fire_test.py; docs/guide.md should state the supported behavior.
Environment: Python 3.12.10, Python Fire source version 0.7.1, Windows. Reproduced on the master revision above.
- 主要語言
- Python
- 星號
- 28.2k
- 分支
- 1.5k
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
google/python-fire 的其他 Issue
-
Async functools.partial calls return an unawaited coroutine可能已有人在做 @rioyu123 於 10 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 72/100
google/python-fire#711 ·
-
WrappedJoin adds an empty line before an oversized first item可能已有人在做 @GreedyC 於 18 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 76/100
google/python-fire#705 ·
-
Brace-based template arguments crash literal parsing with TypeError可能已有人在做 @codewithfourtix 於 19 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 76/100
google/python-fire#703 ·
-
Test suite fails on Windows: unescaped path regex in testArgPassing, NamedTemporaryFile reopen in testFileNameFire可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 85/100
google/python-fire#693 ·
-
Mio ❤️未關閉
難度 5/5 一週以上 新手友好度 1/100
google/python-fire#709 · 2 則留言 ·
查看 google/python-fire 的全部 Issue
相似的 Issue
-
bug
難度 2/5 1-3 小時 新手友好度 68/100
anchore/grype-db#1149 · 1 則留言 ·
維護者通常 1 天內回覆
-
[Bug]: `hermes plugins update` clones an entire monorepo twice - `subdir` is resolved but dropped at both catalog-update call sites可能已有人在做 @HootRock 今天認領。 未關閉comp/cli P2 type/perf
難度 2/5 1-3 小時 新手友好度 72/100
NousResearch/hermes-agent#142417 · 2 則留言 ·
維護者通常 1 天內回覆
-
SMC SPEC
難度 2/5 1-3 小時 新手友好度 70/100
tenstorrent/tt-oca-harness#3287 ·
維護者通常 1 天內回覆
-
bug enhancement python
難度 2/5 1-3 小時 新手友好度 85/100
DewaldOosthuizen/tech-cheat-sheets-and-notes#392 ·
維護者通常 1 天內回覆
-
security
難度 2/5 1-3 小時 新手友好度 72/100
fabriziosalmi/wildbox#801 ·
維護者通常 1 天內回覆