1

My python program creates a pipe, forks, and then spawns another program from the child. The parent then sits and waits for the reader-side of the pipe to become readable.

    reader, writer = os.pipe()
    fcntl.fcntl(reader, fcntl.F_SETFL, os.O_NONBLOCK)
    child = os.fork()
    if child == 0:
        os.close(reader)
        os.execvp('program', ['program', '-o', '/dev/fd/%d' % writer])

    while True:
        if os.waitpid(child, os.WNOHANG) != (0, 0):
            break
        logger.debug('Going into select')
        r, w, x = select.select([reader], [], [])
        .....

For some reason, when the spawn child exits, the parent continues to wait in the select... Indefinitely... How is one supposed to detect this situation?

3
  • 1
    Did you try to close the writer in the parent (select might be deadlocked by parent's writer)?
    – jerry
    May 13, 2020 at 1:53
  • Yes, that was it... I came back here to report it and saw your comment. If you make it a proper answer, I'll accept it. Thanks!
    – Mikhail T.
    May 13, 2020 at 1:59
  • OK, then I'll write it as an answer :)
    – jerry
    May 13, 2020 at 2:00

1 Answer 1

1

There is a deadlock since parent process' writer is not closed before going to select. You could also close the writer in the parent process:

if child == 0:
    os.close(reader)
    ...
else:
    os.close(writer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.