[Data] Fix sync UDF execution in fused async map worker - #65923
Draft
luicarus wants to merge 1 commit into
Draft
Conversation
Signed-off-by: luxing <luicarus@users.noreply.github.com>
luicarus
force-pushed
the
fix/data-map-batches-asyncio-loop
branch
from
September 4, 2026 07:05
ce59cb6 to
a933b62
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #57729.
When consecutive
map_batchesoperators are fused into a single actor worker that also hosts an async UDF,_MapActorContext._init_async()runs an asyncio event loop on a dedicatedrun_loopthread. 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: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 callasyncio.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_actorin: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:
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.