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

Subprocess error with I/O redirection to Pipes #48442

Closed
pakal mannequin opened this issue Oct 24, 2008 · 4 comments
Closed

Subprocess error with I/O redirection to Pipes #48442

pakal mannequin opened this issue Oct 24, 2008 · 4 comments
Labels
OS-windows stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@pakal
Copy link
Mannequin

pakal mannequin commented Oct 24, 2008

BPO 4192
Nosy @pakal

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2009-09-30.06:50:11.200>
created_at = <Date 2008-10-24.15:23:18.653>
labels = ['library', 'OS-windows', 'type-crash']
title = 'Subprocess error with I/O redirection to Pipes'
updated_at = <Date 2009-09-30.06:50:11.199>
user = 'https://github.com/pakal'

bugs.python.org fields:

activity = <Date 2009-09-30.06:50:11.199>
actor = 'pakal'
assignee = 'none'
closed = True
closed_date = <Date 2009-09-30.06:50:11.200>
closer = 'pakal'
components = ['Library (Lib)', 'Windows']
creation = <Date 2008-10-24.15:23:18.653>
creator = 'pakal'
dependencies = []
files = []
hgrepos = []
issue_num = 4192
keywords = []
message_count = 4.0
messages = ['75169', '83130', '83131', '83217']
nosy_count = 2.0
nosy_names = ['ocean-city', 'pakal']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'closed'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue4192'
versions = ['Python 2.6']

@pakal
Copy link
Mannequin Author

pakal mannequin commented Oct 24, 2008

I've created on my desktop a file flooder.py containing just the following:
##################
import sys, os

progress=open(r"C:\Users\v-pascha\Desktop\STDERR.txt","w")

for i in range(101):
   print str(i)*20
   progress.write( str(i)+"\n" )
   
progress.close()
##################

and a file receiver.py containing just :

################
import sys, os, subprocess

#os.system("pause")
subprocess.Popen(r"python
C:\Users\v-pascha\Desktop\flooder.py",stdin=subprocess.PIPE,stdout=subprocess.PIPE)
#,stdin=subprocess.PIPE,stdout=subprocess.PIPE
#os.system("pause")

#####################

And when I launch receiver.py, I get a crash with that (not explicit)
message :
"close failed in file object destructor:
Error in sys.excepthook:

Original exception was:"

The crash doesn't happen if I don't redirect the stdout and stdin of the
child process. So it seems something weird happens when subprocess tries
to redirect the child's I/O to the PIPEs...

@pakal pakal mannequin added stdlib Python modules in the Lib dir OS-windows type-crash A hard crash of the interpreter, possibly with a core dump labels Oct 24, 2008
@ocean-city
Copy link
Mannequin

ocean-city mannequin commented Mar 4, 2009

Probably this happens because receiever.py doesn't wait for flooder.py
termination, and pipe end is closed when recieiver.py terminates.

Does this code work for you?

p = subprocess.Popen("python
flooder.py",stdin=subprocess.PIPE,stdout=subprocess.PIPE)
p.wait()

@ocean-city
Copy link
Mannequin

ocean-city mannequin commented Mar 4, 2009

This happens because when flooder.py terminates, its stdout will be
closed, but another pipe end in receirver.py process is already closed, so

Python\sysmodule.c(1098): _check_and_flush (FILE *stream)

In this function, fflush() fails. The reason why error message is not
helpful is probably this close function is called in interpreter
termination process, other modules/objects to do so are already destroyed.

Maybe this error message can be improved in some way, but I'm not sure.

@pakal
Copy link
Mannequin Author

pakal mannequin commented Mar 5, 2009

Thansk a lot for reviewing the problem

Indeed, "p.wait()" seems to do the trick in this case.

Is there any global way to avoid such pipe problems ? I mean, in any
case, one end of each pipe has to be closed before the other end, so
such errors might occur with the child process' stdin (in case the child
dies first and the parent flushes then its pipe toward the child's
stdin) or with its stdout (if the parent dies first and the child
flushes then its stdout).

Or are we sure that there won't be errors as long as children die before
the parent process ?

@pakal pakal mannequin closed this as completed Sep 30, 2009
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

0 participants