Skip to content

Own the BaseAsyncOperator event loop through asyncio.Runner - #73560

Merged
ashb merged 4 commits into
apache:mainfrom
dabla:fix/base-async-operator-event-loop-deprecation
Sep 29, 2026
Merged

ashb merged 4 commits into
apache:mainfrom
dabla:fix/base-async-operator-event-loop-deprecation

Conversation

@dabla

@dabla dabla commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Every task that runs through BaseAsyncOperator.execute logs DeprecationWarning: There is no current event loop as its first line on Python 3.12 and 3.13:

[2026-09-22 16:34:41] INFO - Worker startup parse complete bundle_name=dags-folder bundle_version= dag_file=test_msgraph.py bundle_prepare_ms=1 dag_file_parse_ms=17
[2026-09-22 16:34:41] WARNING - There is no current event loop category=DeprecationWarning

The task-runner process starts without an event loop set, and the event_loop() helper relied on asyncio.get_event_loop() to create one. That is exactly the case Python deprecated in 3.12, and Python 3.14 raises RuntimeError there instead of creating a loop. The helper also never closed a loop that asyncio created implicitly on its behalf, so that loop leaked for the rest of the process.

The first version of this PR avoided asyncio.get_event_loop() by reading the event loop policy's private per-thread state. Review rightly called that fragile, and the policy API is itself deprecated for removal in Python 3.16.

event_loop() now owns its loop through asyncio.Runner, as the asyncio docs recommend in place of policies. Plain asyncio.run() does not cover the helper's purpose: it creates a loop for one coroutine and closes it, while the helper hands out a loop that outlives a single coroutine so callers can drive it through several run_until_complete() calls (the upcoming iterable operator does exactly that between batches of sub-tasks). Runner gives that, and on exit also cancels leftover tasks and shuts down async generators and the default executor, which the previous helper never did. Python 3.10 has no asyncio.Runner, so a small fallback replicates Runner.close() until Python 3.10 support is dropped. Entering the helper from a running loop is now refused with a clear error instead of yielding a loop that run_until_complete() cannot drive anyway.

TestEventLoop runs every case against both the asyncio.Runner path and the Python 3.10 fallback (the fallback is exercised on every Python by removing asyncio.Runner for the test). It covers loop creation and cleanup without deprecation warnings, a loop surviving several run_until_complete() calls, cancellation of pending tasks on exit, not reusing a loop the caller already has set, refusing a running loop, and BaseAsyncOperator.execute with and without execution_timeout.

Checks run locally: task-sdk/tests/task_sdk/bases/test_operator.py -k TestEventLoop on Python 3.10 (8 passed, the 8 asyncio.Runner variants skipped) and Python 3.12 (16 passed), and prek run --files on the two changed files with every hook passing.


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Fable 5.1)

Generated-by: Claude Code (Fable 5.1) following the guidelines

…ask start

Every task that runs through BaseAsyncOperator.execute logs
"DeprecationWarning: There is no current event loop" as its first line on
Python 3.12 and 3.13. The task-runner process starts without an event loop
set, and the event_loop() helper relied on asyncio.get_event_loop() to
create one, which is exactly the case that call has deprecated: Python 3.14
raises RuntimeError there instead of creating a loop. The helper also never
closed a loop that asyncio created implicitly on its behalf, because it only
considered loops it created itself as its own, so that loop leaked for the
rest of the process.

The helper now looks up the loop already set for the thread without asking
asyncio to create one, so a running loop or an open loop set by the caller
is still reused untouched, and otherwise it owns the loop it creates and
closes it on exit. This keeps the current behaviour for callers that
already have a loop, removes the warning, and does not depend on the
Python version.

TestEventLoop covers loop creation and cleanup without deprecation
warnings, reuse of an existing or running loop, replacement of a closed
loop, and BaseAsyncOperator.execute with and without execution_timeout.
@dabla
dabla requested a review from potiuk September 22, 2026 15:17
@dabla dabla self-assigned this Sep 22, 2026
@dabla dabla added this to the Airflow 3.3.3 milestone Sep 24, 2026

@ashb ashb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we use asyncio.run() like the docs suggest btw?

Policies are deprecated and will be removed in Python 3.16. Users are encouraged to use the asyncio.run() function or the asyncio.Runner with loop_factory to use the desired loop implementation.

Or the asyncio.Runner() context manager: https://devdocs.io/python~3.14/library/asyncio-runner#asyncio.Runner

@ashb

ashb commented Sep 25, 2026

Copy link
Copy Markdown
Member

In short, I'm not sure the event_loop helper is designed right, and working around it like this feels even more fragile.

Review of the first version pointed out that avoiding asyncio.get_event_loop()
by reading the event loop policy's private per-thread state is fragile, and the
policy API itself is deprecated for removal in Python 3.16. asyncio.run() alone
does not cover the helper's purpose either: it hands out a loop that outlives a
single coroutine, so callers can drive it through several run_until_complete()
calls, which asyncio.run() cannot do. asyncio.Runner is the standard way to own
such a loop and also cancels leftover tasks and shuts down async generators on
exit, which the previous helper never did. Python 3.10 has no Runner, so a
fallback replicates Runner.close() until Python 3.10 support is dropped.
@dabla dabla changed the title Stop BaseAsyncOperator emitting a deprecation warning on task start Own the BaseAsyncOperator event loop through asyncio.Runner Sep 25, 2026
@dabla

dabla commented Sep 25, 2026

Copy link
Copy Markdown
Contributor Author

Fair points on both counts, thanks. The PR is reworked in 11f5177 and the title and description are updated to match.

On asyncio.run(): it would be enough for BaseAsyncOperator.execute on its own, since that runs one coroutine to completion. The reason the helper exists is that it hands out a loop that outlives a single coroutine. The iterable operator (the follow-up that will build on this) drives one loop through several run_until_complete() calls, pausing between batches of sub-tasks, and asyncio.run() cannot do that: it creates a loop, runs one coroutine and closes it, so nothing survives between calls. I had tried asyncio.run() there earlier and that is why it "didn't work".

That is exactly what asyncio.Runner is for, so event_loop() now owns its loop through a Runner: runner.get_loop() is what gets yielded, and on exit Runner cancels leftover tasks and shuts down async generators and the default executor, which the old helper never did. Nothing touches asyncio.get_event_loop() or the policy API any more. The one wrinkle is Python 3.10, which has no asyncio.Runner, so a small fallback replicates Runner.close() until 3.10 support is dropped; the tests run every case against both paths.

The previous "reuse a running loop" branch is gone too. It never actually worked for the callers, since run_until_complete() on a running loop raises, so entering the helper from a running loop is now refused up front with a clear error.


Drafted-by: Claude Code (Fable 5.1); reviewed by @dabla before posting

dabla and others added 2 commits September 25, 2026 14:22
Review asked for tests grouped by the class or subject under test rather than
by behaviour. task-sdk had no test class for BaseAsyncOperator at all, only the
compat stub test for Airflow below 3.2, so the operator now gets one, with the
execution_timeout enforcement covered as well, and the event loop helper keeps
a class of its own. The fixtures both classes need live at module level.
@dabla
dabla requested a review from ashb September 27, 2026 18:16
@ashb
ashb merged commit 8d815ef into apache:main Sep 29, 2026
107 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants