Repository navigation
fix(trainer): clean up the local venv even when the TrainJob fails - #889
Open
SAURABHSALVE wants to merge 1 commit into
Open
SAURABHSALVE wants to merge 1 commit into
SAURABHSALVE wants to merge 1 commit into
Conversation
LOCAL_EXEC_JOB_TEMPLATE starts with `set -e` and ran the cleanup command ($CLEANUP_SCRIPT, `rm -rf $PYENV_LOCATION` when cleanup_venv=True) as the very last statement, after the entrypoint. If the entrypoint (the user's training function) or any earlier setup step -- venv creation, ensurepip, dependency install -- exited non-zero, set -e aborted the script immediately and the cleanup line was never reached. Every failed local TrainJob therefore leaked its per-job virtualenv under the OS temp directory, even with the default cleanup_venv=True. Register the cleanup command as a trap on EXIT right after `set -e`, before venv creation, so it always runs exactly once on any exit path (success, or failure at any step) without altering the script's own exit code, which LocalJob.success depends on. Fixes kubeflow#888 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Signed-off-by: Saurabh Vijay Salve <saurabhsalve9999@gmail.com>
Contributor
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does / why we need it:
LOCAL_EXEC_JOB_TEMPLATE(kubeflow/trainer/backends/localprocess/constants.py) starts withset -eand ran the cleanup command ($CLEANUP_SCRIPT→rm -rf $PYENV_LOCATION, used whenevercleanup_venv=True, the default) as the last statement, after the entrypoint:If the entrypoint (the user's training function) or any earlier setup step — venv creation,
ensurepip, dependency install — exited non-zero,set -eaborted the script immediately and the trailing cleanup line was never reached. So every failedLocalProcessBackendTrainJob permanently leaked its per-job virtualenv under the OS temp directory, regardless ofcleanup_venv=True. Only jobs that completed successfully were ever cleaned up — exactly backwards from what matters during normal local iteration, where failures are common.Fix: register the cleanup command as a
trap ... EXITimmediately afterset -e, before venv creation, instead of as a trailing statement. A bash EXIT trap always runs exactly once on any exit path and does not alter the script's own exit status (verified empirically — see below), whichLocalJob.successdepends on (self._success = self._process.returncode == 0).Verification
kubeflow/trainer/backends/localprocess/utils_test.py(this module previously had no dedicated unit tests for its script-building helpers) with two regression tests on the real generated script string: the trap is registered before venv creation and the cleanup command is not a bare trailing statement after the entrypoint; and acleanup_venv=Falsecase still registers a harmless no-op trap.mainfor the right reason (notrapin the script; cleanup command is a trailing statement) and pass on this branch.pytest kubeflow/trainer/backends/localprocess/→ 29 passed (no regressions in the rest of the module).set -e; trap 'echo cleanup-ran' EXIT; false→ cleanup runs, script still exits 1; same withtrue→ exits 0), then ran the real generated script end-to-end through Git Bash for both a failing and a succeeding training function with POSIX-normalized paths (this dev machine's ownpython -m venvlayout and backslash-path handling hit two unrelated, already-tracked Windows issues — venvbin/vsScripts/layout and backslash path mangling, same family as fix(trainer): resolve LocalProcessBackend Windows compatibility issues #762/bug(trainer): container paths are built with backslashes on a Windows client #848/fix(trainer): build container paths with POSIX separators #849 — so paths were normalized only to exercise the trap itself): in both the failing and succeeding case, and even when the script failed later at an unrelated step, the venv directory did not survive, confirming cleanup fires on every exit path, not just the entrypoint.ruff check,ruff format --check, andty check kubeflow/trainer/backends/localprocesson the changed files show no new issues (pre-existing diagnostics injob.py/utils.pyare all on unrelated lines and present onmaintoo).Which issue(s) this PR fixes:
Fixes #888
Checklist:
cleanup_venv's documented behavior (remove the venv after training) is unchanged; this fixes it to actually hold on the failure path too.🤖 Generated with Claude Code