Fix free-threaded cleanup race in _replace_dunder_methods#21618
Open
Furkan-rgb wants to merge 1 commit intoLightning-AI:masterfrom
Open
Fix free-threaded cleanup race in _replace_dunder_methods#21618Furkan-rgb wants to merge 1 commit intoLightning-AI:masterfrom
Furkan-rgb wants to merge 1 commit intoLightning-AI:masterfrom
Conversation
justusschock
approved these changes
Mar 30, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #21618 +/- ##
=======================================
- Coverage 87% 87% -0%
=======================================
Files 270 270
Lines 23895 23899 +4
=======================================
+ Hits 20675 20676 +1
- Misses 3220 3223 +3 |
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 does this PR do?
Fixes a race in
lightning.fabric.utilities.data._replace_dunder_methodswhen the cleanup path runs concurrently under free-threaded Python.In the
finallyblock, two overlapping callers can observe__old__*incls.__dict__, but one thread may restore and delete the attribute before the other finishes its owngetattr/setattr/delattrsequence. Under Python 3.14t withPYTHON_GIL=0, that can raiseAttributeErrorand interrupt the training loop.This change makes the cleanup tolerant to that race by skipping
AttributeErrorif another thread has already restored the original dunder method. It also adds a regression test that simulates a concurrent restore during__old__delattr__cleanup.Why is this needed?
This affects free-threaded Python runs where Lightning patches overlapping class hierarchies from multiple threads, for example threaded hyperparameter searches.
Does this PR introduce any breaking changes?
No.
How was this tested?
python -m pytest tests/tests_fabric/utilities/test_data.py -k replace_dunder_methods -vpython -m pytest tests/tests_pytorch/utilities/test_data.py::test_custom_torch_batch_sampler -v📚 Documentation preview 📚: https://pytorch-lightning--21618.org.readthedocs.build/en/21618/