Skip to content

Conversation

littlebullGit
Copy link
Contributor

@littlebullGit littlebullGit commented Sep 9, 2025

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' with save_top_k=-1 creates a recursive symlink (last.ckpt -> last.ckpt). This occurs when the last checkpoint saved is the last.ckpt file itself, leading to a self-referencing symlink.

Changes Made:

  • 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

This 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/

…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
@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Sep 9, 2025
@SkafteNicki SkafteNicki requested a review from Copilot September 9, 2025 03:49
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_checkpoint method
  • 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.

@Borda Borda changed the title Fix for issue #21110: Prevent recursive symlink creation iwhen save_last='link' and save_top_k=-1 Prevent recursive symlink creation iwhen save_last='link' and save_top_k=-1 Sep 9, 2025
@Borda Borda merged commit 9eb3740 into Lightning-AI:master Sep 10, 2025
83 of 90 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pl Generic label for PyTorch Lightning package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ModelCheckpoint options save_last='link' and save_top_k=-1 cause recursive symlink creation
3 participants