Repository navigation
Own the BaseAsyncOperator event loop through asyncio.Runner - #73560
Conversation
…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.
ashb
left a comment
There was a problem hiding this comment.
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
|
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.
|
Fair points on both counts, thanks. The PR is reworked in 11f5177 and the title and description are updated to match. On That is exactly what The previous "reuse a running loop" branch is gone too. It never actually worked for the callers, since Drafted-by: Claude Code (Fable 5.1); reviewed by @dabla before posting |
Co-authored-by: Ash Berlin-Taylor <[email protected]>
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.
Every task that runs through
BaseAsyncOperator.executelogsDeprecationWarning: There is no current event loopas 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 onasyncio.get_event_loop()to create one. That is exactly the case Python deprecated in 3.12, and Python 3.14 raisesRuntimeErrorthere 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 throughasyncio.Runner, as the asyncio docs recommend in place of policies. Plainasyncio.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 severalrun_until_complete()calls (the upcoming iterable operator does exactly that between batches of sub-tasks).Runnergives 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 noasyncio.Runner, so a small fallback replicatesRunner.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 thatrun_until_complete()cannot drive anyway.TestEventLoopruns every case against both theasyncio.Runnerpath and the Python 3.10 fallback (the fallback is exercised on every Python by removingasyncio.Runnerfor the test). It covers loop creation and cleanup without deprecation warnings, a loop surviving severalrun_until_complete()calls, cancellation of pending tasks on exit, not reusing a loop the caller already has set, refusing a running loop, andBaseAsyncOperator.executewith and withoutexecution_timeout.Checks run locally:
task-sdk/tests/task_sdk/bases/test_operator.py -k TestEventLoopon Python 3.10 (8 passed, the 8asyncio.Runnervariants skipped) and Python 3.12 (16 passed), andprek run --fileson the two changed files with every hook passing.Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Fable 5.1) following the guidelines