Repository navigation
[train] Give each TrainContext its own checkpoint upload pool and report condition - #68651
AnvitDevadiga wants to merge 2 commits into
Conversation
…ort condition TrainContext set checkpoint_upload_threadpool and report_order_condition as class-level dataclass defaults, so every instance shared one ThreadPoolExecutor and one Condition. The worker shuts the pool down when the training function returns, so any later TrainContext in the same process got the shut-down pool, and submitting a checkpoint upload raised RuntimeError: cannot schedule new futures after shutdown. Create both per instance with field(default_factory=...), as health_lock already is. Fixes ray-project#68645 Signed-off-by: AnvitDevadiga <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request updates TrainContext to use field(default_factory=...) for report_order_condition and checkpoint_upload_threadpool, ensuring that each instance gets its own independent resources and preventing issues where subsequent contexts reuse a shut-down thread pool. A corresponding test has been added to verify this behavior. The review feedback recommends adding a timeout to the new test helper _run_to_completion to avoid potential infinite hangs in the CI pipeline.
| def _run_to_completion(worker, train_fn): | ||
| train_fn_ref = create_autospec(ObjectRefWrapper, instance=True) | ||
| train_fn_ref.get.return_value = train_fn | ||
| worker.run_train_fn(train_fn_ref) | ||
| while worker.poll_status().running: | ||
| time.sleep(0.01) | ||
| return worker.poll_status() |
There was a problem hiding this comment.
The helper function _run_to_completion polls worker.poll_status().running in a while loop without a timeout. If the worker hangs or deadlocks, this test will hang indefinitely, blocking the CI pipeline. Adding a timeout ensures the test fails fast and provides a clear error message.
| def _run_to_completion(worker, train_fn): | |
| train_fn_ref = create_autospec(ObjectRefWrapper, instance=True) | |
| train_fn_ref.get.return_value = train_fn | |
| worker.run_train_fn(train_fn_ref) | |
| while worker.poll_status().running: | |
| time.sleep(0.01) | |
| return worker.poll_status() | |
| def _run_to_completion(worker, train_fn, timeout_s: float = 10.0): | |
| train_fn_ref = create_autospec(ObjectRefWrapper, instance=True) | |
| train_fn_ref.get.return_value = train_fn | |
| worker.run_train_fn(train_fn_ref) | |
| start_time = time.monotonic() | |
| while worker.poll_status().running: | |
| if time.monotonic() - start_time > timeout_s: | |
| raise TimeoutError( | |
| f"Worker did not finish training within {timeout_s} seconds." | |
| ) | |
| time.sleep(0.01) | |
| return worker.poll_status() |
Fail with TimeoutError if a training run does not finish within 10s, so a hung worker cannot block CI. Signed-off-by: AnvitDevadiga <[email protected]>
Description
TrainContextsetcheckpoint_upload_threadpoolandreport_order_conditionas class-level dataclass defaults, so every instance shared oneThreadPoolExecutorand onethreading.Condition.RayTrainWorker.run_train_fnshuts the pool down when the training function returns (worker.py), so any laterTrainContextin the same process got that shut-down pool, and its first checkpoint upload raisedRuntimeError: cannot schedule new futures after shutdown.Both fields now use
field(default_factory=...), ashealth_lockin the same dataclass already does. Every use of the two fields goes through an instance (self.orget_train_context()), and the report ordering is per context (report_call_index), so nothing relied on sharing them.Related issues
Fixes #68645
Additional information
Not a duplicate: the issue has no comments or assignee, and there is no open PR for it (
gh pr list --state open --search "68645 in:body", and searches forTrainContext threadpoolandcheckpoint_upload_threadpool, return nothing).Tests run (Ray nightly wheel
3.0.0.dev0, with this branch'spython/ray/trainandpython/ray/testslinked into it, assetup-dev.pydoes):test_later_train_context_gets_its_own_checkpoint_upload_threadpoolinpython/ray/train/v2/tests/test_worker.py: a first worker runs a training function to completion, then a second worker's training function submits a checkpoint upload. On master the second run fails withRuntimeError: cannot schedule new futures after shutdown; with this change it passes, and the two contexts no longer share the pool or the condition.pytest python/ray/train/v2/tests/test_worker.py: 6 passed, 1 skipped (torch).test_report_fault_tolerance.py,test_logging.py,test_metrics.py,test_train_annotations.py,test_preemption_fault_tolerance.py,test_telemetry.py: all pass.test_async_checkpointing_validation.py,test_circular_imports.pyandtest_v2_api.pyneed boto3, torch and lightgbm, which I don't have installed;test_worker_group.py::test_zombie_actor_terminationneeds the Ray dashboard's state API and fails the same way on master in my environment.I used Opus 5.5 to analyze the issue, reviewed the code myself, and implemented the fix.