Skip to content

[core] impl backpressure for async streaming generators - #64383

Merged
rueian merged 7 commits into
ray-project:masterfrom
rueian:async-generator-backpressure
Jul 7, 2026
Merged

rueian merged 7 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 Resolved
Comment thread python/ray/_raylet.pyx Resolved
@rueian
rueian force-pushed the async-generator-backpressure branch from 5cc2a06 to 6c12d76 Compare June 26, 2026 19:42
@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 Resolved
Signed-off-by: Rueian Huang <[email protected]>
@edoakes

edoakes commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator

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

Signed-off-by: Rueian Huang <[email protected]>

@sampan-s-nayak sampan-s-nayak 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.

Thanks for working on this, mostly looks good except for a few concerns

Comment thread src/ray/core_worker/core_worker.h Resolved
Comment thread src/ray/core_worker/core_worker.h Resolved
Comment thread python/ray/_raylet.pyx Outdated
# Block inline on backpressure (no-op when disabled). Each sync
# generator runs on its own execution thread, so blocking here
# does not stall other tasks.
_wait_for_object_consumed(context)

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.

why do we need this? wont _reserve_actor_generator_slot(context) check conditions for us?

@rueian rueian Jul 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We do need this. This, WaitUntilObjectConsumed, was for task-level backpressure and was originally done inside the ReportGeneratorItemReturns. I moved it out because it would block the thread, but to support async, we need an async version of that, which is now _async_wait_for_object_consumed.

_wait_for_object_consumed here is making them symmetrical.

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.

ahh got it, I remember now thanks!

Comment thread python/ray/_raylet.pyx Outdated

# Fallback re-check interval for the async backpressure wait. The event is
# woken promptly by the core worker when consumption advances; this bounds how
# long an async generator can stay parked if a wake-up is ever missed (e.g. a

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.

should we do without this and fix any issues we spot? wondering if keeping this ends up hiding issues in our implementation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, makes sense. I added the wake-up everywhere it is needed in the latest commit.

Comment thread python/ray/_raylet.pyx Resolved
Signed-off-by: Rueian Huang <[email protected]>

@cursor cursor 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.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit b260378. Configure here.

Comment thread src/ray/core_worker/core_worker.cc Resolved
rueian added 2 commits July 6, 2026 10:12
Signed-off-by: Rueian Huang <[email protected]>
Signed-off-by: Rueian Huang <[email protected]>
@rueian
rueian force-pushed the async-generator-backpressure branch from 8387344 to c92f4dd Compare July 6, 2026 18:41
Signed-off-by: Rueian Huang <[email protected]>

@sampan-s-nayak sampan-s-nayak 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.

LGTM

@rueian

rueian commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Thank you @sampan-s-nayak!

@rueian
rueian merged commit d967e45 into ray-project:master Jul 7, 2026
3 checks passed
elliot-barn added a commit that referenced this pull request Jul 18, 2026
Three independent Windows-only breaks, all verified against postmerge
builds 18675/18684:

- generator_waiter_test.cc: MSVC C2131 — a by-ref-captured constexpr
  local used as an array bound inside a lambda is not a constant
  expression to MSVC. Make the test constants static constexpr. Broken
  since #64383.

- test_streaming_generator: move from the medium to the large shard.
  Passing Windows runs take ~296s against the moderate 300s limit
  (build 18675: PASSED in 296.2s; build 18684: TIMEOUT at 300s in 2/2
  attempts, mid-suite at test 21-22 of 23) — it is slow, not hung.

- test_runtime_env_uv::test_uv_with_version_and_check: use
  importlib.metadata instead of pkg_resources. virtualenv >= 21 (bumped
  in #64257) no longer seeds setuptools into the venvs Ray creates, and
  setuptools >= 83 removed pkg_resources entirely, so the test must not
  depend on it regardless of the virtualenv pin decision in #64739.

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: elliot-barn <[email protected]>
elliot-barn added a commit that referenced this pull request Jul 19, 2026
…ile error, streaming-generator timeout, uv test (#64865)

## Why are these changes needed?

Fixes all four Windows test failures on master postmerge (builds
18675/18684). Windows tests run only on release-automation-triggered
postmerge builds, so these landed silently.

### 1. `test_experimental_collective` — torch gloo broken on Windows
(pin torch 2.7.0)

Fails since #64257 bumped torch `2.7.0 → 2.9.0`:

```
RuntimeError: makeDeviceForHostname(): unsupported gloo device
```

torch 2.8/2.9 stock Windows wheels are built **without libuv**, and
gloo's TCP transport is Linux-only, so `torch.distributed`'s gloo
backend cannot initialize at all on Windows (pytorch/pytorch#150381).
Fixed upstream via pytorch/gloo#472, shipped in torch **2.10** and
explicitly not cherry-picked into 2.9.x. Ray's own postmerge bisection
(builds 18569–18574) confirmed #64257 as first-bad; Linux/macOS are
unaffected.

The windowsbuild image previously pip-installed its test deps
constrained by `requirements_compiled.txt` directly, so there was no
depset to override. This brings it under raydepsets
(`ci_windows.depsets.yaml`), mirroring the relax/expand pattern from
`ci_data.depsets.yaml`:

- `windows_tests_ci_depset_310` — compile `requirements.txt` +
`test-requirements.txt` + `dl-cpu-requirements.txt` for windows/py3.10
constrained by `requirements_compiled` (same inputs `build_base.sh`
used).
- `relaxed_windows_tests_ci_depset_310` — relax torch, torchvision, and
the ABI-coupled PyG binaries.
- `windows_tests_torch27_ci_depset_310` — expand with
`python/requirements/windows-requirements.txt`: `torch==2.7.0`,
`torchvision==0.22.0`, and the `+pt27cpu` PyG local builds (explicit
local tags because the relaxed lock still emits the torch-2.9.0
find-links and unsafe-best-match would otherwise re-pick `+pt29cpu`).

`build_base.sh` installs the final lock hash-stripped with `--no-deps`
(same pattern as `ci/ray_ci/macos/macos_ci.sh`). The wanda srcs now key
on the lock instead of the four requirements files. The final lock
differs from the base resolution by exactly the six torch packages.
Windows **wheel** builds are unaffected (hermetic `uv run` env in
`build-wheel-windows.sh`). Delete `windows-requirements.txt` and the
torch2.7 depsets once the repo-wide torch pin reaches ≥2.10.

### 2. `generator_waiter_test` — MSVC C2131 compile error (cpp tests
job)

Broken since #64383: a by-ref-captured `constexpr` local used as an
array bound inside a lambda is not a constant expression to MSVC. Make
the test constants `static constexpr`.

### 3. `test_streaming_generator` — timeout, moved to the large shard

Passing Windows runs take ~296s against the moderate (300s) limit (build
18675: PASSED in 296.2s; build 18684: TIMEOUT at 300s in 2/2 attempts,
dying mid-suite at test 21–22 of 23) — legitimately slow, not hung. The
Windows job selects `//python/ray/tests/...` by path, so the size-tag
move keeps Windows coverage.

### 4. `test_runtime_env_uv::test_uv_with_version_and_check` —
pkg_resources removed

Use `importlib.metadata` instead of `pkg_resources`: virtualenv ≥21
(bumped in #64257) no longer seeds setuptools into the venvs Ray
creates, and setuptools ≥83 removed `pkg_resources` entirely, so the
test must not depend on it regardless of the virtualenv pin decision in
#64739. Ray's non-test code has zero pkg_resources usage.

## Related issue number

Root causes: upstream pytorch/pytorch#150381 (item 1); regressions
introduced by #64257 (items 1, 4) and #64383 (item 2).

## Checks

- Not a duplicate: searched open PRs — none address the Windows
gloo/torch failure, the MSVC break, or the streaming-generator timeout.
#64739 (virtualenv pin) is complementary: item 4 here makes the test
independent of that decision.
- Tests run locally: `bazel run //ci/raydepsets:raydepsets -- build
ci/raydepsets/configs/ci_windows.depsets.yaml` (all depsets compile;
final lock pins `torch==2.7.0+cpu`, diff vs base is exactly the six
torch packages); `bazel query` confirms `test_streaming_generator` is a
unique target with `size = large`; `pre-commit` passed on all changed
files. Windows image build + test runs can only be validated in CI —
watch `windowsbuild` (cold rebuild expected) and the Windows cpp/python
test jobs on this PR.
- AI assistance was used for this change (diagnosis and implementation);
reviewed and submitted by a human.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: elliot-barn <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…64383)

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

---------

Signed-off-by: Rueian Huang <[email protected]>
Signed-off-by: 400Ping <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…ile error, streaming-generator timeout, uv test (ray-project#64865)

## Why are these changes needed?

Fixes all four Windows test failures on master postmerge (builds
18675/18684). Windows tests run only on release-automation-triggered
postmerge builds, so these landed silently.

### 1. `test_experimental_collective` — torch gloo broken on Windows
(pin torch 2.7.0)

Fails since ray-project#64257 bumped torch `2.7.0 → 2.9.0`:

```
RuntimeError: makeDeviceForHostname(): unsupported gloo device
```

torch 2.8/2.9 stock Windows wheels are built **without libuv**, and
gloo's TCP transport is Linux-only, so `torch.distributed`'s gloo
backend cannot initialize at all on Windows (pytorch/pytorch#150381).
Fixed upstream via pytorch/gloo#472, shipped in torch **2.10** and
explicitly not cherry-picked into 2.9.x. Ray's own postmerge bisection
(builds 18569–18574) confirmed ray-project#64257 as first-bad; Linux/macOS are
unaffected.

The windowsbuild image previously pip-installed its test deps
constrained by `requirements_compiled.txt` directly, so there was no
depset to override. This brings it under raydepsets
(`ci_windows.depsets.yaml`), mirroring the relax/expand pattern from
`ci_data.depsets.yaml`:

- `windows_tests_ci_depset_310` — compile `requirements.txt` +
`test-requirements.txt` + `dl-cpu-requirements.txt` for windows/py3.10
constrained by `requirements_compiled` (same inputs `build_base.sh`
used).
- `relaxed_windows_tests_ci_depset_310` — relax torch, torchvision, and
the ABI-coupled PyG binaries.
- `windows_tests_torch27_ci_depset_310` — expand with
`python/requirements/windows-requirements.txt`: `torch==2.7.0`,
`torchvision==0.22.0`, and the `+pt27cpu` PyG local builds (explicit
local tags because the relaxed lock still emits the torch-2.9.0
find-links and unsafe-best-match would otherwise re-pick `+pt29cpu`).

`build_base.sh` installs the final lock hash-stripped with `--no-deps`
(same pattern as `ci/ray_ci/macos/macos_ci.sh`). The wanda srcs now key
on the lock instead of the four requirements files. The final lock
differs from the base resolution by exactly the six torch packages.
Windows **wheel** builds are unaffected (hermetic `uv run` env in
`build-wheel-windows.sh`). Delete `windows-requirements.txt` and the
torch2.7 depsets once the repo-wide torch pin reaches ≥2.10.

### 2. `generator_waiter_test` — MSVC C2131 compile error (cpp tests
job)

Broken since ray-project#64383: a by-ref-captured `constexpr` local used as an
array bound inside a lambda is not a constant expression to MSVC. Make
the test constants `static constexpr`.

### 3. `test_streaming_generator` — timeout, moved to the large shard

Passing Windows runs take ~296s against the moderate (300s) limit (build
18675: PASSED in 296.2s; build 18684: TIMEOUT at 300s in 2/2 attempts,
dying mid-suite at test 21–22 of 23) — legitimately slow, not hung. The
Windows job selects `//python/ray/tests/...` by path, so the size-tag
move keeps Windows coverage.

### 4. `test_runtime_env_uv::test_uv_with_version_and_check` —
pkg_resources removed

Use `importlib.metadata` instead of `pkg_resources`: virtualenv ≥21
(bumped in ray-project#64257) no longer seeds setuptools into the venvs Ray
creates, and setuptools ≥83 removed `pkg_resources` entirely, so the
test must not depend on it regardless of the virtualenv pin decision in
ray-project#64739. Ray's non-test code has zero pkg_resources usage.

## Related issue number

Root causes: upstream pytorch/pytorch#150381 (item 1); regressions
introduced by ray-project#64257 (items 1, 4) and ray-project#64383 (item 2).

## Checks

- Not a duplicate: searched open PRs — none address the Windows
gloo/torch failure, the MSVC break, or the streaming-generator timeout.
ray-project#64739 (virtualenv pin) is complementary: item 4 here makes the test
independent of that decision.
- Tests run locally: `bazel run //ci/raydepsets:raydepsets -- build
ci/raydepsets/configs/ci_windows.depsets.yaml` (all depsets compile;
final lock pins `torch==2.7.0+cpu`, diff vs base is exactly the six
torch packages); `bazel query` confirms `test_streaming_generator` is a
unique target with `size = large`; `pre-commit` passed on all changed
files. Windows image build + test runs can only be validated in CI —
watch `windowsbuild` (cold rebuild expected) and the Windows cpp/python
test jobs on this PR.
- AI assistance was used for this change (diagnosis and implementation);
reviewed and submitted by a human.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: elliot-barn <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
Signed-off-by: 400Ping <[email protected]>
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.

3 participants