Skip to content

Commit 34ef6da

Browse files
authored
[3.6] bpo-28326: Fix multiprocessing.Process when stdout and/or stderr is closed or None. (GH-4073). (#4075)
* bpo-28326: Fix multiprocessing.Process when stdout and/or stderr is closed or None. (#4073) (cherry picked from commit daeefd2) * [3.6] bpo-28326: Fix multiprocessing.Process when stdout and/or stderr is closed or None. (GH-4073). (cherry picked from commit daeefd2)
1 parent 1e78ed6 commit 34ef6da

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Lib/multiprocessing/popen_fork.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ class Popen(object):
1414
method = 'fork'
1515

1616
def __init__(self, process_obj):
17-
sys.stdout.flush()
18-
sys.stderr.flush()
17+
try:
18+
sys.stdout.flush()
19+
except (AttributeError, ValueError):
20+
pass
21+
try:
22+
sys.stderr.flush()
23+
except (AttributeError, ValueError):
24+
pass
1925
self.returncode = None
2026
self._launch(process_obj)
2127

Lib/test/_test_multiprocessing.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,27 @@ def test_lose_target_ref(self):
425425
self.assertEqual(q.get(), 5)
426426
close_queue(q)
427427

428+
@classmethod
429+
def _test_error_on_stdio_flush(self, evt):
430+
evt.set()
431+
432+
def test_error_on_stdio_flush(self):
433+
streams = [io.StringIO(), None]
434+
streams[0].close()
435+
for stream_name in ('stdout', 'stderr'):
436+
for stream in streams:
437+
old_stream = getattr(sys, stream_name)
438+
setattr(sys, stream_name, stream)
439+
try:
440+
evt = self.Event()
441+
proc = self.Process(target=self._test_error_on_stdio_flush,
442+
args=(evt,))
443+
proc.start()
444+
proc.join()
445+
self.assertTrue(evt.is_set())
446+
finally:
447+
setattr(sys, stream_name, old_stream)
448+
428449

429450
#
430451
#
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix multiprocessing.Process when stdout and/or stderr is closed or None.

0 commit comments

Comments
 (0)