Skip to content

[tune] Stop Repeater raising KeyError on unknown trial IDs - #65952

Closed
akashjainn wants to merge 1 commit into
ray-project:masterfrom
akashjainn:fix/repeater-unknown-trial-keyerror
Closed

akashjainn wants to merge 1 commit into
ray-project:masterfrom
akashjainn:fix/repeater-unknown-trial-keyerror

Conversation

@akashjainn

Copy link
Copy Markdown

Why are these changes needed?

Repeater.on_trial_complete guards against a trial that is missing from its
_trial_id_to_group mapping, logs "Trial {} not in group; cannot report score",
and then immediately indexes that same mapping anyway:

if trial_id not in self._trial_id_to_group:
    logger.error(
        "Trial {} not in group; cannot report score. "
        "Seen trials: {}".format(trial_id, list(self._trial_id_to_group))
    )
trial_group = self._trial_id_to_group[trial_id]   # KeyError

The log message says the score cannot be reported, but there is no return, so
the next line raises KeyError and the exception propagates up through
SearchGenerator.on_trial_complete into _process_trial, taking down the whole
experiment rather than skipping the one trial.

This is reachable on resume. Tune checkpoints the search algorithm and the trial
state on separate cadences, so a trial suggested after the last search algorithm
checkpoint is still restored from the trial state while being absent from the
restored _trial_id_to_group. When it completes, Repeater has never seen the
ID. That matches the original report, where resume fails when the last trial
before the interruption was incomplete.

This change adds the missing return so the trial is skipped, which is the
behavior the existing log message already describes. The group bookkeeping for
trials the Repeater does know about is unchanged.

Worth noting for reviewers: the 2020 comment on the issue suggested the cause was
that save/restore were not implemented for Repeater. That is no longer the
case. SearchGenerator.save_to_dir / restore_from_dir walk the wrapper chain
and call get_state() / set_state() on each wrapper, so Repeater state is
checkpointed today. What remains is only the missing early return on the
unknown-trial path.

Related issue number

Closes #11918

Not a duplicate

  • gh pr list --repo ray-project/ray --state open --search "11918 in:body" → no results
  • gh pr list --repo ray-project/ray --state all --search "repeater in:title" → no results
  • The issue has no linked PR and no open cross-referenced PR.

Checks

Environment: Python 3.11.16, ray[tune] 2.58.0 in a venv, with python/ray/tune
symlinked to this branch via python/ray/setup-dev.py -y --allow tune.

Added test_repeater_ignores_unknown_trial, which reproduces the reported
failure. Before the fix it fails with the same signature as the issue report:

ERROR repeater.py:161 -- Trial unknown not in group; cannot report score. Seen trials: ['known_1']
>       trial_group = self._trial_id_to_group[trial_id]
E       KeyError: 'unknown'

After the fix:

$ pytest ray/tune/tests/test_searcher_utils.py -q
...........                                                              [100%]
11 passed in 0.02s

$ pytest ray/tune/tests/execution/test_controller_search_alg_integration.py -q
........ss.........                                                      [100%]
17 passed, 2 skipped in 45.90s

Lint:

$ pre-commit run --files python/ray/tune/search/repeater.py python/ray/tune/tests/test_searcher_utils.py
# all hooks pass

Commit is DCO signed off.

AI assistance

Per AGENTS.md: AI assistance (Claude Code) was used for this change. The
diagnosis, the fix, and the regression test were reviewed and the tests were run
locally as shown above before opening this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QtvvymoPBWrpb7JZQ4zEBc

Repeater.on_trial_complete logs "cannot report score" when a trial is
missing from _trial_id_to_group, then falls through and indexes that same
mapping, raising KeyError and aborting the experiment.

On resume, Tune can restore a trial that was suggested after the last
search algorithm checkpoint was written, so the restored mapping does not
contain that trial. Return after the log so the trial is skipped, which is
what the log message already claims happens.

Closes ray-project#11918

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01QtvvymoPBWrpb7JZQ4zEBc
Signed-off-by: akashjainn <[email protected]>
@akashjainn
akashjainn requested a review from a team as a code owner September 4, 2026 22:28

@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 prevents a KeyError in Repeater.on_trial_complete when a trial is not found in the _trial_id_to_group mapping by adding an early return statement. It also introduces a corresponding unit test, test_repeater_ignores_unknown_trial, to verify this behavior. There are no review comments, and I have no additional feedback to provide.

@akashjainn

Copy link
Copy Markdown
Author

Closing this — I could not substantiate the impact claim in the description, and I would rather withdraw it than spend reviewer time on it.

What I got wrong: the description asserts that Tune checkpoints trial state and search algorithm state on separate cadences, leaving a window where a resumed trial is missing from the restored _trial_id_to_group. That is not true. TuneController.save_to_dir writes the trial state, the searcher state, and the callback state in a single call, so they do not drift apart the way I described. I asserted that mechanism without verifying it.

What I found when I did verify it, on 2.58 with Repeater(HyperOptSearch(...), repeat=10):

Scenario Repeater.on_trial_complete calls unknown-trial hits Result
Normal run (control, to prove the probe fires) 40 0 —
Interrupt with SIGINT, then Tuner.restore(...) 0 0 resumed cleanly
Same, with the searcher checkpoint forced to a stale copy knowing only 9 of 40 trials 0 0 resumed cleanly

Restored trials do not report back through the search algorithm on resume, so on_trial_complete is not called at all — even when I deliberately created the exact on-disk divergence my theory required. The KeyError branch does not appear reachable through the resume flow any more.

The one-line change is still defensible on its own terms: the branch logs "cannot report score" and then unconditionally indexes the mapping it just found the key missing from, so it raises instead of skipping. But as an unreachable path with no demonstrated user impact, it is closer to the "no low-value busywork PRs" case in AGENTS.md than to something worth a review cycle, so I will not push it.

Separately, and more usefully: #11918 looks stale. The 2020 traceback does not reproduce on current Ray, and the fix appears to have come from changes elsewhere in the resume path rather than in repeater.py. Might be worth closing.

Apologies for the noise.

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.

[tune] Resume fails when using Repeater if last trial before resume is incomplete

1 participant