-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
Clean modules in the reversed order #77512
Comments
Since dict is ordered, modules in sys.modules are ordered for the time of importing. Currently they are cleaned in PyImport_Cleanup() in the direct order -- from imported first to imported later. I wonder if cleaning them in the reversed order can solve some problems with the interpreter shutdown. For example reverting the order fixes bpo-33328 and may help in other cases. If revert the order, should only iterating weaklist be reverted (with setting all module globals to None), or iterating sys.modules (with setting sys.module values to None) too? |
I think the key concern here is that we *don't* consistently add modules to sys.modules in the order their bodies are executed: we add them in a kind of sawtooth order based on the imports from __main__, and the order of import statements in the imported modules. For example, given the following import chains:
Then the order in which modules get added to sys.modules will be:
and they'll get cleaned up from left to right (We're making the assumption here that, for whatever reason, GC hasn't cleaned up A, X, and their dependencies after sys.modules got cleared) This means that in the status quo, unloading X, Y, and Z can have problems, since B, C, and D will already be gone. Reversing the order doesn't fix that, and if anything will make things worse, as it means that in the "A -> B -> C -> D" dependency chain, A now gets cleared *last*, instead of getting cleared first as it does today. So instead of just reversing the order, I wondering if what we may want to consider doing is to:
|
It also occurred to me to ask whether or not moving modules to the end of sys.modules each time they're successfully imported would help solve the problem (albeit at the expense of making import cache hits more expensive). I don't think it does, though, since in my example above, the least-recently-imported ordering would end up looking like:
Since D was only imported by C, and hence never gets moved to the end later, even when C gets moved by the import from Z. Instead, if we truly wanted to solve the problem comprehensively, we'd need to:
Taking better advantage of the existing cyclic GC seems like it should be simpler, though, and would hopefully be enough to handle all but the most complex edge cases. |
If move a module to the end of sys.modules when it have finished execution, we will get the following order:
In can be cleaned from the end, and each module will be cleaned before its dependencies. |
Aye, that option sounds like it would work to me (as long as throwing an |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: