Skip to content
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

rhtooks: multiprocessing: use class-wide lock to prevent race conditions #7411

Merged
merged 1 commit into from Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions PyInstaller/hooks/rthooks/pyi_rth_multiprocessing.py
Expand Up @@ -9,6 +9,7 @@
# SPDX-License-Identifier: Apache-2.0
#-----------------------------------------------------------------------------

import threading
import multiprocessing
import multiprocessing.spawn as spawn
# 'spawn' multiprocessing needs some adjustments on osx
Expand Down Expand Up @@ -64,14 +65,17 @@ def _freeze_support():

# Mix-in to re-set _MEIPASS2 from sys._MEIPASS.
class FrozenSupportMixIn:
_lock = threading.Lock()

def __init__(self, *args, **kw):
if hasattr(sys, 'frozen'):
# The whole code block needs be executed under a lock to prevent race conditions between `os.putenv` and
# `os.unsetenv` calls when processes are spawned concurrently from multiple threads. See #7410.
with self._lock:
# We have to set original _MEIPASS2 value from sys._MEIPASS to get --onefile mode working.
os.putenv('_MEIPASS2', sys._MEIPASS) # @UndefinedVariable
try:
super().__init__(*args, **kw)
finally:
if hasattr(sys, 'frozen'):
try:
super().__init__(*args, **kw)
finally:
# On some platforms (e.g. AIX) 'os.unsetenv()' is not available. In those cases we cannot delete the
# variable but only set it to the empty string. The bootloader can handle this case.
if hasattr(os, 'unsetenv'):
Expand Down
3 changes: 3 additions & 0 deletions news/7410.bugfix.rst
@@ -0,0 +1,3 @@
(non-Windows) Fix race condition in environment modification done by
``multiprocessing`` runtime hook when multiple threads concurrently
spawn processes using the ``spawn`` method.