acoustic-carpenter-78188
06/12/2026, 10:56 PMfailure_policy.max_restarts times inside a single Flyte attempt. Two things were broken in that flow:
1. error.pb was written on every failed attempt to the same output path. A later successful restart left the stale error.pb behind, so the run could be misreported as FAILED despite succeeding. (The original version of this PR worked around it by deleting the stale file on success — fragile, and it left a window where the error could be read mid-restart.)
2. A task failure never produced a non-zero pod exit. The runtime's contract is "write error.pb, exit 0" (correct for normal tasks, where the backend reads error.pb). But the JobSet controller keys its gang-restart off the pod exit code, not error.pb. So a clustered worker that hit an exception exited 0 → torchrun exited 0 → the Job "succeeded" → the JobSet never restarted, even with max_restarts > 0.
Summary
Replaces the delete-on-success workaround with two complementary fixes:
1. Write error.pb only on the terminal JobSet attempt (io.py)
• upload_error skips the write on transient restarts and only writes once the restart budget is exhausted (JOBSET_RESTART_ATTEMPT >= JOBSET_MAX_RESTARTS).
• New helpers _get_clustered_max_restarts() / _is_terminal_clustered_attempt(). Safe fallback: if the budget env is absent (older operator) or the task isn't clustered, it writes as before — errors are never silently hidden.
• Removed _clear_stale_clustered_error_if_needed / _delete_path (no stale file is ever written now, so there's nothing to delete).
2. Clustered workers exit non-zero on failure (taskrunner.py)
• After the (now gated) error.pb handling, a clustered worker (TORCHELASTIC_RUN_ID present) calls sys.exit(1) so torchrun → Job → JobSet detects the failure and triggers the gang restart. Normal tasks keep the exit-0 contract.
• Added _is_clustered_worker() (refactored out of _is_nonzero_rank_clustered_worker).
Together: the non-zero exit makes the JobSet climb through restart attempts; the terminal-only error.pb write keeps those transient attempts from poisoning the eventual success.
Test Plan
• Unit — `pytest tests/flyte/internal/runtime/test_taskrunner.py tests/flyte/clustered/`:
• upload_error gating: transient attempt → no write; terminal attempt → write; non-clustered → write; missing-budget-env fallback → write; max_restarts=0 → write; non-zero rank → no write; upload_outputs no longer deletes.
• exit behavior: clustered failure → `SystemExit(1)`; non-clustered failure → no exit; clustered success → no exit.
• End-to-end — examples/clustered/ddp_train_restart.py (max_restarts=1, fails on attempt 0): with the new wheel and the operator deployed, attempt 0 exits non-zero → JobSet restarts → attempt 1 trains → run SUCCEEDED with no stale error.pb.
flyteorg/flyte-sdkacoustic-carpenter-78188
06/18/2026, 9:51 PM