Which exits publish the last checkpoint when the flag is on, and which one still does not

NVIDIA-NeMo/RL #4859, flag async_rl.defer_async_checkpoint_finalize: true. The bug links pin 0a4103fb, the head first reviewed; the fix links pin the current head 8841e8e4.

SFT v2SingleController (GRPO / PPO)
mainEvery exit publishes the last save: run()'s cleanup calls CheckpointManager.shutdown(), which waits for the background thread that finalizes and renames it.
First push (flag on)Only a normal exit finalizes. An error after a save leaves it as tmp_step_N/.Same for errors, and for the normal end of an epoch-limited run: the loop leaves through an early return that skips the final finalize.
Now (5f93e1bae)The cleanup loop publishes the pending save first, before checkpoint close.Teardown publishes the pending save right after the pumps stop, before checkpoint close, in its own try/except.
StatusPartly fixed. The flag introduced this bug. 5f93e1bae fixes every common exit (table below). One narrow case is still open: the train loop is stopped while it waits for the checkpoint lock (review comment C7). Runs with the flag off were never affected.

Three pages on this PR, one question each β€” this one: which exits lose the last save.

As first pushed, with the flag on, a save was published only by _finish_pending_save. The train loop calls it at the top of each step and once more, blocking, after the loop (L3906). But a run limited by epochs ends differently: when the data is used up, the last step is marked final and saves (L3725), the loop goes round once more, finds nothing to train, and leaves through a plain return that skips L3906. The GRPO example config ends this way: max_num_epochs: 1, max_num_steps: 1000000.

The last two steps of a 2-step run, with and without the flag

step 2 — data used up, saves loop goes round once more run() cleanup main save → thread will finalize nothing to train → return shutdown() waits → finalize disk at exit: step_1/ step_2/ shutdown() joins the thread flag on save → marked pending poll→False return (L3115) shutdown(): nothing to wait disk at exit: step_1/ tmp_step_2/ skipped: L3906 blocking finalize cleanup ignores the pending save last save published where the pending save is dropped quick check, write not done yet The process still exits 0. The workers' final metadata write and barrier never run, so renaming tmp_step_2/ by hand does not give a complete checkpoint. Resume starts from step_1. Reproduced on CPU: the existing exhaustion test, run with the flag on, ends with {step_1, tmp_step_2}.
  1. Step 2 is the last step because the data is used up, so it saves. With the flag on, the save is only marked pending.
  2. The loop condition only checks max_num_steps, so the loop runs once more. Its first poll comes right after the save, so the write is not done: False.
  3. No batch is left, so the loop returns and never reaches the blocking finalize after it.
  4. run()'s cleanup calls CheckpointManager.shutdown(). The manager was never told about the pending save, so it does nothing.

An error in the step after a save takes the same route to the cleanup, in both the SingleController and SFT v2, and drops the save the same way. On main, that cleanup publishes it.

The fix β€” landed in this PR (5f93e1bae)

Where the fix lands: RL only, two functions. Each loop now publishes a still-pending save in its teardown, before checkpoint close. One spot covers every exit: the early return, errors, and cancellation. The finalize after the loop stays; the teardown call does nothing once no save is pending.

@@ single_controller.py L941-L951 Β· SingleControllerActor._run_pumps (finally)
             for task in tasks:
                 task.cancel()
             await asyncio.gather(*tasks, return_exceptions=True)
+            try:
+                # With the train pump stopped, publish a save that
+                # defer_async_checkpoint_finalize left pending: a failure or an
+                # orderly rollout-checkpoint stop ends the pump before its own
+                # blocking finalize. Before checkpoint close, which joins the rename.
+                await self._finish_pending_save(blocking=True)
+            except Exception as e:  # teardown must not mask the original failure
+                print(f"Error publishing the pending checkpoint: {e}", flush=True)
             if self._engine_supervisor is not None:
@@ sft_v2.py L257-L265 Β· SFTSingleControllerActor.run (finally)
         finally:
             for cleanup, name in (
+                # A save deferred by defer_async_checkpoint_finalize is published only
+                # here when the run raised after it. Before checkpoint close, so that
+                # close joins the rename this starts.
+                (
+                    lambda: self._finish_pending_save(blocking=True),
+                    "pending checkpoint publish",
+                ),
                 (self._close_loaders, "loader close"),
  1. Both call the PR's own _finish_pending_save(blocking=True): wait for the write on every worker, then rename the folder. Main's cleanup already did this through shutdown(), so a stuck worker hangs here exactly as it does on main.
  2. Both run before checkpoint close, so the close waits for the rename.
  3. A failure in either only prints or warns, so it never hides the error that ended the run.
  4. Checked on CPU: the existing exhaustion test, run with the flag on, now ends with {step_1, step_2}.
How the run ends (flag on)What publishes the last saveChecked
max_num_steps reachedblocking finalize after the loop (L3927; SFT v2 L255)PR's tests
Save-by-time limit (checkpoint_must_save_by)save, then break to the same finalizeCPU test, flag on
Data runs outteardown publish (L949)CPU test, flag on
Error in a later stepteardown publish (SFT v2: first cleanup entry, L259)author's tests (C8 fixes the SFT v2 one)
Orderly stop after a rollout checkpointteardown publishby reading the code
Stopped while waiting for the checkpoint locknothing: the save was already cleared (C7)CPU test fails at head
Process killed (preemption, node loss)nothing can run. Flag off loses an unfinished write the same way; flag on adds up to one step after the write lands.β€”

What is left β€” proposed in the review, not in the PR

_finish_pending_save clears the pending save before waiting for the checkpoint lock. If teardown cancels the train pump during that wait, the workers have already finalized, but the new teardown publish finds nothing pending and tmp_step_N/ stays. Nothing below is implemented: clearing it inside the lock, right after the rename starts, closes the gap.

@@ single_controller.py L5430-L5433 Β· SingleControllerActor._finish_pending_save
         checkpoint_path, step = self._pending_checkpoint
-        self._pending_checkpoint = None
         async with self._checkpoint_save_lock:
             self._checkpointer.begin_finalization(checkpoint_path)
+            # Cleared only once published: a cancel while waiting for the lock
+            # leaves it for the teardown publish.
+            self._pending_checkpoint = None

🟑 review comment C7 β€” single_controller.py:5433