December 7th, 2016

Why don’t I get a broken pipe when the child process terminates?

A customer was having a problem with named pipes.

Our main process creates a named pipe with ACCESS_INBOUND and passes the write handle to the child process. The main process keeps reading from the pipe until it gets ERROR_PIPE_BROKEN. We expect that when the child process terminates, the main process will get the ERROR_PIPE_BROKEN error. However, we are finding that sometimes the main proecss doesn’t get the ERROR_PIPE_BROKEN error, even though the child process has definitely terminated. Are there cases where the process with the write end of the pipe terminates, but the read doesn’t error out with ERROR_PIPE_BROKEN?

You won’t get ERROR_PIPE_BROKEN until all the write handles are closed. One common reason why you don’t get the error is that there’s still a write handle open in the parent process. Another possibility is that the child process launched a subprocess which inherited the write handle, or more generally, the handle got duplicated into another process by some means.

The customer wrote back.

Thanks. That is indeed the issue. The main process spawns many child processes simultaneously, so depending on race conditions, the write handle for one pipe could inadvertently be inherited by an unrelated child process. We could explicitly serialize our Create­Process calls, but is there another way to specify that a child process should inherit only certain handles and not others?

Yes. You can use the PROC_THREAD_ATTRIBUTE_LIST structure to exercise finer control over which handles are inherited.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.