{"id":15193,"date":"2010-01-22T07:00:00","date_gmt":"2010-01-22T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/01\/22\/during-process-termination-the-gates-are-now-electrified\/"},"modified":"2010-01-22T07:00:00","modified_gmt":"2010-01-22T07:00:00","slug":"during-process-termination-the-gates-are-now-electrified","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100122-00\/?p=15193","title":{"rendered":"During process termination, the gates are now electrified"},"content":{"rendered":"<p>It turns out that my <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/05\/03\/2383346.aspx\"> quick overview of how processes exit on Windows&nbsp;XP<\/a> was already out of date when I wrote it. Mind you, the information is still accurate for Windows&nbsp;XP (as far as I know), but the rules changed in Windows&nbsp;Vista.<\/p>\n<blockquote class=\"q\"><p> What about critical sections? There is no &#8220;Uh-oh&#8221; return value for critical sections; <code>EnterCriticalSection<\/code> doesn&#8217;t have a return value. <strike> Instead, the kernel just says &#8220;Open season on critical sections!&#8221; I get the mental image of all the gates in a parking garage just opening up and letting anybody in and out.<\/strike> <\/p><\/blockquote>\n<p> In Windows&nbsp;Vista, the gates don&#8217;t go up. Instead they become electrified!\n If during <code>DLL_PROCESS_DETACH<\/code> at process termination on Windows&nbsp;Vista you call <code>EnterCriticalSection<\/code> on a critical section that has been orphaned, the kernel no longer responds by just letting you through. Instead, it says, &#8220;Oh dear, things are in unrecoverably bad shape. Best to just terminate the process now.&#8221; If you try to enter an orphaned critical section during process shutdown, the kernel simply calls <code>TerminateProcess<\/code> on the current process!\n It&#8217;s sort of like the movie <a href=\"http:\/\/www.quotedb.com\/quotes\/175\"> <i>Speed<\/i><\/a>: If the thread encounters a critical section that causes it to drop below 50 miles per hour, it blows up.\n Fortunately, this error doesn&#8217;t change the underlying analysis of <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/05\/04\/2402028.aspx\"> How my lack of understanding of how processes exit on Windows XP forced a security patch to be recalled<\/a>.<\/p>\n<p> But it also illustrates how the details of process shutdown are open to changes in the implementation at any time, so you shouldn&#8217;t rely on them. Remember <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/05\/02\/2365433.aspx\"> the classical model for how processes exit<\/a>: You cleanly shut down all your worker threads, and then call <code>ExitProcess<\/code>. If you don&#8217;t follow that model (and given the current programming landscape, you pretty have no choice but to abandon that model, what with DLLs creating worker threads behind your back), it&#8217;s even more important that you follow the general guidance of <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/01\/27\/63401.aspx\"> not doing anything scary in your <code>DllMain<\/code> function<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>It turns out that my quick overview of how processes exit on Windows&nbsp;XP was already out of date when I wrote it. Mind you, the information is still accurate for Windows&nbsp;XP (as far as I know), but the rules changed in Windows&nbsp;Vista. What about critical sections? There is no &#8220;Uh-oh&#8221; return value for critical sections; [&hellip;]<\/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-15193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It turns out that my quick overview of how processes exit on Windows&nbsp;XP was already out of date when I wrote it. Mind you, the information is still accurate for Windows&nbsp;XP (as far as I know), but the rules changed in Windows&nbsp;Vista. What about critical sections? There is no &#8220;Uh-oh&#8221; return value for critical sections; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15193","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=15193"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15193\/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=15193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=15193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=15193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}