# Fix "AttributeError: type object '_asyncio.Task' has no attribute 'all_tasks'" on Python 3.9 and Later

Yes. An agent replaces asyncio.Task.all_tasks(), passing the loop where no loop is running, and tests the shutdown path the old call was guarding.

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

## What the agent does

`asyncio.Task.all_tasks()` and `asyncio.Task.current_task()` were class methods until Python 3.7 added the module functions `asyncio.all_tasks()` and `asyncio.current_task()`. Python 3.8 warned with `Task.all_tasks() is deprecated, use asyncio.all_tasks() instead`, and 3.9 removed them. The message invites a rename, and the rename fails in the place this call usually lives. The old method worked without a running loop, because it looked up the current loop through `get_event_loop()`. The new function calls `get_running_loop()`, so outside a coroutine it raises `RuntimeError: no running event loop`. Shutdown code is exactly that situation: it runs after `run_until_complete` has returned.

boyan-soubachov/tastyworks_api, an unofficial brokerage client, has it in `tastyworks/example.py`. `main()` gets a loop, runs `loop.run_until_complete(main_loop(tasty_client, streamer))` inside `try`, logs any exception, and in `finally` builds `pending_tasks` from `asyncio.Task.all_tasks()`, waits for them with `asyncio.gather` and closes the loop. Because the call sits in `finally`, it runs on success and on failure alike. On Python 3.9 and later every run of the example ends with the `AttributeError`, the pending tasks are never awaited and `loop.close()` is never reached. When `main_loop` itself failed, the traceback a user sees last is this one and not the logged cause.

The agent makes the smallest correct change first: `asyncio.all_tasks(loop)`, with the loop the function already holds. Then it reads what the block is for and tests it, since nothing in the repository does. The `tests` directory covers the models only. `main_loop` builds an order and submits it to the brokerage, so the agent never runs the example against a real account, in any mode. It extracts the shutdown steps into a small function and tests that with stand-in coroutines: one background task still sleeping when the main coroutine returns, one when it raises. The test asserts that the task is awaited and the loop is closed in both cases.

That test brings up the design question the agent reports instead of deciding. Waiting on every pending task with `gather` returns only if those tasks finish on their own. A streaming client holds a listener that runs until cancelled, and then shutdown hangs. The modern form, `asyncio.run(main_loop(...))`, cancels whatever is left and closes the loop itself, which is a different contract: remaining work is stopped, not completed. The agent keeps the existing contract in the fix and describes the alternative with the one-line change it needs. It also lists the neighbouring calls a newer interpreter complains about, here two `asyncio.get_event_loop().run_until_complete(...)` calls in `DataStreamer.__init__` and `__del__` in `tastyworks/streamer.py`, without rewriting them in the same pull request. `.travis.yml` lists Python 3.6 to 3.9 and `3.10-dev`, and `asyncio.all_tasks` needs 3.7, so the agent states the new minimum beside the `python_requires='>= 3.6.0'` it found in `setup.py`.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "This project fails on Python 3.9 or later with \"AttributeError: type object '_asyncio.Task' has no attribute 'all_tasks'\" (or 'current_task'). Find every use of asyncio.Task.all_tasks and asyncio.Task.current_task, including calls that pass a loop argument. For each, say whether it executes inside a coroutine or callback with a running event loop, or in synchronous code such as a finally block after run_until_complete, a signal handler or a destructor. Replace with asyncio.all_tasks() or asyncio.current_task() where a loop is running, and with asyncio.all_tasks(loop) where it is not, because the module function raises \"RuntimeError: no running event loop\" there. Do not wrap the call in try and except to hide that. Then read the code around each call and state what it is meant to guarantee at shutdown: that pending tasks finish, that they are cancelled, or only that the loop closes. Extract that logic into a function and add tests that use stand-in coroutines, covering a clean exit and an exit by exception, each with one task still pending, and asserting what happens to the task and that the loop is closed. Never run example or entry-point scripts that contact a real external service, place orders, send messages or spend money; test the shutdown path in isolation. If waiting on pending tasks can hang because a task runs until cancelled, report it and describe the asyncio.run alternative and how its behaviour differs, without switching to it. List other deprecated event-loop calls you see nearby as findings. State the minimum Python version the fix requires next to the one the package declares. Run the tests and include commands and output. Open a pull request.",
  "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 changes the failing call, moves the shutdown steps into a function and adds a test module for it. The description classifies each use of the removed methods by whether a loop is running at that point, which is what determines the replacement. It shows the test output for both exits and states the contract the tests pin: pending tasks are awaited, then the loop is closed.

A findings section follows. It says whether any pending task could run until cancelled and so block shutdown, sets out the `asyncio.run` option with its different behaviour, and lists other event-loop calls that newer Python versions deprecate. It also records the version consequence: the fix needs Python 3.7, and the package metadata and CI list say 3.6. A reviewer confirms the classification, since a call marked as running inside a loop when it is not would raise at shutdown, the least exercised path. They then choose between the two shutdown contracts, which is a decision about the program and not about the error.

## Run it on every terminal hand-off

This traceback usually appears in a developer's own terminal, at the end of a script that otherwise did its work. With Rush connected as an MCP server in their coding tool, they can hand the job over without leaving it: the assistant calls `create_session` with this prompt and the repository, gets the session back at once, and `wait_for_sessions` reports the answer when the run settles. The developer keeps working on whatever the script was for.

From a shell, the same body goes to `POST /o/{org}/sessions` with an API key. Send a `request_id`, a UUID, so a retried start returns the run it already began. When the turn ends, the session read names the pull request, and a follow-up message to the same session can ask for the `asyncio.run` version as a second commit once the team has chosen it.

## Why run it on Rush?

The line that breaks runs once per process, at exit, and only its absence is noticed: a hung shutdown, a connection never closed, a real error hidden behind this one. Testing it means building small event-loop scenarios and running them on an interpreter that still has the old method and on one that does not. The agent does both in its sandbox and keeps the output, so the pull request shows the old behaviour, the failure and the fixed behaviour from the same test file.

It matters as well that the job runs away from the developer's credentials. Example scripts for trading, messaging or cloud clients act on real accounts when started, and a quick check on a laptop with environment variables set is how an unintended order gets placed. A sandbox that holds only the repository has nothing to act on, and the prompt forbids running such entry points regardless.

## Frequently asked questions

### What replaces asyncio.Task.all_tasks()?

`asyncio.all_tasks()` inside a coroutine or callback, and `asyncio.all_tasks(loop)` in synchronous code that holds a loop that is not running. `asyncio.Task.current_task()` becomes `asyncio.current_task()` in the same way. Both functions exist from Python 3.7.

### Why do I get "RuntimeError: no running event loop" after renaming?

The module function looks up the running loop when called with no argument, and there is none once `run_until_complete` has returned. Pass the loop explicitly. The removed method did not need this because it used `get_event_loop()`.

### Does the result differ between the old and new call?

Slightly. The old method returned every task for the loop, finished or not, which is why code filters with `task.done()`. `asyncio.all_tasks` returns only tasks that are not yet done. The filter becomes redundant and is harmless to keep.

### Should I switch to asyncio.run() instead?

Often, yes, for a script's entry point: it creates the loop, cancels remaining tasks and closes it. That is a behaviour change if your code relied on pending tasks finishing, so the agent describes it and leaves the choice to you.

## More asyncio jobs

- [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/)
- [Fix "RuntimeError: There is no current event loop" on Python 3.14](https://getrush.ai/run/fix-no-current-event-loop-runtime-error/asyncio/)

## More jobs like this: Hand off a long task

- [Fix the ts-jest Config Under globals Is Deprecated Warning](https://getrush.ai/run/fix-ts-jest-config-under-globals-is-deprecated/ts-jest/)
- [Keep Supabase TypeScript Types in Sync With Your Migrations](https://getrush.ai/run/generate-database-types/supabase/)
- [Harden Docker Compose Services with Read-Only Filesystems and Dropped Capabilities](https://getrush.ai/run/harden-compose-services/docker-compose/)

## 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/)
