Skip to content

[Data] Fix sync UDF execution in fused async map worker - #65923

Draft
luicarus wants to merge 1 commit into
ray-project:masterfrom
luicarus:fix/data-map-batches-asyncio-loop
Draft

[Data] Fix sync UDF execution in fused async map worker#65923
luicarus wants to merge 1 commit into
ray-project:masterfrom
luicarus:fix/data-map-batches-asyncio-loop

Conversation

@luicarus

@luicarus luicarus commented Sep 4, 2026

Copy link
Copy Markdown

Description

Fixes #57729.

When consecutive map_batches operators are fused into a single actor worker that also hosts an async UDF, _MapActorContext._init_async() runs an asyncio event loop on a dedicated run_loop thread. The fused synchronous UDF is then invoked on that same thread.

If the synchronous UDF calls asyncio.run(), Python rejects it because an event loop is already running on the calling thread:

RuntimeError: asyncio.run() cannot be called from a running event loop

This change detects whether the calling thread already has a running event loop using asyncio.get_running_loop(). When it does, the synchronous UDF call is dispatched to a dedicated loop-free worker thread and the caller waits for the result.

The common no-loop path remains on the existing direct-call path and incurs no thread-hop overhead. Only the affected running-loop path takes the additional thread hop.

The thread pool is created lazily with max_workers=1. Its worker thread does not host a pre-existing event loop, so synchronous user code can safely call asyncio.run() there.

An alternative would be to prevent sync-UDF operators from being fused into async-context actors at the planning level. I kept this change at runtime so the existing fusion behavior remains intact, but I'm happy to rework it if the maintainers prefer the planning-level approach.

Tests

Added test_map_batches_sync_udf_with_asyncio_run_chained_with_async_actor in:

python/ray/data/tests/test_map_batches.py

The regression test covers a synchronous UDF that calls asyncio.run() followed by an async callable-class transform. The chained pipeline must complete successfully and produce the expected results.

Locally verified on Windows with Python 3.12 and Ray 2.58.0, with the same planner patch applied to the installed Ray package:

Before the fix, the chained sync UDF + async actor case raised:

RuntimeError: asyncio.run() cannot be called from a running event loop

Local validation limitation

The Ray source tree could not be built locally on Windows, so the in-repo regression test was not executed directly from a source build.

Local validation used the installed Ray 2.58.0 package with the same planner change applied, together with an equivalent standalone regression harness. The repository test will be exercised by CI.

AI disclosure

AI tooling was used to assist the root-cause analysis and the implementation. The human contributor reviewed every changed line, ran the tests locally, and understands and can defend this change end-to-end, as required by this repository's contribution policy for AI-assisted PRs.

Signed-off-by: luxing <luicarus@users.noreply.github.com>
@luicarus
luicarus force-pushed the fix/data-map-batches-asyncio-loop branch from ce59cb6 to a933b62 Compare September 4, 2026 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Data] map_batches fails with asyncio.run() in func and chaining with acync actor due to event loop conflict

1 participant