[issue44916] Random behaviour when importing two modules with the same name but different source files

Steven D'Aprano report at bugs.python.org
Sat Aug 14 22:08:45 EDT 2021


Steven D'Aprano <steve+python at pearwood.info> added the comment:

"Undefined behaviour" has a particular meaning to C programmers, which is relevant to Python because the interpreter is written in C. It's probably best not to use that term here.

Let me see if I understand your issue.

* You have two separate Python processes.

* Each process has a thread which dynamically writes a file called "m1.py", containing a class C.

* Each process has a second thread which dynamically writes a file called "m2.py", also containing a class C.

* Each thread then imports its file using the common name "M", and tries to pickle and unpickle objects of type C.

* And seemingly at random, each thread sometimes picks up its class M.C, but sometimes the class M.C from the other thread.

* Not sure if you get any cross-process contamination as well (that is, process 1 picks up the modules from process 2), but it wouldn't surprise me in the least.


My instinct here is to back away in horror *wink*

You have a lot of non-deterministic code here. I'm kinda impressed that it ever works at all :-)

1. If you have two processes writing to the same file "m1.py", its a lottery which one will end up actually written to disk. It is at least theoretically possible that the data actually on the disk could be a hybrid of bits of process 1's m1.py and bits of process 2's m1.py.

2. Likewise for the file m2.py.

3. When you go to import the files, it is non-deterministic which file you will see, e.g.

- process 1 writes its m1.py
- process 2 writes its m1.py, overriding the previous m1.py
- process 1 goes to import m1.py, but ends up reading the m1.py
  created by process 2

So that's how you could get cross-process contamination.

----------
nosy: +steven.daprano
title: Undefined/random behaviour when importing two modules with the same name but different source files -> Random behaviour when importing two modules with the same name but different source files

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44916>
_______________________________________


More information about the Python-bugs-list mailing list