Skip to content

[RLlib] Fix wrong assert variable in _update_env_seed_if_necessary#61823

Merged
ArturNiederfahrenhorst merged 9 commits into
ray-project:masterfrom
nathon-lee:fix_issue_61593
Apr 14, 2026
Merged

[RLlib] Fix wrong assert variable in _update_env_seed_if_necessary#61823
ArturNiederfahrenhorst merged 9 commits into
ray-project:masterfrom
nathon-lee:fix_issue_61593

Conversation

@nathon-lee

Copy link
Copy Markdown
Contributor

Description

This PR fixes a small bug in rllib's _update_env_seed_if_necessary() helper.

The assert in that function was checking worker_idx < max_num_envs_per_env_runner, but the boundary there should apply to vector_idx, not worker_idx. Because of this, valid cases with a large worker_idx could be rejected incorrectly, while the intended guard is to ensure that vector_idx stays within the per-worker env limit used in the seed calculation.

This PR makes the fix by updating the assert to:

  • vector_idx < max_num_envs_per_env_runner

and adds a regression test covering the boundary behavior:

  • a valid large worker_idx with an in-range vector_idx should succeed
  • an out-of-range vector_idx should still raise

The change is intentionally minimal and should be low risk.

Related issues

Fixes #61593

Additional information

I kept the change narrowly scoped to the incorrect assert and the corresponding regression test.

I was not able to fully run the targeted test locally in this checkout because the local environment is missing the compiled ray._raylet module, but the logic change is straightforward and the added test is designed specifically to prevent regression for this boundary condition.

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
@nathon-lee nathon-lee requested a review from a team as a code owner March 18, 2026 02:21

@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 correctly fixes a bug in _update_env_seed_if_necessary by using vector_idx instead of worker_idx in the assertion to prevent seed collisions. The accompanying regression tests are well-designed, covering both the success case for the fix and the failure case for out-of-bounds indices. I have one suggestion to improve the maintainability of the new tests by replacing magic numbers with a named constant.

Comment on lines +186 to +201
def test_update_env_seed_accepts_max_worker_idx_with_valid_vector_idx(self):
env = SeedRecordingEnv()

_update_env_seed_if_necessary(env, seed=7, worker_idx=1000, vector_idx=999)

self.assertEqual(env.last_seed, 1000 * 1000 + 999 + 7)

def test_update_env_seed_rejects_too_large_vector_idx(self):
env = SeedRecordingEnv()

with self.assertRaisesRegex(
AssertionError, "Too many envs per worker. Random seeds may collide."
):
_update_env_seed_if_necessary(
env, seed=7, worker_idx=0, vector_idx=1000
)

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.

medium

The new tests use the magic number 1000, which corresponds to max_num_envs_per_env_runner in _update_env_seed_if_necessary. This makes the tests brittle if the constant in the implementation ever changes.

To improve maintainability, consider defining this value as a constant and using it in these tests. This makes the intent clearer and simplifies future updates.

Here's a suggestion that refactors both tests to use a local constant:

    def test_update_env_seed_accepts_max_worker_idx_with_valid_vector_idx(self):
        # This value is hardcoded in `_update_env_seed_if_necessary`.
        max_num_envs_per_env_runner = 1000
        env = SeedRecordingEnv()
        worker_idx = max_num_envs_per_env_runner
        vector_idx = max_num_envs_per_env_runner - 1
        seed = 7

        _update_env_seed_if_necessary(
            env, seed=seed, worker_idx=worker_idx, vector_idx=vector_idx
        )

        self.assertEqual(
            env.last_seed,
            worker_idx * max_num_envs_per_env_runner + vector_idx + seed,
        )

    def test_update_env_seed_rejects_too_large_vector_idx(self):
        # This value is hardcoded in `_update_env_seed_if_necessary`.
        max_num_envs_per_env_runner = 1000
        env = SeedRecordingEnv()

        with self.assertRaisesRegex(
            AssertionError, "Too many envs per worker. Random seeds may collide."
        ):
            _update_env_seed_if_necessary(
                env, seed=7, worker_idx=0, vector_idx=max_num_envs_per_env_runner
            )
Signed-off-by: nathon-lee <leejianwoo@gmail.com>
@ray-gardener ray-gardener Bot added rllib RLlib related issues community-contribution Contributed by the community labels Mar 18, 2026

@pseudo-rnd-thoughts pseudo-rnd-thoughts left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Personally, I think that this assert shouldn't exist as we starting to work with users that certainly have more than a thousand environments per env-runner.
Its easier just not to have this assert and let the user get an error if the sub-environment doesn't exist anymore.
@ArturNiederfahrenhorst

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
@nathon-lee

Copy link
Copy Markdown
Contributor Author

Personally, I think that this assert shouldn't exist as we starting to work with users that certainly have more than a thousand environments per env-runner. Its easier just not to have this assert and let the user get an error if the sub-environment doesn't exist anymore. @ArturNiederfahrenhorst

Good point — I removed the assert instead of switching it to vector_idx, and updated the test to verify that large vector_idx values are allowed.

Comment thread rllib/evaluation/rollout_worker.py
@ArturNiederfahrenhorst

Copy link
Copy Markdown
Contributor

Agreed. The assertion does not make sense anymore.

@nathon-lee

Copy link
Copy Markdown
Contributor Author

@ArturNiederfahrenhorst @pseudo-rnd-thoughts Thanks! I’ve pushed the fix.
If it looks good now, could you please merge the PR when you have time?

@github-actions

github-actions Bot commented Apr 8, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Apr 8, 2026
Comment thread rllib/evaluation/tests/test_rollout_worker.py
Comment thread rllib/evaluation/rollout_worker.py Outdated
@@ -132,9 +132,6 @@ def _update_env_seed_if_necessary(
# A single RL job is unlikely to have more than 10K
# rollout workers.
max_num_envs_per_env_runner: int = 1000

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should be remove as well now along with the comment above

self.assertTrue(len(unroll_ids_2) > 1)
ev.stop()

def test_update_env_seed_accepts_max_worker_idx_with_valid_vector_idx(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Merge these two tests together

def test_update_env_seed(self):
        env = SeedRecordingEnv()

        _update_env_seed_if_necessary(env, seed=7, worker_idx=0, vector_idx=1000)
        self.assertEqual(env.last_seed, 1007)

        _update_env_seed_if_necessary(env, seed=7, worker_idx=1000, vector_idx=999)
        self.assertEqual(env.last_seed, 1000 * 1000 + 999 + 7)
@pseudo-rnd-thoughts

Copy link
Copy Markdown
Member

@nathon-lee Could you make these two changes then we're good to merge

@pseudo-rnd-thoughts pseudo-rnd-thoughts removed the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Apr 9, 2026
@nathon-lee

nathon-lee commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

@nathon-lee如果您能做这两项更改,我们就可以合并了。

ok , @pseudo-rnd-thoughts hi, I've made the revisions as per your suggestion, and it's ready to merge

@pseudo-rnd-thoughts pseudo-rnd-thoughts added the go add ONLY when ready to merge, run all tests label Apr 9, 2026

@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 and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 13d5812. Configure here.

Comment thread rllib/evaluation/rollout_worker.py
@pseudo-rnd-thoughts

Copy link
Copy Markdown
Member

The CI seems to be failing for reasons unrelated to this PR

@nathon-lee

Copy link
Copy Markdown
Contributor Author

The CI seems to be failing for reasons unrelated to this PR

I re-ran the CI, but it still failed with the same ray.init() timeout. It seems to be an unrelated flaky test. Could a maintainer please take a look when you have a chance? @pseudo-rnd-thoughts

@pseudo-rnd-thoughts pseudo-rnd-thoughts changed the title fix: Fix wrong assert variable in _update_env_seed_if_necessary Apr 10, 2026
@pseudo-rnd-thoughts

Copy link
Copy Markdown
Member

@nathon-lee Thanks, I lost track of this PR, yeah, we just need to update the branch to fix it

@ArturNiederfahrenhorst ArturNiederfahrenhorst merged commit 781d562 into ray-project:master Apr 14, 2026
6 checks passed
@nathon-lee

Copy link
Copy Markdown
Contributor Author

@nathon-lee Thanks, I lost track of this PR, yeah, we just need to update the branch to fix it

Thanks, appreciate the merge!

HLDKNotFound pushed a commit to chichic21039/ray that referenced this pull request Apr 22, 2026
…ay-project#61823)

## Description

This PR fixes a small bug in `rllib`'s `_update_env_seed_if_necessary()`
helper.

The assert in that function was checking `worker_idx <
max_num_envs_per_env_runner`, but the boundary there should apply to
`vector_idx`, not `worker_idx`. Because of this, valid cases with a
large `worker_idx` could be rejected incorrectly, while the intended
guard is to ensure that `vector_idx` stays within the per-worker env
limit used in the seed calculation.

This PR makes the fix by updating the assert to:

- `vector_idx < max_num_envs_per_env_runner`

and adds a regression test covering the boundary behavior:
- a valid large `worker_idx` with an in-range `vector_idx` should
succeed
- an out-of-range `vector_idx` should still raise

The change is intentionally minimal and should be low risk.

## Related issues

Fixes ray-project#61593

## Additional information

I kept the change narrowly scoped to the incorrect assert and the
corresponding regression test.

I was not able to fully run the targeted test locally in this checkout
because the local environment is missing the compiled `ray._raylet`
module, but the logic change is straightforward and the added test is
designed specifically to prevent regression for this boundary condition.

---------

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Signed-off-by: nathon <leejianwoo@gmail.com>
Lucas61000 pushed a commit to Lucas61000/ray that referenced this pull request May 15, 2026
…ay-project#61823)

## Description

This PR fixes a small bug in `rllib`'s `_update_env_seed_if_necessary()`
helper.

The assert in that function was checking `worker_idx <
max_num_envs_per_env_runner`, but the boundary there should apply to
`vector_idx`, not `worker_idx`. Because of this, valid cases with a
large `worker_idx` could be rejected incorrectly, while the intended
guard is to ensure that `vector_idx` stays within the per-worker env
limit used in the seed calculation.

This PR makes the fix by updating the assert to:

- `vector_idx < max_num_envs_per_env_runner`

and adds a regression test covering the boundary behavior:
- a valid large `worker_idx` with an in-range `vector_idx` should
succeed
- an out-of-range `vector_idx` should still raise

The change is intentionally minimal and should be low risk.

## Related issues

Fixes ray-project#61593

## Additional information

I kept the change narrowly scoped to the incorrect assert and the
corresponding regression test.

I was not able to fully run the targeted test locally in this checkout
because the local environment is missing the compiled `ray._raylet`
module, but the logic change is straightforward and the added test is
designed specifically to prevent regression for this boundary condition.

---------

Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Signed-off-by: nathon <leejianwoo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community go add ONLY when ready to merge, run all tests rllib RLlib related issues

3 participants