Prevent recursive symlink creation iwhen save_last='link' and save_top_k=-1#21186
Merged
Borda merged 7 commits intoLightning-AI:masterfrom Sep 10, 2025
Merged
Conversation
…in ModelCheckpoint - Added a check in _link_checkpoint to compare absolute paths of filepath and linkpath - Only create symlink if paths differ, avoiding self-linking when save_last='link' and save_top_k=-1 - Updated test to assert the fix prevents the recursive symlink bug
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug in PyTorch Lightning's ModelCheckpoint callback where using save_last='link' with save_top_k=-1 creates a recursive symlink that points to itself.
- Adds path comparison logic to prevent self-referencing symlinks in
_link_checkpointmethod - Updates test coverage to verify the fix works correctly
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/lightning/pytorch/callbacks/model_checkpoint.py | Adds absolute path comparison to prevent recursive symlink creation |
| tests/tests_pytorch/callbacks/test_model_checkpoint_additional_cases.py | Adds comprehensive regression test for the symlink bug scenario |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
SkafteNicki
reviewed
Sep 9, 2025
tests/tests_pytorch/callbacks/test_model_checkpoint_additional_cases.py
Outdated
Show resolved
Hide resolved
save_last='link' and save_top_k=-1
SkafteNicki
approved these changes
Sep 10, 2025
Borda
approved these changes
Sep 10, 2025
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.
Fix ModelCheckpoint recursive symlink creation when save_last='link' and save_top_k=-1
What does this PR do?
This PR fixes a bug in PyTorch Lightning's ModelCheckpoint callback where using
save_last='link'withsave_top_k=-1creates a recursive symlink (last.ckpt -> last.ckpt). This occurs when the last checkpoint saved is thelast.ckptfile itself, leading to a self-referencing symlink.Changes Made:
filepathandlinkpathsave_last='link'andsave_top_k=-1This ensures the checkpoint functionality works correctly without creating invalid symlinks, maintaining backward compatibility.
Fixes #21110
No breaking changes introduced.
📚 Documentation preview 📚: https://pytorch-lightning--21186.org.readthedocs.build/en/21186/