Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class pywintypes:
else:
import select
_has_poll = hasattr(select, 'poll')
try:
import threading
except ImportError:
threading = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe use dummy_threading?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

dummy_threading doesn't give us anything useful. this is fine.

import fcntl
import pickle

Expand Down Expand Up @@ -878,6 +882,21 @@ def _close_fds(self, but):
pass


# Used as a bandaid workaround for https://bugs.python.org/issue27448
# to prevent multiple simultaneous subprocess launches from interfering
# with one another and leaving gc disabled.
if threading:
_disabling_gc_lock = threading.Lock()
else:
class _noop_context_manager(object):
# A dummy context manager that does nothing for the rare
# user of a --without-threads build.
def __enter__(self): pass
def __exit__(self, *args): pass

_disabling_gc_lock = _noop_context_manager()


def _execute_child(self, args, executable, preexec_fn, close_fds,
cwd, env, universal_newlines,
startupinfo, creationflags, shell, to_close,
Expand Down Expand Up @@ -909,10 +928,12 @@ def _close_in_parent(fd):
errpipe_read, errpipe_write = self.pipe_cloexec()
try:
try:
gc_was_enabled = gc.isenabled()
# Disable gc to avoid bug where gc -> file_dealloc ->
# write to stderr -> hang. http://bugs.python.org/issue1336
gc.disable()
with self._disabling_gc_lock:
gc_was_enabled = gc.isenabled()
# Disable gc to avoid bug where gc -> file_dealloc ->
# write to stderr -> hang.
# https://bugs.python.org/issue1336
gc.disable()
try:
self.pid = os.fork()
except:
Expand Down Expand Up @@ -986,9 +1007,10 @@ def _dup2(a, b):
exc_value.child_traceback = ''.join(exc_lines)
os.write(errpipe_write, pickle.dumps(exc_value))

# This exitcode won't be reported to applications, so it
# really doesn't matter what we return.
os._exit(255)
finally:
# This exitcode won't be reported to applications, so it
# really doesn't matter what we return.
os._exit(255)

# Parent
if gc_was_enabled:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Work around a `gc.disable()` race condition in the `subprocess` module that
could leave garbage collection disabled when multiple threads are spawning
subprocesses at once. Users are *strongly encouraged* to use the
`subprocess32` module from PyPI on Python 2.7 instead, it is much more
reliable.