close-on-exec handles are left open by exec parent

Christopher Faylor
Wed Jul 25 18:32:00 GMT 2001

On Tue, Jul 24, 2001 at 03:46:32PM +0400, egor duda wrote:
>test.exe create pipe and forks. when write end of the pipe is marked
>as close-on-exec, we DuplicateHandle () appropriately. but forked
>instance left running after execlp () is performed, and it doesn't
>close it's own copy of pipe handle. so read () blocks forever.
>i can see 2 ways to fix it. We can either always perform
>dtable::fixup_before_exec() and close handles marked as close-on-exec
>there, or always close all handles when exec child is started and exec
>parent cycles waiting for its child to exit.

Nice analysis.  I think the second is the way to deal with this.  The first
assumes that the exec will be successful, which is not necessarily true.
fixup_before_exec is a compromise for sockets.  I'd rather not have to
do this for other fds, too.

So, the correct fix is to close the handles after a successful CreateProcess.

I can't believe that this bug has been around for so long.  Of course, there
was a time (in B20.1, I think <gasp>) where close-on-exec didn't work at
all for non-cygwin processes...


More information about the Cygwin-developers mailing list