{"id":94875,"date":"2016-12-07T07:00:00","date_gmt":"2016-12-07T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=94875"},"modified":"2019-03-13T10:34:37","modified_gmt":"2019-03-13T17:34:37","slug":"20161207-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20161207-00\/?p=94875","title":{"rendered":"Why don&#8217;t I get a broken pipe when the child process terminates?"},"content":{"rendered":"<p>A customer was having a problem with named pipes. <\/p>\n<blockquote CLASS=\"q\"><p>Our main process creates a named pipe with <code>ACCESS_INBOUND<\/code> and passes the write handle to the child process. The main process keeps reading from the pipe until it gets <code>ERROR_PIPE_BROKEN<\/code>. We expect that when the child process terminates, the main process will get the <code>ERROR_PIPE_BROKEN<\/code> error. However, we are finding that sometimes the main proecss doesn&#8217;t get the <code>ERROR_PIPE_BROKEN<\/code> 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&#8217;t error out with <code>ERROR_PIPE_BROKEN<\/code>? <\/p><\/blockquote>\n<p>You won&#8217;t get <code>ERROR_PIPE_BROKEN<\/code> until all the write handles are closed. One common reason why you don&#8217;t get the error is that <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">there&#8217;s still a write handle open in the parent process<\/a>. 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. <\/p>\n<p>The customer wrote back. <\/p>\n<blockquote CLASS=\"q\"><p>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 <code>Create&shy;Process<\/code> calls, but is there another way to specify that a child process should inherit only certain handles and not others? <\/p><\/blockquote>\n<p>Yes. You can use the <code>PROC_THREAD_ATTRIBUTE_LIST<\/code> structure to <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20111216-00\/?p=8873\">exercise finer control over which handles are inherited<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The pipe isn&#8217;t broken yet.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-94875","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The pipe isn&#8217;t broken yet.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=94875"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94875\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=94875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=94875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=94875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}