Skip to content

[core] impl backpressure for async streaming generators#64383

Open
rueian wants to merge 2 commits into
ray-project:masterfrom
rueian:async-generator-backpressure
Open

[core] impl backpressure for async streaming generators#64383
rueian wants to merge 2 commits into
ray-project:masterfrom
rueian:async-generator-backpressure

Conversation

@rueian

@rueian rueian commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Allow setting _actor_generator_backpressure_num_objects and _generator_backpressure_num_objects on async streaming generators by using asyncio.Event.

@rueian rueian added core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests labels Jun 26, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces non-blocking backpressure support for async streaming generators in Ray by utilizing an asyncio.Event and a C++ trampoline callback to wake the event loop when objects are consumed, preventing thread blockage. The changes span the Cython interface, C++ core worker, and include comprehensive test coverage. The review feedback highlights a potential segmentation fault in NotifyAsyncGeneratorBackpressureUnblock due to a missing null check on the callback, and suggests explicitly clearing the event and loop references in the finally block of execute_streaming_generator_async to avoid reference leaks.

Comment thread src/ray/core_worker/core_worker.cc
Comment thread python/ray/_raylet.pyx
@rueian rueian force-pushed the async-generator-backpressure branch from 5cc2a06 to 6c12d76 Compare June 26, 2026 19:42
Signed-off-by: Rueian Huang <rueiancsie@gmail.com>
@rueian rueian force-pushed the async-generator-backpressure branch from 6c12d76 to c541446 Compare June 26, 2026 20:02
@rueian rueian marked this pull request as ready for review June 26, 2026 22:11
@rueian rueian requested a review from a team as a code owner June 26, 2026 22:11

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c541446aa1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread python/ray/_raylet.pyx
Comment on lines 1529 to 1531
if context.actor_backpressure_metadata.get() != NULL:
await loop.run_in_executor(
executor,
_reserve_actor_generator_slot,
context,
)
await _async_reserve_actor_generator_slot(context)
output = await gen.asend(stats)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Check cancellation before resuming async generators

When an async streaming generator's owner dies while it is backpressured, HandleOwnerDied tears down the actor metadata and marks generator_id.TaskId() canceled; TryReserveSlot then returns true for the dead task so this await can complete, but the async path immediately calls gen.asend(...) without the IsTaskCanceled guard that the sync path has. In that owner-death scenario the actor can run another iteration of user code after the caller is gone, causing side effects or expensive work and delaying the actor slot from being released.

Useful? React with 👍 / 👎.

Signed-off-by: Rueian Huang <rueiancsie@gmail.com>
@edoakes

edoakes commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator

@karticam @sampan-s-nayak can you help review pls?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants