# Fix "RuntimeError: There is no current event loop" on Python 3.14

Yes. An agent replaces module-level asyncio.get_event_loop() calls with asyncio.run, restructures the scripts that ran forever, and runs each one on Python 3.14.

[Run this on Rush](https://cloud.getrush.ai/run/fix-no-current-event-loop-runtime-error/asyncio/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

For years the standard way to start an asyncio program was `asyncio.get_event_loop().run_until_complete(main())`, and `get_event_loop()` quietly created a loop when none existed. Python 3.14 ended that: called outside a coroutine with no loop set, it raises `RuntimeError: There is no current event loop in thread 'MainThread'`. The library you import may be fine while every script that starts it is broken. Polyconseil/aioamqp shows the split. The package itself asks for a loop in one place, `aioamqp/protocol.py`, and it uses `asyncio.get_running_loop()` there, which is correct on every version. The old call is in what users copy: all 13 files under `examples/` call it at module level, and so do three snippets in `docs/api.rst` and `docs/examples/hello_world.rst`.

The agent runs the examples to see where each one stops, without needing a broker to reach the answer. With the package's one dependency, `pamqp`, installed, `examples/send.py` on Python 3.12 prints `DeprecationWarning: There is no current event loop`, creates a loop anyway and goes on to attempt the connection, which is refused because nothing listens on the AMQP port. On 3.14 the same file raises the `RuntimeError` on its last line before any connection is tried. With that line changed to `asyncio.run(send())`, the 3.14 run reaches the connection attempt again. A refused connection is the expected outcome in a sandbox with no RabbitMQ, and reaching it shows the loop problem is gone.

The scripts are not all the same shape, and the agent sorts them before editing. Eight are one-shot: they publish or make one call and exit, and `asyncio.run(coro())` replaces the last line directly. Five are consumers that call `run_until_complete(setup())` and then `run_forever()`, relying on the loop to stay alive after the setup coroutine returns. `asyncio.run` closes its loop when the coroutine finishes, so a direct swap would make a consumer subscribe and exit at once. Each of those needs a `main()` coroutine that performs the setup and then waits, for instance on an `asyncio.Event` that a signal handler sets, with the connection closed on the way out. One of them, `receive_log_direct.py`, already builds an `asyncio.Event` and catches `KeyboardInterrupt` around the loop call, and the agent keeps that behaviour inside the new structure.

The documentation snippets get the same edit, since readers copy those first. Afterwards the agent searches the repository for other reliance on an implicit loop: `asyncio.get_event_loop()` at import time, a `loop=` argument stored in a constructor, or a call made after `asyncio.run` has returned, at which point no current loop exists again. It runs each example on the oldest Python the project claims, 3.7 in `setup.py`, and on 3.14, and tabulates how far each gets. It also reports that `requirements_dev.txt` names `asynctest` and that the workflow matrix in `.github/workflows/tests.yml` ends at 3.10, so the suite has not been exercised on the interpreter where this error appears.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Scripts in this project fail on Python 3.14 with \"RuntimeError: There is no current event loop in thread 'MainThread'\". Find every call to asyncio.get_event_loop() that can run when no loop is running: at module level, in a main function, in a constructor, in examples, in documentation snippets and in tests. Leave calls that are only ever made inside a running coroutine, but prefer asyncio.get_running_loop() there. Install the project's dependencies on Python 3.14 and on the oldest Python it claims to support, run each affected script on both, and record how far each one gets; if a script needs an external service that is not available, say so and treat reaching the connection attempt as the measure. Then fix them. A script that runs one coroutine and exits becomes asyncio.run(coro()). A script that calls run_until_complete and then run_forever must not be changed by a one-line swap, because asyncio.run closes the loop when its coroutine returns: give it a main coroutine that does the setup and then waits until it is interrupted, and close connections on exit. Keep each script's behaviour on Ctrl-C. Update documentation examples the same way. Do not change the library's public API. Re-run every script on both interpreters and show the table before and after. Report, without fixing here, anything else that blocks the test suite on Python 3.14. Open a pull request with the tables, the tracebacks and what you left alone.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that touches the 13 example files and two documentation pages and no library code. The description has a table with one row per script and one column per interpreter, before and after: the line each run stopped on and the message it printed. For the one-shot scripts the after column shows the connection attempt. For the consumers it shows the script still waiting after setup when it is interrupted, which is the property a one-line swap would have lost.

A reviewer reads the five consumer scripts closely, since those were restructured, and checks that each still stops cleanly on an interrupt and closes its protocol and transport. They should also note the stated limit: no message is sent to or received from a broker in the sandbox unless the description says a RabbitMQ server is installed and started there. The section on the test suite tells them what a follow-up needs before 3.14 can be added to the matrix.

## Run it on every issue that reports a broken example

Broken examples are reported by newcomers, one issue at a time, each quoting the same traceback. Add an automation on the Project with a `github` trigger on `issues.labeled` and a label such as `examples`, holding the prompt above. When a maintainer adds the label, the issue is appended after the prompt as event text. It fills in no placeholder, and the prompt remains the only instruction, so write it to say that a user report follows.

Only an owner, member or collaborator of the repository fires the trigger, which keeps a stranger's issue from starting work until someone on the team has looked at it. Set the automation's max runs per hour low, since one run covers every script.

## Why run it on Rush?

Whether a start-up script works is a question about one interpreter at a time, and the answer has had three states across versions: silent, then a warning, then an error. The agent installs the old and the new interpreter in its sandbox and runs all of the scripts under both, a chore nobody does by hand for example code. The session keeps the output of each run, and the table in the pull request is assembled from it.

Example code is also the least owned code in a repository. It has no tests, and the continuous integration never executes it. An agent that treats thirteen small files as thirteen things to run, and tells the two shapes apart before editing, does the tedious part correctly and leaves a reviewer with five files that need real attention.

## Frequently asked questions

### What replaces asyncio.get_event_loop().run_until_complete(main())?

`asyncio.run(main())`. It creates a new loop, runs the coroutine, cancels what is left, and closes the loop. It has been available since Python 3.7, so the replacement does not narrow the versions a typical project supports.

### My script calls run_forever() after run_until_complete(). Can I still use asyncio.run?

Yes, with a small restructure. Put the setup in a `main()` coroutine and end it with an `await` that lasts as long as the program should, such as waiting on an `asyncio.Event`. Without that wait the coroutine returns, `asyncio.run` closes the loop, and the consumer exits right after subscribing.

### Why did it only warn before?

Earlier versions kept creating the loop for you and emitted a `DeprecationWarning` with nearly the same text. Python 3.14 turned the warning into the error. A project that showed deprecation warnings in its test runs had notice; scripts started by hand usually did not.

### Is get_event_loop() still usable anywhere?

Inside a coroutine or callback it returns the running loop, and it works after you have set one with `asyncio.set_event_loop`. For code that runs inside a loop, `asyncio.get_running_loop()` states the intent and fails clearly when there is none.

## More asyncio jobs

- [Fix "AttributeError: type object '_asyncio.Task' has no attribute 'all_tasks'" on Python 3.9 and Later](https://getrush.ai/run/fix-task-all-tasks-attribute-error/asyncio/)
- [Fix "AttributeError: module 'asyncio' has no attribute 'coroutine'"](https://getrush.ai/run/fix-asyncio-coroutine-attribute-error/asyncio/)
- [Fix "TypeError: As of 3.10, the *loop* parameter was removed from Lock()" in asyncio](https://getrush.ai/run/fix-asyncio-loop-parameter-removed/asyncio/)

## More jobs like this: Fix a bug from an alert

- [Fix "no matches for kind Ingress in version extensions/v1beta1" in Kubernetes Manifests](https://getrush.ai/run/fix-no-matches-for-kind-ingress/kubernetes/)
- [Fix "ModuleNotFoundError: No module named 'asyncore'" on Python 3.12](https://getrush.ai/run/fix-no-module-named-asyncore/asyncore/)
- [Fix "ModuleNotFoundError: No module named 'audioop'" on Python 3.13](https://getrush.ai/run/fix-no-module-named-audioop/audioop/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
