-
Notifications
You must be signed in to change notification settings - Fork 1.4k
dealloc and finalize_modules #6934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
📝 WalkthroughWalkthroughAdds a staged module-finalization flow via a new Changes
Sequence DiagramsequenceDiagram
participant Interp as Interpreter
participant VM as VirtualMachine
participant Sys as sys.modules
participant Builtins as builtins
participant Mods as ModuleDicts
Interp->>VM: finalize_modules()
activate VM
VM->>Sys: Phase 1 — clear special sys attrs / restore stdio
VM->>VM: Phase 2 — nullify sys.modules entries, collect weak refs
VM->>Sys: Phase 3 — clear sys.modules dict
VM->>Mods: Phase 4 — clear module dicts in reverse import order
Mods->>Mods: two-pass clear (safeguard __main__)
VM->>Builtins: Phase 5 — clear sys and builtins dicts
VM-->>Interp: return
deactivate VM
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
f7fc6a8 to
7db6dba
Compare
|
Code has been automatically formatted The code in this PR has been formatted using:
git pull origin finalize-modules |
7202fa5 to
cef4196
Compare
Replace the absence of module finalization during interpreter shutdown with a 5-phase algorithm matching pylifecycle.c finalize_modules(): 1. Set special sys attributes to None, restore stdio 2. Set all sys.modules values to None, collect module dicts 3. Clear sys.modules dict 4. Clear module dicts in reverse import order (2-pass _PyModule_ClearDict) 5. Clear sys and builtins dicts last This ensures __del__ methods are called during shutdown and modules are cleaned up in reverse import order without hardcoded module names.
…sts as expected failure
Without GC, clearing all module dicts during finalization causes __del__
handlers to fail (globals are None). Restrict Phase 4 to only clear
__main__ dict — other modules' globals stay intact for their __del__
handlers.
Mark test_daemon_threads_shutdown_{stdout,stderr}_deadlock as expected
failures — without GC+GIL, finalize_modules clears __main__ globals
while daemon threads are still running.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
f9709f9 to
0db8e53
Compare
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [ ] test: cpython/Lib/test/test_builtin.py (TODO: 17) dependencies: dependent tests: (no tests depend on builtin) [ ] lib: cpython/Lib/io.py dependencies:
dependent tests: (87 tests)
[ ] test: cpython/Lib/test/test_sys.py (TODO: 16) dependencies: dependent tests: (208 tests)
Legend:
|
Replace the absence of module finalization during interpreter shutdown with a 5-phase algorithm matching pylifecycle.c finalize_modules():
This ensures del methods are called during shutdown and modules are cleaned up in reverse import order without hardcoded module names.
Summary by CodeRabbit
New Features
Bug Fixes
Chores
✏️ Tip: You can customize this high-level summary in your review settings.