Repository navigation
[jobs] Avoid mutating runtime_env during submission - #63990
Conversation
There was a problem hiding this comment.
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 {}) |
There was a problem hiding this comment.
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)Signed-off-by: qihouliang <[email protected]>
5a8d0c4 to
e632717
Compare
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]>
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]>
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]>
## 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]>
Why are these changes needed?
Fixes #46625.
JobSubmissionClient.submit_job()uploaded local runtime_env resources by mutating the user-providedruntime_envdict in place. This could replace localworking_dir/py_modulesentries withgcs://URIs. If the sameruntime_envobject 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_envat 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.pyPYTHONPATH=/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_envpre-commit run --files python/ray/dashboard/modules/job/sdk.py python/ray/dashboard/modules/job/tests/test_sdk.pyNote:
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.