This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: fdopen does not work as expected
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.4, Python 2.3, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jafo, luis@luispedro.org
Priority: normal Keywords:

Created on 2007-09-11 18:03 by luis@luispedro.org, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg55829 - (view) Author: Luis Pedro Coelho (luis@luispedro.org) Date: 2007-09-11 18:03
from os import *

def Fork():
    b = fork()
    if b < 0:
        raise Exception('fork() failed')
    return b


r,w=pipe()
b = Fork()
if b == 0:
   dup2(w,1)
   close(w)
   execlp('echo',\
       'echo',\
        'Hello world')
else:
   for line in fdopen(r):
       print 'Read %s' % line

I was expecting this code to print "Read Hello World". Instead, it 
hangs forever.

Changing "for line in fdopen(r): print 'Read %s' % line" 
to "line=read(r,100); print 'Read %s' % line" makes the program work 
as expected. This is what I did on my actual production code, but it 
seems funny behaviour on the part of fdopen.

I am running on Ubuntu on PowerPC.
msg55987 - (view) Author: Sean Reifschneider (jafo) * (Python committer) Date: 2007-09-18 04:14
I do not believe this is a Python problem but instead a problem with
your code.  I believe the problem is likely that you still have a copy
of the stdout is open, the fork that is reading also gets associated
with that same file.  I can't point you at any specific references, but
you should check Posix references or possibly other sources for more
information about side-effects such as this.

These interactions, I have found, can be pretty subtle, so you may need
to spend some time on getting everything right.
History
Date User Action Args
2022-04-11 14:56:26adminsetgithub: 45490
2007-09-18 04:14:58jafosetstatus: open -> closed
resolution: not a bug
messages: + msg55987
nosy: + jafo
2007-09-11 18:03:46luis@luispedro.orgcreate