{"id":43983,"date":"2014-09-25T07:00:00","date_gmt":"2014-09-25T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/09\/25\/if-a-process-crashes-while-holding-a-mutex-why-is-its-ownership-magically-transferred-to-another-process\/"},"modified":"2014-09-25T07:00:00","modified_gmt":"2014-09-25T07:00:00","slug":"if-a-process-crashes-while-holding-a-mutex-why-is-its-ownership-magically-transferred-to-another-process","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140925-00\/?p=43983","title":{"rendered":"If a process crashes while holding a mutex, why is its ownership magically transferred to another process?"},"content":{"rendered":"<p>\nA customer was observing strange mutex ownership behavior.\nThey had two processes that used a mutex to coordinate access to\nsome shared resource.\nWhen the first process crashed while owning the mutex,\nthey found that the second process somehow magically gained ownership\nof that mutex.\nSpecifically, when the first process crashed,\nthe second process could take the mutex,\nbut when it released the mutex, the mutex was still not released.\nThey discovered that\nin order to release the mutex, the second process had to call\n<code>Release&shy;Mutex<\/code> <i>twice<\/i>.\nIt&#8217;s as if the claim on the mutex from the crashed process was\nsecretly transferred to the second process.\n<\/p>\n<p>\nMy psychic powers told me that that&#8217;s not what was happening.\nI guessed that their code went something like this:\n<\/p>\n<pre>\n<i>\/\/ code in italics is wrong\nbool TryToTakeTheMutex()\n{\n return WaitForSingleObject(TheMutex, TimeOut) == WAIT_OBJECT_0;\n}<\/i>\n<\/pre>\n<p>\nThe code failed to\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/09\/12\/463977.aspx\">\nunderstand the consequences of <code>WAIT_ABANDONED<\/code><\/a>.\n<\/p>\n<p>\nIn the case where the mutex was held by the first process when\nit crashed,\nthe second process will attempt to claim the mutex,\nand it will succeed,\nand the return code from <code>Wait&shy;For&shy;Single&shy;Object<\/code>\nwill be\n<code>WAIT_ABANDONED<\/code>.\nTheir code treated that value as\na failure code rather than a modified success code.\n<\/p>\n<p>\nThe second program therefore claimed the mutex <i>without realizing it<\/i>.\nThat is what led the customer to believe that ownership was being\nmagically transferred to the second program.\nIt wasn&#8217;t magic.\nThe second program misinterpreted the return code.\n<\/p>\n<p>\nThe second program saw that\n<code>Try&shy;To&shy;Take&shy;The&shy;Mutex<\/code>\n&#8220;failed&#8221;,\nand it went off and did something else for a while.\nThen the next time it called\n<code>Try&shy;To&shy;Take&shy;The&shy;Mutex<\/code>,\nthe function succeeded:\nIt was a successful recursive acquisition,\nbut the program thought it was the <i>initial<\/i> acquisition.\n<\/p>\n<p>\nThe customer didn&#8217;t reply back, so we never found out whether\nthat was the actual problem,\nbut I suspect it was.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer was observing strange mutex ownership behavior. They had two processes that used a mutex to coordinate access to some shared resource. When the first process crashed while owning the mutex, they found that the second process somehow magically gained ownership of that mutex. Specifically, when the first process crashed, the second process could [&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-43983","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer was observing strange mutex ownership behavior. They had two processes that used a mutex to coordinate access to some shared resource. When the first process crashed while owning the mutex, they found that the second process somehow magically gained ownership of that mutex. Specifically, when the first process crashed, the second process could [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43983","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=43983"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43983\/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=43983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=43983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=43983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}