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 于 18 天前认领。 未关闭
难度 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
-
documentation
难度 2/5 1-3 小时 新手友好度 67/100
维护者通常 1 天内回复
-
good first issue module-baseline needs triage ux
难度 2/5 1-3 小时 新手友好度 82/100
darnitdevorg/darnit#650 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
nyjc-computing/campus#938 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
nf-osi/nf-metadata-dictionary#1027 ·
维护者通常 1 天内回复
-
bug documentation
难度 1/5 1 小时以内 新手友好度 92/100
qatrackplus/qatrackplus#913 ·
维护者通常 3 天内回复