fork/exec & close file descriptors

Marko Rauhamaa marko at pacujo.net
Wed Jun 3 08:25:52 EDT 2015


Steven D'Aprano <steve at pearwood.info>:

> How does the child process know what action didn't complete? What
> error message are you going to display to the user?
>
> "Error when closing file descriptor 123456"
>
> What action do you think the user can take on seeing this error
> message?

Besides, even if you wanted to display that error message, you might not
be able to since maybe you have unknowingly closed fd 123433, which is
the hidden connection to the display server. So the program might just
crash in the attempt. Or the hidden fd is still open but won't tolerate
two processes sharing the connection.

You definitely must not use sys.stderr from the child process nor are
you allowed to exit through an exception but must call os._exit()
instead.

The child process is highly limited in what it can do between os.fork()
and os.execve(). To put it simply, it must not call any library
functions except system calls.

You might be able to reserve a special pipe to convey such errors to the
parent. Only don't rely on getting an EOF as closing the pipe might
fail...


Marko



More information about the Python-list mailing list