{"id":4436,"date":"2014-03-30T10:58:38","date_gmt":"2014-03-30T10:58:38","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-cs-using-statement-abort-safe-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:58:38","modified_gmt":"2014-03-30T10:58:38","slug":"is-cs-using-statement-abort-safe-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-cs-using-statement-abort-safe-collection-of-common-programming-errors\/","title":{"rendered":"Is C#&#39;s using statement abort-safe?-Collection of common programming errors"},"content":{"rendered":"<blockquote>\n<p>Are authors of the book right and the using statement is not abort-safe or are they wrong and it behaves like in my second solution?<\/p>\n<p>According to the book (p. 856), ThreadAbortException can be thrown anywhere in managed code. But maybe there are exceptions and the first variant is abort-safe after all?<\/p>\n<\/blockquote>\n<p>The authors are right. The <code>using<\/code> block is not abort-safe. Your second solution is also not abort-safe, the thread could be aborted in the middle of the resource acquisition.<\/p>\n<p>Although it&#8217;s not abort-safe, any disposable that has unmanged resources should also implement a finalizer, which will <em>eventually<\/em> run and clean up the resource. The finalizer should be robust enough to also take care of not completely initialized objects, in case the thread aborts in the middle of the resource acquisition.<\/p>\n<p>A <code>Thread.Abort<\/code> will only wait for code running inside Constrained Execution Regions (CERs), <code>finally<\/code> blocks, <code>catch<\/code> blocks, static constructors, and unmanaged code. So this is an abort-safe solution (<em>only<\/em> regarding the acquisition and disposal of the resource):<\/p>\n<pre><code>StreamReader reader = null;\ntry {\n  try { }\n  finally { reader = File.OpenText(\"file.txt\"); }\n  \/\/ ...\n}\nfinally {\n  if (reader != null) reader.Dispose();\n}\n<\/code><\/pre>\n<p>But be <strong>careful<\/strong>, abort-safe code should run <em>fast<\/em> and <em>not block<\/em>. It could hang a whole app domain unload operation.<\/p>\n<blockquote>\n<p>If using is equivalent to the first variant (not abort-safe), why does it check for null in finally?<\/p>\n<\/blockquote>\n<p>Checking for null makes the <code>using<\/code> pattern safe in the presence of <code>null<\/code> references.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are authors of the book right and the using statement is not abort-safe or are they wrong and it behaves like in my second solution? According to the book (p. 856), ThreadAbortException can be thrown anywhere in managed code. But maybe there are exceptions and the first variant is abort-safe after all? The authors are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4436","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4436"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4436\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}