Skip to content

Calling an async command from an active event loop raises RuntimeError and leaves an unawaited coroutine #716

Open

Description

@sansynx

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions