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
Show file tree
Hide file tree
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
Next Next commit
bpo-42178: Fix issue causing cmd to hang
Try writing subprocess data before creating subprocess
  • Loading branch information
John-Ted committed Nov 14, 2020
commit c9a059cac00a8e1393fa6118df5947a70f327d0b
14 changes: 14 additions & 0 deletions Lib/multiprocessing/popen_spawn_win32.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import io
import msvcrt
import signal
import sys
Expand Down Expand Up @@ -44,6 +45,19 @@ class Popen(object):
def __init__(self, process_obj):
prep_data = spawn.get_preparation_data(process_obj._name)

# bpo-42178: Check for pickle error before creating subprocess
self.sentinel = None
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)
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.

# read end of pipe will be duplicated by the child process
# -- see spawn_main() in spawn.py.
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix issue causing cmd to hang in Windows