Fix SharedMemoryArray.__del__ AttributeError during interpreter shutdown - #1386
Draft
rootkiller6788 wants to merge 1 commit into
Draft
Fix SharedMemoryArray.__del__ AttributeError during interpreter shutdown#1386rootkiller6788 wants to merge 1 commit into
rootkiller6788 wants to merge 1 commit into
Conversation
During interpreter shutdown, module-level names in shared_memory_array.py (such as mmap and shared_memory) may be set to None. The __del__ method referenced these globals directly, so a lingering SharedMemoryArray raised AttributeError: 'NoneType' object has no attribute 'mmap' at shutdown, cluttering logs (issue google#398). Bind the mmap.mmap and shared_memory.SharedMemory types and the _del_shm helper as default arguments (bound at import time), and access the class attributes via type(self), so __del__ no longer depends on module globals. Adds a regression test that clears the relevant module globals and verifies deletion still unlinks the shared memory block.
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.
Problem
At interpreter shutdown,
SharedMemoryArray.__del__can raise:This is reported in #398 and clutters shutdown logs in data pipelines that use shared memory (e.g.
BatchOperation._enable_shared_memory()).Root cause
__del__referenced module-level names directly:During interpreter shutdown CPython clears module dicts, setting these globals to
None. ASharedMemoryArrayinstance that outlives the module then hitsNone.mmapand raises.Fix
Bind the
mmap.mmapandshared_memory.SharedMemorytypes and the_del_shmhelper as default arguments (bound at import time, so they survive module teardown), and reach the class attributes viatype(self)instead of theSharedMemoryArrayglobal.Test
Added
test_del_after_module_globals_cleared, which clearsmmap,shared_memory,SharedMemoryArrayand_del_shmon the module and verifies deletion still unlinks the shared memory block without raising.📚 Documentation preview 📚: https://google-grain--1386.org.readthedocs.build/