Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
Bug description:
Files
a/__init__.py
importosimporttimeprint(f"init{os.getpid()} at{time.clock_gettime_ns(time.CLOCK_MONOTONIC)}")
repro.py
importmultiprocessingimportosimporttimeif__name__=='__main__':print(f"run main{os.getpid()}")multiprocessing.set_forkserver_preload(['a'])for_inrange(2):p=multiprocessing.Process()p.start()p.join()else:print(f"re-import main{os.getpid()} at{time.clock_gettime_ns(time.CLOCK_MONOTONIC)}")
Reproduction
- Create a new module
acontaining the__init__.pyfile above - Run the
repro.pyscript above, ensuring the module created is onPYTHONPATH
Result
> ./python repro.pyrun main 1056488init 1056490 at 151009034069836re-import main 1056491 at 151009045273212re-import main 1056492 at 151009051787587> ./python repro.py | tee /dev/nullrun main 1056607init 1056610 at 151113770440639re-import main 1056611 at 151113781130002init 1056610 at 151113770440639re-import main 1056612 at 151113787814593init 1056610 at 151113770440639Expected
The output to be the same when stdout is redirected as when it is not.
Analysis
This is due to fork server preloading a module that writes tostdout, but not flushing it. When a child process is spawned it inherits the buffered data and spuriously outputs it when it flushesitsstdout. Note that#126631 prevents__main__ from being preloaded, so at present this will not be triggered by printing from__main__.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-135335: flush stdout/stderr in forkserver after preloading modules #135338
- [3.14] gh-135335: flush stdout/stderr in forkserver after preloading modules (GH-135338) #135670
- [3.13] gh-135335: flush stdout/stderr in forkserver after preloading modules (GH-135338) #135671
- gh-135335: Simplify preload regression test using __main__ #138686
- [3.14] gh-135335: Simplify preload regression test using __main__ (GH-138686) #141886
- [3.13] gh-135335: Simplify preload regression test using __main__ (GH-138686) #141887