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

bpo-42178: Fix issue causing cmd to hang #23290

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix whitespace issues
  • Loading branch information
John-Ted committed Nov 14, 2020
commit fa00b0c0a05aca115caaa016d0527261854ecaf6
11 changes: 6 additions & 5 deletions Lib/multiprocessing/popen_spawn_win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
def _path_eq(p1, p2):
return p1 == p2 or os.path.normcase(p1) == os.path.normcase(p2)


WINENV = not _path_eq(sys.executable, sys._base_executable)


Expand Down Expand Up @@ -50,11 +51,11 @@ def __init__(self, process_obj):
set_spawning_popen(self)
try:
with io.BytesIO() as f:
reduction.dump(prep_data, f)
try:
reduction.pickle.dump(process_obj, f)
except:
reduction.dump(process_obj, f)
reduction.dump(prep_data, f)
try:
reduction.pickle.dump(process_obj, f)
except:
reduction.dump(process_obj, f)
finally:
set_spawning_popen(None)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pickling Semaphore or SemLock objects entails duplicating an OS handle to the child process via duplicate_for_child. So we need the child process in order to do the reduction only once. What we can do to improve the situation is to spawn the child process with a suspended thread. This will require adding the CREATE_SUSPENDED flag and ResumeThread function to the _winapi module. After creating the child process with a suspended thread, set up the self attributes (e.g. self.sentinel) and dump the reduction to a BytesIO instance. If the latter fails, kill the child process via _winapi.TerminateProcess. Otherwise resume the thread in the child, and write the reduction from the BytesIO instance to the pipe.

Expand Down