Skip to content

[train] Give each TrainContext its own checkpoint upload pool and report condition - #68651

Open
AnvitDevadiga wants to merge 2 commits into
ray-project:masterfrom
AnvitDevadiga:fix/train-context-per-instance-threadpool
Open

AnvitDevadiga wants to merge 2 commits into
ray-project:masterfrom
AnvitDevadiga:fix/train-context-per-instance-threadpool

Conversation

@AnvitDevadiga

Copy link
Copy Markdown

Description

TrainContext set checkpoint_upload_threadpool and report_order_condition as class-level dataclass defaults, so every instance shared one ThreadPoolExecutor and one threading.Condition. RayTrainWorker.run_train_fn shuts the pool down when the training function returns (worker.py), so any later TrainContext in the same process got that shut-down pool, and its first checkpoint upload raised RuntimeError: cannot schedule new futures after shutdown.

Both fields now use field(default_factory=...), as health_lock in the same dataclass already does. Every use of the two fields goes through an instance (self. or get_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 for TrainContext threadpool and checkpoint_upload_threadpool, return nothing).

Tests run (Ray nightly wheel 3.0.0.dev0, with this branch's python/ray/train and python/ray/tests linked into it, as setup-dev.py does):

  • New test_later_train_context_gets_its_own_checkpoint_upload_threadpool in python/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 with RuntimeError: 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.py and test_v2_api.py need boto3, torch and lightgbm, which I don't have installed; test_worker_group.py::test_zombie_actor_termination needs the Ray dashboard's state API and fails the same way on master in my environment.
  • ruff 0.17.3 and black 22.10.0 pass on the changed files.

I used Opus 5.5 to analyze the issue, reviewed the code myself, and implemented the fix.

…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]>
@AnvitDevadiga
AnvitDevadiga requested a review from a team as a code owner November 29, 2026 13:36

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

Comment on lines +79 to +85
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()

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

Suggested change
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]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Ray Train v2 TrainContext dataclass shares class-level ThreadPoolExecutor and Condition, crashing new instances after shutdown

1 participant