Issue2096

classification
Title: subprocess.call with no redirection breaks stdout/stderr mingling
Type: behaviour Severity: normal
Components: Core Versions: Jython 2.7
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: cdleonard, pjenvey, zyasoft
Priority: high Keywords:

Created on 2013-10-03.14:36:59 by cdleonard, last changed 2014-07-09.23:59:24 by zyasoft.

Messages
msg8142 (view) Author: Leonard Crestez (cdleonard) Date: 2013-10-03.14:36:58
Sample printer.sh:

#! /bin/bash
for i in `seq 10`; do
    echo -n O >&1
    echo -n E >&2
done

Sample subproc_call.py:

#! /usr/bin/python
import subprocess
subprocess.call('./printer.sh')

When running subproc_call.py with cpython the output is always OEOEOEOEOEOEOEOEOEOE, as expected. When running with jython the output is usually OOOOOOOOOOEEEEEEEEEE but other variants are also possible.

The documentation of subprocess.Popen states by default "no redirection will occur; the child’s file handles will be inherited from the parent". Jython behaves differently in a way that introduces visible behavior changes.

Jython Popen with stdout=subprocess.PIPE and stderr.subprocess.STDOUT apparently behaves as expected. This can also happen if entire lines are printed, or with manual flushes. The following cpython print script also reproduces the issue:

#! /usr/bin/env python
import sys
for i in range(50):
    sys.stdout.write('O\n')
    sys.stdout.flush()
    sys.stderr.write('E\n')
    sys.stderr.flush()
msg8143 (view) Author: Philip Jenvey (pjenvey) Date: 2013-10-03.17:00:11
This is a known issue: the original problem was subprocess was built for Jython 2.5 which ran on a minimum Java 5. There's no easy way to have a child process to inherit file handles from the parent w/ pure Java on Java 5 or 6

Java 7 finally added support for this
msg8144 (view) Author: Philip Jenvey (pjenvey) Date: 2013-10-03.17:00:58
by 'no easy way' I meant 'no way' (w/ pure Java) =]
msg8480 (view) Author: Jim Baker (zyasoft) Date: 2014-05-21.21:32:49
Should be an easy fix now that we require Java 7 by using inheritIO:

http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#inheritIO()

Target beta 4
msg8842 (view) Author: Jim Baker (zyasoft) Date: 2014-06-28.06:27:08
I'm not certain how to test in a cross platform fashion, but this is a straightforward fix.

http://hg.python.org/jython/rev/e9156cf6283f
History
Date User Action Args
2014-07-09 23:59:24zyasoftsetstatus: pending -> closed
2014-06-28 06:27:08zyasoftsetstatus: open -> pending
resolution: accepted -> fixed
messages: + msg8842
2014-06-19 04:42:07zyasoftsetpriority: high
2014-05-21 21:32:49zyasoftsetresolution: accepted
messages: + msg8480
nosy: + zyasoft
2013-10-03 17:00:58pjenveysetmessages: + msg8144
2013-10-03 17:00:12pjenveysetnosy: + pjenvey
messages: + msg8143
2013-10-03 14:36:59cdleonardcreate