Skip to content

[jobs] Avoid mutating runtime_env during submission - #63990

Merged
edoakes merged 1 commit into
ray-project:masterfrom
neuyilan:fix/job-submit-runtime-env-mutation
Jun 10, 2026
Merged

edoakes merged 1 commit into
ray-project:masterfrom
neuyilan:fix/job-submit-runtime-env-mutation

Conversation

@neuyilan

@neuyilan neuyilan commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Why are these changes needed?

Fixes #46625.

JobSubmissionClient.submit_job() uploaded local runtime_env resources by mutating the user-provided runtime_env dict in place. This could replace local working_dir / py_modules entries with gcs:// URIs. If the same runtime_env object was reused after the temporary package had been garbage-collected, the second submission would skip the local package existence/upload path and later fail during runtime env setup.

This change copies runtime_env at the Jobs SDK boundary before upload helpers rewrite local paths to package URIs.

Related issue number

Fixes #46625

Checks

  • python -m py_compile python/ray/dashboard/modules/job/sdk.py python/ray/dashboard/modules/job/tests/test_sdk.py
  • PYTHONPATH=/home/houliangqi/codespace/github.com/ray-project/ray/python python -m pytest -q python/ray/dashboard/modules/job/tests/test_sdk.py::test_submit_job_does_not_mutate_runtime_env
  • pre-commit run --files python/ray/dashboard/modules/job/sdk.py python/ray/dashboard/modules/job/tests/test_sdk.py

Note: python/ray/dashboard/modules/job/tests/test_sdk.py::test_temporary_uri_reference[0] was attempted locally, but the local Ray cluster failed to start because GCS server port discovery timed out before test assertions ran.

@neuyilan
neuyilan requested a review from a team as a code owner June 10, 2026 13:35

@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 ensures that the user-provided runtime_env dictionary is not mutated during job submission by creating a deep copy of it. It also adds unit and regression tests to verify this behavior. The reviewer suggested a similar improvement for the metadata dictionary to prevent it from being mutated in-place when updating with default metadata.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

)

runtime_env = runtime_env or {}
runtime_env = copy.deepcopy(runtime_env or {})

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

Similar to runtime_env, the user-provided metadata dictionary is also mutated in-place when metadata.update(self._default_metadata) is called on line 223. If a user reuses the same metadata dictionary across multiple job submissions, it will accumulate default metadata from previous submissions, which can lead to unexpected side effects.

To prevent this, we should create a copy of metadata before updating it:

metadata = dict(metadata or {})
metadata.update(self._default_metadata)

@neuyilan
neuyilan force-pushed the fix/job-submit-runtime-env-mutation branch from 5a8d0c4 to e632717 Compare June 10, 2026 13:41
@ray-gardener ray-gardener Bot added dashboard Issues specific to the Ray Dashboard core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Jun 10, 2026

@edoakes edoakes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@edoakes edoakes added the go add ONLY when ready to merge, run all tests label Jun 10, 2026
@edoakes
edoakes enabled auto-merge (squash) June 10, 2026 17:33
@edoakes
edoakes merged commit 2d8a749 into ray-project:master Jun 10, 2026
4 checks passed
limarkdcunha pushed a commit to limarkdcunha/ray that referenced this pull request Jun 30, 2026
Fixes ray-project#46625.

`JobSubmissionClient.submit_job()` uploaded local runtime_env resources
by mutating the user-provided `runtime_env` dict in place. This could
replace local `working_dir` / `py_modules` entries with `gcs://` URIs.
If the same `runtime_env` object was reused after the temporary package
had been garbage-collected, the second submission would skip the local
package existence/upload path and later fail during runtime env setup.

This change copies `runtime_env` at the Jobs SDK boundary before upload
helpers rewrite local paths to package URIs.

## Related issue number

Fixes ray-project#46625

## Checks

- `python -m py_compile python/ray/dashboard/modules/job/sdk.py
python/ray/dashboard/modules/job/tests/test_sdk.py`
-
`PYTHONPATH=/home/houliangqi/codespace/github.com/ray-project/ray/python
python -m pytest -q
python/ray/dashboard/modules/job/tests/test_sdk.py::test_submit_job_does_not_mutate_runtime_env`
- `pre-commit run --files python/ray/dashboard/modules/job/sdk.py
python/ray/dashboard/modules/job/tests/test_sdk.py`

Note:
`python/ray/dashboard/modules/job/tests/test_sdk.py::test_temporary_uri_reference[0]`
was attempted locally, but the local Ray cluster failed to start because
GCS server port discovery timed out before test assertions ran.

Signed-off-by: qihouliang <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
Fixes ray-project#46625.

`JobSubmissionClient.submit_job()` uploaded local runtime_env resources
by mutating the user-provided `runtime_env` dict in place. This could
replace local `working_dir` / `py_modules` entries with `gcs://` URIs.
If the same `runtime_env` object was reused after the temporary package
had been garbage-collected, the second submission would skip the local
package existence/upload path and later fail during runtime env setup.

This change copies `runtime_env` at the Jobs SDK boundary before upload
helpers rewrite local paths to package URIs.

## Related issue number

Fixes ray-project#46625

## Checks

- `python -m py_compile python/ray/dashboard/modules/job/sdk.py
python/ray/dashboard/modules/job/tests/test_sdk.py`
-
`PYTHONPATH=/home/houliangqi/codespace/github.com/ray-project/ray/python
python -m pytest -q
python/ray/dashboard/modules/job/tests/test_sdk.py::test_submit_job_does_not_mutate_runtime_env`
- `pre-commit run --files python/ray/dashboard/modules/job/sdk.py
python/ray/dashboard/modules/job/tests/test_sdk.py`

Note:
`python/ray/dashboard/modules/job/tests/test_sdk.py::test_temporary_uri_reference[0]`
was attempted locally, but the local Ray cluster failed to start because
GCS server port discovery timed out before test assertions ran.

Signed-off-by: qihouliang <[email protected]>
Signed-off-by: 400Ping <[email protected]>
marwan116 added a commit that referenced this pull request Oct 20, 2026
Since 2.57, JobSubmissionClient.submit_job() raises before uploading
anything when py_modules contains an imported module, one of the entry
types the runtime_env docs list:

    import my_module
    client.submit_job(entrypoint="...", runtime_env={"py_modules": [my_module]})
    TypeError: cannot pickle 'module' object

#63990 made submit_job copy runtime_env with copy.deepcopy, so that
uploading no longer rewrites the caller's dict with gcs:// URIs (#46625).
Module objects can't be deep-copied, so the copy itself raises.

A shallow copy is enough for #46625: the working_dir and py_modules
upload helpers only rebind top-level keys and never mutate a value in
place. Path strings, URIs and `ray job submit` were never affected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Marwan Sarieddine <[email protected]>
Sparks0219 pushed a commit that referenced this pull request Oct 20, 2026
## What broke

Since 2.57, `JobSubmissionClient.submit_job()` raises before it uploads
anything when `py_modules` contains an imported module, which the
[runtime_env API
reference](https://docs.ray.io/en/latest/ray-core/handling-dependencies.html#runtime-environments-api-ref)
lists as a supported entry:

```python
import my_module
from ray.job_submission import JobSubmissionClient

client = JobSubmissionClient("http://127.0.0.1:8265")
client.submit_job(entrypoint="python main.py", runtime_env={"py_modules": [my_module]})
```

```
  File ".../ray/dashboard/modules/job/sdk.py", line 221, in submit_job
    runtime_env = copy.deepcopy(runtime_env or {})
TypeError: cannot pickle 'module' object
```

Expected: the module's directory is uploaded and the job runs, as on
2.56.1. A user upgrading from 2.56.1 hit this and is pinned there.

| `py_modules` entry | 2.56.1 | 2.57.0, 2.58.0, master |
|---|---|---|
| module object, Python SDK | works | `TypeError` at submit |
| path string, Python SDK | works | works |
| path string, `ray job submit --runtime-env-json` | works | works |

## Why

#63990 (the fix for #46625) made `submit_job` deep-copy `runtime_env`,
so that uploading stops rewriting the caller's dict with `gcs://` URIs.
Module objects can't be deep-copied, so the copy itself raises. Nothing
in CI passes a module object through `submit_job`, so it went unnoticed.

## The fix

Copy shallowly: `dict(runtime_env or {})`.

- That is enough for #46625. `upload_working_dir_if_needed` and
`upload_py_modules_if_needed` only rebind top-level keys
(`runtime_env["py_modules"] = py_modules_uris`, a new list) and never
mutate a value in place, so the caller's dict and list are left alone.
- A `RuntimeEnv` argument becomes a plain dict before upload and is
rebuilt by the existing `RuntimeEnv(**runtime_env)` right after.
`RuntimeEnv` validation rejects module objects, so only a plain dict can
carry one.

## Not a duplicate

No open issue or PR covers this. The open PRs in the area don't touch
the submission path: #64621 changes CLI stack traces (`cli.py`,
`cli_utils.py`), #66570 changes how nodes install `py_modules` wheels,
and #65933 and #66279 change how nodes download packages. Nothing on
#63990 mentions module objects.

## Testing

`test_submit_job_with_module_object_in_py_modules` runs the real upload
path behind a fake HTTP layer and checks that the module's directory is
zipped with its parent (`my_module/__init__.py`), that the job request
carries a `gcs://` URI, and that the caller's dict still holds the
module. Against the unmodified `sdk.py` it fails with `TypeError: cannot
pickle 'module' object`.

```
$ python -m pytest -v python/ray/dashboard/modules/job/tests/test_sdk.py
110 passed in 141.93s

$ python -m pytest -v python/ray/dashboard/modules/job/tests/test_http_job_server.py
1 failed, 63 passed in 348.11s
```

The one failure, `test_submit_job[conda_yaml]`, times out because conda
isn't installed on the test machine; it times out the same way with the
unmodified `sdk.py`.

End to end on a local cluster, `submit_job(runtime_env={"py_modules":
[my_module]})` now succeeds and the driver imports the module; the same
script raises the `TypeError` with the 2.58.0 wheel. Pre-commit passes
on both changed files.

AI assistance was used for this change.

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

---------

Signed-off-by: Marwan Sarieddine <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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 core Issues that should be addressed in Ray Core dashboard Issues specific to the Ray Dashboard go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Ray Core] GCS expires due to unmodified files in the working-dir

2 participants