gh-91351: Support re-entrancy in importlib/_bootstrap.py by exarkun · Pull Request #94342 · python/cpython (original) (raw)

This is a PR against the 3.9 branch (I will forward-port changes after addressing other review feedback).

Note that most of the size of the diff for this PR is generated changes to re-freeze _bootstrap.py and most of what's left after subtracting that is new comments about how the implementation works to make it easier to understand.

See #91351 for details about the problem.

Re-entrancy is always tricky and given the requirements of _bootstrap.py (to operate with re-entrancy and multi-threading and to do so without exposing any of the details to application code doing an import) I think this goes double.

This PR does a few things to achieve better safety in the face of re-entrancy:

I'm not quite sure I believe this new version of the code is 100% correct with respect to re-entrancy but it does fixes mishandling of two specific cases:

This PR also does not include any new unit tests. I have a small stand-alone program which can reproduce both of these but only with the assistance of some additional instrumentation inside _bootstrap.py to make sure the re-entrancy happens at the interesting times. If adding this kind of instrumentation is acceptable then it may be possible to turn this program into some unit tests.

It may also be possible to simplify _BlockingOnManager by switching _ModuleLock.lock to an RLock. That solution didn't originally occur to me so I developed this - but if others think that is a better approach I think it's a fairly simple change.

For reference, here is a stand-alone reproducer. This one isn't quite deterministic but by running the codepath over and over it seems to be fairly reliable in reproducing one of the problem codepaths on my system. For a completely deterministic reproducer, I think _bootstrap.py instrumentation is required.

import sys, socket, gc

class Cycle:
    pass

def a_cycle():
    c = Cycle()
    c.cycle = c
    c.s = socket.socket()

def main():
    while True:
        # import a module that socket.__del__ is going to import to exercise
        # re-entrant _ModuleLock.lock handling
        a_cycle()
        import linecache

        del sys.modules["linecache"]

main()