Should PyImport_ImportModule be threadsafe when importing from zipfiles?

Chris Angelico rosuav at gmail.com
Thu Feb 20 10:41:59 EST 2020


On Fri, Feb 21, 2020 at 2:37 AM Geoff Bache <geoff.bache at gmail.com> wrote:
> When several threads execute this simultaneously I often get a stacktrace
> saying some function near the end of module b is not defined, presumably
> because the module has been imported part-initialised.
> This only seems to happen when my Python modules are packaged in a zip
> file, not when they are ordinary files on disk.
>
> I have observed this in both Python 3.7 and Python 3.8. Does anyone have
> any insights or suggestions for how to debug this? It seems likely to be
> hard to produce a reproducible test case.

One easy way to probe the bug would be to pre-import the module before
starting any secondary threads. If you ever get the problem under that
pattern, then it's not a concurrency problem (IOW have fun figuring
out what *is* the problem).

Another thing to try: Slap a print call at the top and bottom of the
module. See if you get multiple of them.

ChrisA


More information about the Python-list mailing list