{"id":142,"date":"2010-05-14T11:24:59","date_gmt":"2010-05-14T11:24:59","guid":{"rendered":"http:\/\/www.themissingdocs.net\/wordpress\/?p=142"},"modified":"2010-05-14T11:24:59","modified_gmt":"2010-05-14T11:24:59","slug":"odd-bug-in-64bit-net-runtime","status":"publish","type":"post","link":"https:\/\/www.themissingdocs.net\/?p=142","title":{"rendered":"Odd bug in 64bit .Net runtime"},"content":{"rendered":"<p>Was investigating a bug today and found some very peculiar behaviour in the 64bit .Net runtime.<\/p>\n<p>Lets say we have a thread running the following loop.<\/p>\n<pre>while (true)\n{\n try\n {\n  Thread.Sleep(10000);\n }\n catch\n {\n }\n try\n {\n }\n catch\n {\n }\n}\n<\/pre>\n<p>Okay so that code is not something you would write, I&#8217;ve reduced it down to the bare essentials to demonstrate the problem.<\/p>\n<p>If you throw a thread abort at this thread (for whatever reason&#8230;) most likely that is going to land in the sleep statement.\u00a0 Thread will be woken up and the catch block invoked, then at the end of the catch thread abort rethrown and the thread will exit &#8230; right?\u00a0 Not on 64bit runtime.\u00a0 On 64bit runtime with some strategic debugging you can see that the thread abort does not get rethrown (something to do with the second try catch block&#8230; since if you take it away it works), not until the while loop has gone round and you hit the sleep statement.\u00a0 The net result is a tight loop continually throwing ThreadAbortException.\u00a0 If you put some code in the try section of the second try catch, that will often cause ThreadAbort to get rethrown and the loop terminates.\u00a0 Unless the code is in a finally block or is somehow otherwise not suitable for thread abort raising, in which case it won&#8217;t raise ThreadAbort and the loop continues.\u00a0 The second try catch block can be a try finally instead and it still loops forever.<\/p>\n<p>Final trick, if you step through in the debugger rather than letting the code run, it behaves correctly!<\/p>\n<p>All in all a strange bug, it affects both the .Net 2 and .Net 4 runtimes, although i&#8217;ve only checked the most recent releases of both on windows 7\/2k8r2 environments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Was investigating a bug today and found some very peculiar behaviour in the 64bit .Net runtime. Lets say we have a thread running the following loop. while (true) { try { Thread.Sleep(10000); } catch { } try { } catch { } } Okay so that code is not something you would write, I&#8217;ve reduced &hellip; <a href=\"https:\/\/www.themissingdocs.net\/?p=142\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Odd bug in 64bit .Net runtime<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-net-stuff"],"_links":{"self":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=142"}],"version-history":[{"count":0,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}