<rss version="2.0"><channel><title>Daniel Fortunov's Adventures in Software Development</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/</link><description></description><pubDate>2010-06-08T08:10:20Z</pubDate><generator>http://www.webjam.com/</generator><language>en</language><item><title>Concurrent Programming on Windows</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/06/08/concurrent_programming_on_windows</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/06/08/concurrent_programming_on_windows#Comments</comments><pubDate>2010-06-08T08:10:20Z</pubDate><category>.net, "book review", win32</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/06/08/concurrent_programming_on_windows</guid><description><![CDATA[<p><em><img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="'Concurrent Programming on Windows' book cover" border="0" alt="'Concurrent Programming on Windows' book cover" align="right" src="http://webjam-upload.s3.amazonaws.com/48419206-a45d-4eb8-bb76-a128d8d035ae_WindowsLiveWriterConcurrentProgrammingFacemelt_6BB39780321434821%5B1%5D_3.jpg" width="185" height="244"> Concurrent Programming on Windows</em> (by Joe Duffy) is <a title="&quot;A truly good book teaches me better than to read it. I must soon lay it down, and commence to living on its hint.&quot; -Thoreau" href="http://www.johndcook.com/blog/2010/02/12/great-books/">a book so good I had to put it down</a>, frequently, to stop and think. The information density is pretty high and I often found myself staring blankly into space for minutes at a time, book open on my lap, thinking through what I’d just read.</p> <p>This is probably the definitive book on concurrency in Windows, covering general principles and the relevant APIs across both native (Win32) and managed (.NET). It has a good balance of theoretical discussion and practical advice, with no shortage of references at the end of each chapter for those who feel inclined for some additional background reading. (For instance, the “Further Reading” section at the end of Chapter 10 “Memory Models and Lock Freedom” points to some light reading: <em>AMD x86-64 Architecture Programmer’s Manual</em>, Volumes 1–5 (!))</p> <p>What makes this book truly valuable is the amount of information and knowledge that it aggregates, from obscure technical sources, academic papers, and even first-hand spelunking in the Windows source code to find answers to some undocumented behavioural details. It also provides plenty of practical advice garnered from years of experience.</p> <p>For me, having mainly managed programming experience, this book provided a nice opportunity to understand more about the underlying obscurities of Win32, and how these relate to and contrast with what is exposed in .NET. Having that underlying knowledge has let me see how <a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/01/31/invalid_wait_handle">passing the Invalid Wait Handle value to some asynchronous methods can make them execute synchronously instead</a>, and to understand that <a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/05/10/asynchronous_file_io_in_net">asynchronous I/O needs to be decided on when a file handle is opened</a> — details that had previously eluded me in my journeys through managed-land.</p> <p>Other things that were interesting to learn about included lock-free algorithms (with clever tricks like structuring a lock-free linked list such that it has a sentinel node when empty, cunningly avoiding the problem of updating two pointers when the list transitions between empty and non-empty), and the details of kernel-mode synchronisation primitives, with their limitless caveats (the abandoned mutex scenario was my favourite… when waiting on a named mutex it is possible that it would have been abandoned if another process exited before releasing it. Despite returning an error, the operation has succeeded in acquiring the mutex and you must still remember to release it! As if you didn’t have enough to think about by that point, with all the other complexities around alertable waits and pumping the message queue if you’re in an STA).</p> <p>I previously said that, at a length of 736 pages, <a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/02/03/book_review_clr_via_c_2nd_edition"><em>CLR via C# (2nd Edition)</em></a> was the largest book I have ever read. But with a length of 930 pages, <em>Concurrent Programming on Windows</em> has surpassed this. Next up on the reading list: <em>CLR via C# (<u>3rd Edition</u>).</em></p>]]></description></item><item><title>Asynchronous File I/O in .NET</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/05/10/asynchronous_file_io_in_net</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/05/10/asynchronous_file_io_in_net#Comments</comments><pubDate>2010-05-10T06:57:00Z</pubDate><category>.net, obscure, win32, concurrency</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/05/10/asynchronous_file_io_in_net</guid><description><![CDATA[<p><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/01/31/invalid_wait_handle">Another</a> useful snippet of knowledge gained from reading <em><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/06/08/concurrent_programming_on_windows">Concurrent Programming on Windows</a></em> (by Joe Duffy):</p> <p>Did you know that asynchronous file I/O in .NET is not just about calling <tt>FileStream.BeginRead()</tt> or <tt>BeginWrite()</tt> in place of <tt>Read()</tt> or <tt>Write()</tt>? You should also make sure that the <tt>FileStream</tt> is opened for asynchronous operations, otherwise you’ll quietly get less performant ‘mock’ async operations that just execute synchronous I/O on the thread pool, rather than using true overlapped I/O at the Win32 level.</p> <h3>Excuses, Excuses</h3> <p>The natural starting point for creating a <tt>FileStream</tt> is the static <tt><a href="http://msdn.microsoft.com/en-us/library/y973b725.aspx">File.Open()</a></tt> method, the documentation for which mentions nothing about synchronicity of the <tt>FileStream</tt> that is created! Nor does it allow you to provide <tt><a href="http://msdn.microsoft.com/en-us/library/system.io.fileoptions.aspx">FileOptions</a></tt> (which are used to specify the magic <tt>FileOptions.Asynchronous</tt> flag).</p> <p>Instead, the <tt>FileStream</tt> is created with <tt>FileOptions.None.</tt> Any asynchronous operations are quietly faked by the obliging implementation of the <tt>Stream</tt> base class, which merely wraps the corresponding <em>synchronous</em> method in a delegate and invokes it on the thread pool using the <tt>BeginInvoke()</tt> method.</p> <p>This is a deviation from the usual ‘<a href="http://blogs.msdn.com/brada/archive/2003/10/02/50420.aspx">pit of success</a>’ design philosophy, where everything in .NET seems to work as you think it would, without a need to closely read the documentation and/or gradually discover obscure catches and gotchas over time.</p> <h3>On Balance</h3> <p>Admittedly I’ve never actually used asynchronous file I/O (for the applications I’ve worked on have used databases, queues, and other remote data persistence rather than local files) or else I might have read the <tt><a href="http://msdn.microsoft.com/en-us/library/zxt5ahzw.aspx">FileStream.BeginRead()</a></tt> and <tt><a href="http://msdn.microsoft.com/en-us/library/t7e3td2c.aspx">BeginWrite()</a></tt> documentation a little more closely:</p> <blockquote> <p><a href="http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx"><tt>FileStream</tt></a><span></span> provides two different modes of operation: synchronous I/O and asynchronous I/O. While either can be used, the underlying operating system resources might allow access in only one of these modes. <strong>By default, <span><a href="http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx">FileStream<tt></tt></a></span> opens the operating system handle synchronously. In Windows, this slows down asynchronous methods. If asynchronous methods are used, use the <span><a href="http://msdn.microsoft.com/en-us/library/7db28s3c.aspx">FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)<tt></tt></a></span> constructor.</strong></p> <p>&nbsp;</p></blockquote> <p>That last Boolean parameter to the <tt>FileStream</tt> constructor is called <tt>useAsync</tt> and, if true, results in <tt>FileOptions.Asynchronous</tt> being used (or you can also use the other constructor overload which takes <tt>FileOptions</tt> in the last parameter, and specify <tt>FileOptions.Asynchronous</tt> yourself).</p> <p>The underlying <tt><a href="http://msdn.microsoft.com/en-us/library/system.io.stream.beginread.aspx">Stream.BeginRead()</a></tt> and <tt><a href="http://msdn.microsoft.com/en-us/library/system.io.stream.beginwrite.aspx">BeginWrite()</a></tt> methods also talk about synchronicity:</p> <blockquote> <p>The default implementation of <tt>BeginRead</tt> on a stream calls the <tt><a href="http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx">Read</a></tt> method synchronously, which means that <tt>Read</tt> might block on some streams. However, instances of classes such as <tt>FileStream</tt> and <tt>NetworkStream</tt> fully support asynchronous operations if the instances have been opened asynchronously. Therefore, calls to <tt>BeginRead</tt> will not block on those streams. You can override <tt>BeginRead</tt> (by using async delegates, for example) to provide asynchronous behavior.</p></blockquote> <p>I think this documentation is out of date, or at least a little unclear. The default implementation of <tt>BeginRead</tt> does <em>not</em> call <tt>Read</tt> synchronously — Reflector shows that it calls <tt>Read</tt> by wrapping it in a delegate and calling <tt>BeginInvoke</tt>, which would result in it being called on a thread pool thread. This is an <em>asynchronous</em> call (with respect to the caller of <tt>BeginRead</tt>).</p> <p>Perhaps the documentation is out of date, since it also suggests "using async delegates" to implement your own asynchronous behaviour — what advantage would that give you over the default implementation which does just the same?</p> <p>As ever, the truth lies in Reflector.</p> <h3>Conclusion</h3> <p>In summary, if you want to do asynchronous file I/O:</p> <ul> <li>Don’t use <tt>File.Open()</tt> to create your <tt>FileStream</tt> — it will be opened for synchronous I/O.  <li>Create the <tt>FileStream</tt> directly, specifying <tt>useAsync=true</tt> (or <tt>options=FileOptions.Asynchronous</tt>) — this will open the Win32 file handle for overlapped I/O.  <li>Use <tt>BeginRead()</tt> and <tt>BeginWrite()</tt> as normal — the framework will hide the details of overlapped operations behind the Asynchronous Programming Model. </li></ul> <p>Finally, if you’re using asynchronous I/O you must care about performance, so don’t forget to measure, measure, measure! And heed the warning hidden in the documentation of that <tt>useAsync</tt> parameter:</p> <blockquote> <dt>useAsync  <dd>Specifies whether to use asynchronous I/O or synchronous I/O. However, note that the underlying operating system might not support asynchronous I/O, so when specifying <tt>true</tt>, the handle might be opened synchronously depending on the platform. When opened asynchronously, the <a href="http://msdn.microsoft.com/en-us/library/zxt5ahzw.aspx"><tt>BeginRead</tt></a> and <a href="http://msdn.microsoft.com/en-us/library/t7e3td2c.aspx"><tt>BeginWrite</tt></a> methods perform better on large reads or writes, but they might be much slower for small reads or writes. If the application is designed to take advantage of asynchronous I/O, set the <tt>useAsync</tt> parameter to <tt>true</tt>. <strong>Using asynchronous I/O correctly can speed up applications by as much as a factor of 10, but using it without redesigning the application for asynchronous I/O can decrease performance by as much as a factor of 10.</strong></dd></blockquote>]]></description></item><item><title>ILMerge in MSBuild</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/02/09/ilmerge_in_msbuild</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/02/09/ilmerge_in_msbuild#Comments</comments><pubDate>2010-02-09T08:30:00Z</pubDate><category>.net, msbuild</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/02/09/ilmerge_in_msbuild</guid><description><![CDATA[<p><a href="http://research.microsoft.com/en-us/people/mbarnett/ILMerge.aspx">ILMerge</a> is a utility from Microsoft Research that combines multiple .NET assemblies into a single assembly. This is convenient when you want to combine your application and its dependencies into a single DLL file, for example, to make deployment and versioning easier.</p>
<p>ILMerge is released as a console application but also exposes an API to allow you to use it in other applications. For example, I see there are some GUI applications to ease the burden of typing in all those command line switches. ILMerge is mysteriously missing from the community collections of MSBuild tasks, such as the <a href="http://www.codeplex.com/sdctasks">SDC Tasks Library</a> and <a href="http://msbuildextasks.codeplex.com/">MSBuild Extended Tasks</a>, probably because it is perfectly feasible to invoke the ILMerge executable using the <a href="http://msdn.microsoft.com/en-us/library/x8zx72cd.aspx">Exec</a> task that is provided with MSBuild.</p>
<h3>The Goal</h3>
<p>The goal is to integrate ILMerge into MSBuild, such that it runs automagically every time the project is built (either within Visual Studio, or with MSBuild from the command line).</p>
<p>Unfortunately there are some interesting details to integrate smoothly into the build, such as making sure the task handles incremental builds properly (so that adding ILMerge to one project in a solution doesn&rsquo;t force a re-build of that entire sub-tree every time you build!)</p>
<p>I&rsquo;ve not been able to find an adequate pre-canned way to achieve this, but I&rsquo;ve hacked something together starting from <a href="http://blogs.msdn.com/jomo_fisher/articles/544145.aspx">Jomo Fisher&rsquo;s</a> solution and addressing some of the shortcomings I found along the way.</p>
<h3>The Solution</h3>
<p>Hand-edit your MSBuild project (e.g. *.csproj) file to tag the referenced assemblies you&rsquo;d like to merge with the ILMerge=True metadata, like this:</p>
<p><small>
<pre class="code" style="white-space: pre-wrap; word-wrap: break-word;"><span style="color: blue">&lt;</span><span style="color: #a31515">Reference </span><span style="color: red">Include</span><span style="color: blue">=</span>"<span style="color: blue">DependencyLibrary, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL</span>"<span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">SpecificVersion</span><span style="color: blue">&gt;</span>False<span style="color: blue">&lt;/</span><span style="color: #a31515">SpecificVersion</span><span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">HintPath</span><span style="color: blue">&gt;</span>Referenced Assemblies\DependencyLibrary.dll<span style="color: blue">&lt;/</span><span style="color: #a31515">HintPath</span><span style="color: blue">&gt;
<strong>  <span style="text-decoration: underline;">&lt;</span></strong></span><strong><span style="text-decoration: underline;"><span style="color: #a31515">ILMerge</span><span style="color: blue">&gt;</span>True<span style="color: blue">&lt;/</span><span style="color: #a31515">ILMerge</span></span></strong><span style="color: blue"><strong><span style="text-decoration: underline;">&gt;</span></strong>
  &lt;</span><span style="color: #a31515">Private</span><span style="color: blue">&gt;</span>False<span style="color: blue">&lt;/</span><span style="color: #a31515">Private</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Reference</span><span style="color: blue">&gt;</span></pre>
</small></p>
<p>(Note that it is <em>not</em> necessary to set CopyLocal=True for the target assemblies.)</p>
<p>Then, define the following targets and properties at the bottom of your MSBuild project (just above the <span style="color: #0000ff;">&lt;/</span><tt style="color: #a31515">Project</tt><span style="color: blue">&gt; </span>tag):</p>
<p><small>
<pre class="code" style="white-space: pre-wrap; word-wrap: break-word;"><span style="color: blue">&lt;</span><span style="color: #a31515">Target </span><span style="color: red">Name</span><span style="color: blue">=</span>"<span style="color: blue">AfterBuild</span>" <span style="color: red">DependsOnTargets</span><span style="color: blue">=</span>"<span style="color: blue">ILMerge</span>" <span style="color: blue">/&gt;
&lt;</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">ILMergeExecutable</span><span style="color: blue">&gt;</span>"..\BuildTools\ILMerge\ILMerge.exe"<span style="color: blue">&lt;/</span><span style="color: #a31515">ILMergeExecutable</span><span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">KeyFile</span><span style="color: blue">&gt;</span>"$(ProjectDir)MyApplication.snk"<span style="color: blue">&lt;/</span><span style="color: #a31515">KeyFile</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Target </span><span style="color: red">Name</span><span style="color: blue">=</span>"<span style="color: blue">ILMerge</span>" <span style="color: red">Inputs</span><span style="color: blue">=</span>"<span style="color: blue">@(IntermediateAssembly)</span>"
        <span style="color: red">Outputs</span><span style="color: blue">=</span>"<span style="color: blue">@(MainAssembly -&gt; '%(RelativeDir)%(Filename).ILMergeTrigger%(Extension)')</span>"<span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">CreateItem </span><span style="color: red">Include</span><span style="color: blue">=</span>"<span style="color: blue">@(ReferencePath)</span>" <span style="color: red">Condition</span><span style="color: blue">=</span>"<span style="color: blue">'%(ReferencePath.ILMerge)'=='True'</span>"<span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Output </span><span style="color: red">TaskParameter</span><span style="color: blue">=</span>"<span style="color: blue">Include</span>" <span style="color: red">ItemName</span><span style="color: blue">=</span>"<span style="color: blue">ILMergeAssemblies</span>" <span style="color: blue">/&gt;
  &lt;/</span><span style="color: #a31515">CreateItem</span><span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">Exec </span><span style="color: red">Command</span><span style="color: blue">=</span>"<span style="color: blue">$(ILMergeExecutable) /Closed /Internalize /Lib:$(OutputPath) /keyfile:$(KeyFile) /out:@(MainAssembly) </span><span style="color: red">&amp;quot;</span><span style="color: blue">@(IntermediateAssembly)</span><span style="color: red">&amp;quot; </span><span style="color: blue">@(ILMergeAssemblies-&gt;'</span><span style="color: red">&amp;quot;</span><span style="color: blue">%(FullPath)</span><span style="color: red">&amp;quot;</span><span style="color: blue">', ' ')</span>" <span style="color: blue">/&gt;
  &lt;!-- </span><span style="color: green">Make a copy of the merged output DLL to use as a trigger for incremental builds </span><span style="color: blue">--&gt;
  &lt;</span><span style="color: #a31515">Copy
    </span><span style="color: red">SourceFiles</span><span style="color: blue">=</span>"<span style="color: blue">@(MainAssembly)</span>"
    <span style="color: red">DestinationFiles</span><span style="color: blue">=</span>"<span style="color: blue">@(MainAssembly -&gt; '%(RelativeDir)%(Filename).ILMergeTrigger%(Extension)')</span>" <span style="color: blue">/&gt;
&lt;/</span><span style="color: #a31515">Target</span><span style="color: blue">&gt;</span></pre>
</small></p>
<p><strong>Here's the full wolking solution: </strong><a href="http://www.danielfortunov.com/software/~Media?id=a3cc7a43-b523-4a54-913f-416f07d81e04" style="text-decoration : none;"><img height="16" width="16" src="http://www.danielfortunov.com/images/filetype/zip.png" align="absMiddle" border="0" /> ILMergeExperiments</a></p>
<p>There are a couple of hacks here to deal with the fact that we want our ILMerged assembly to have the same name as the original:</p>
<ol>
<li>We are referencing the intermediate assembly from the &lsquo;obj&rsquo; directory as input (because you can&rsquo;t have the same file as both input and output to ILMerge) </li>
<li>A copy of the output file is saved to Foo.ILMergeTrigger.dll, and it is this file that is named as the output of the build target. This is in order to correctly participate in the dependency analysis that is used during incremental builds (because if your merged output assembly has the same name as the unmerged output assembly, then the standard build will overwrite your merged assembly and make it look &lsquo;up to date&rsquo;, and your ILMerge task will not be executed because its outputs are up to date!)</li>
</ol>
<p>This is somewhat hacky, and I&rsquo;m sure there must be a more cunning way to integrate into MSBuild; I&rsquo;ll have to revisit this once I&rsquo;ve read the book <em><a href="http://www.amazon.com/Inside-Microsoft-Build-Engine-PRO-Developer/dp/0735626286/ref=ntt_at_ep_dpi_1">Inside the Microsoft Build Engine: Using MSBuild and Team Foundation Build</a>.</em></p>]]></description></item><item><title>Invalid Wait Handle</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/01/31/invalid_wait_handle</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/01/31/invalid_wait_handle#Comments</comments><pubDate>2010-01-31T08:12:00Z</pubDate><category>.net, obscure, win32, analysys</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/01/31/invalid_wait_handle</guid><description><![CDATA[<p>One of the obscure gems garnered from my <a href="http://webjam-upload.s3.amazonaws.com/cpowprogress___f51a8aa0a3e04672b52a1231f34bdf80(642x444)__27__.png">current reading</a> of the book <em><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/06/08/concurrent_programming_on_windows">Concurrent Programming on Windows</a></em> (by Joe Duffy) is an insight into the <tt>INVALID_HANDLE_VALUE</tt> constant.</p>
<h3>Background</h3>
<p>In Win32 programming, functions that return a <tt>HANDLE</tt> (such as <tt><a href="http://msdn.microsoft.com/en-us/library/aa363858.aspx">CreateFile</a></tt>) may return <tt>INVALID_HANDLE_VALUE</tt> to indicate failure (<a href="http://blogs.msdn.com/oldnewthing/archive/2004/03/02/82639.aspx">sometimes</a>). You can check for this return value and call <tt><a href="http://msdn.microsoft.com/en-us/library/ms679360.aspx">GetLastError</a></tt> to find out why the operation failed.</p>
<p>In .NET functions typically indicate unexpected failure by throwing an exception. The endless dance of &ldquo;Do something; Did it succeed? If not, why did it fail. Do something else; Did it succeed? &hellip;&rdquo; is replaced by structured exception handling and constructs such as <a href="http://msdn.microsoft.com/en-us/library/0yd65esw.aspx">try-catch</a>, which let you defer thinking about error scenarios until you want to, rather than thinking&hellip; about errors&hellip; at every&hellip; step&hellip; of&hellip; the&hellip; way.</p>
<p>So if .NET methods such as <tt><a href="http://msdn.microsoft.com/en-us/library/b9skfh7s.aspx">File.Open()</a></tt> will throw exceptions rather than returning&nbsp; <tt>INVALID_HANDLE_VALUE</tt> we have no need to expose <tt>INVALID_HANDLE_VALUE</tt> in the .NET BCL, right? Not quite.</p>
<h3><tt>INVALID_HANDLE_VALUE</tt> as an input parameter</h3>
<p>In addition to being used as a magic return value indicating failure, <tt>INVALID_HANDLE_VALUE</tt> also has some magic powers with methods that accept a <tt>HANDLE</tt> as a parameter. Now, you won&rsquo;t get any useful behaviour from passing <tt>INVALID_HANDLE_VALUE</tt> to <tt><a href="http://msdn.microsoft.com/en-us/library/ms724211.aspx">CloseHandle</a></tt>, however there is a group of functions that let you provide an event handle, do some asynchronous work, and then signal your event to let you know the work has been completed.</p>
<p>Functions such as <tt><a href="http://msdn.microsoft.com/en-us/library/ms686876.aspx">UnregisterWaitEx</a></tt> and <tt><a href="http://msdn.microsoft.com/en-us/library/ms682569.aspx">DeleteTimerQueueTimer</a></tt> will cancel any pending registered wait operation or a timer-queue timer, however if a callback has already been triggered this will still run to completion. If you need to clean up any resources used by your callback, to avoid pulling the rug out from under its feet, you must first ensure that your callback is not still executing. To avoid having to manually introduce control synchronisation in your callback, <tt>UnregisterWaitEx</tt> and <tt>DeleteTimerQueueTimer</tt> let you provide an event handle which will be signalled when any executing callbacks have returned.</p>
<p>If you don&rsquo;t want the overhead of allocating another event and then registering a wait on it (in order to perform the clean-up asynchronously, when the event is signalled) <strong>you can tell the function to block and wait for any executing collback functions to complete before returning by providing <tt>INVALID_HANDLE_VALUE</tt> for the wait handle.</strong></p>
<h3>What about .NET?</h3>
<p>Now the interesting part: Since we previously concluded that there is no need to expose <tt>INVALID_HANDLE_VALUE</tt> in the .NET BCL, how would we get this handy blocking behaviour from the .NET equivalents to the methods mentioned above: <tt><a href="http://msdn.microsoft.com/en-us/library/system.threading.registeredwaithandle.unregister.aspx">RegisteredWaitHandle.Unregister(WaitHandle)</a></tt> and <tt><a href="http://msdn.microsoft.com/en-us/library/b97tkt95.aspx">System.Threading.Timer.Dispose(WaitHandle)</a></tt>?</p>
<p>The MSDN documentation makes no suggestion that this behaviour is even possible (not even in the <a href="http://msdn.microsoft.com/en-us/library/system.threading.registeredwaithandle.unregister(VS.100).aspx">preview</a> <a href="http://msdn.microsoft.com/en-us/library/b97tkt95(VS.100).aspx">documentation</a> for .NET 4). I&rsquo;m not sure if this is an oversight or an intentionally unsupported behaviour.</p>
<p>To work around this we can do a little poking around the BCL with Reflector:</p>
<ul>
<li><tt>WaitHandle</tt>, the base class for all events in .NET, has a <tt>static readonly IntPtr</tt> field called <tt>InvalidHandle</tt> which is populated with the value of <tt>INVALID_HANDLE_VALUE</tt>; but </li>
<li>Alas, <tt>WaitHandle.InvalidHandle</tt> is <tt>protected</tt>, rather than <tt>public</tt>! (Why, oh why?) </li>
<li>Conveniently, the internal handle value is initialised by the default constructor of <tt>WaitHandle</tt> to <tt>InvalidHandle</tt>; but </li>
<li>Alas, <tt>WaitHandle</tt> is marked <tt>abstract</tt>, so we can't instantiate it directly.<tt></tt> </li>
</ul>
<h3>Conclusion</h3>
<p>So the only way to get at <tt>INVALID_HANDLE_VALUE</tt> in .NET is to subclass <tt>WaitHandle</tt>. We don't actually need to do anything in our subclass, mind you:</p>
<blockquote>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">InvalidWaitHandle </span>: <span style="color: #2b91af">WaitHandle </span>{ }</pre>
</blockquote>
<p>So there you have it, pretty convoluted but works like a charm!</p>
<p>Here&rsquo;s the full version, with documentation and a cached instance:</p>
<pre class="code"><span style="color: blue">using </span>System.Threading;

<span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">An inert wait handle that can be used to avoid allocating a real event in
</span><span style="color: gray">/// </span><span style="color: green">some situations.
</span><span style="color: gray">/// &lt;/summary&gt;
/// &lt;remarks&gt;
/// &lt;para&gt;
/// </span><span style="color: green">An </span><span style="color: gray">&lt;see cref="InvalidWaitHandle"/&gt; </span><span style="color: green">can be provided to methods such as
</span><span style="color: gray">/// &lt;see cref="RegisteredWaitHandle.Unregister(WaitHandle)"/&gt; </span><span style="color: green">and 
</span><span style="color: gray">/// &lt;see cref="Timer.Dispose(WaitHandle)"/&gt;</span><span style="color: green">. In this case, the function waits
</span><span style="color: gray">/// </span><span style="color: green">for all callback functions to complete before returning, rather than
</span><span style="color: gray">/// </span><span style="color: green">returning immediately and signalling the provided wait handle
</span><span style="color: gray">/// </span><span style="color: green">asynchronously.
</span><span style="color: gray">/// &lt;/para&gt;
/// &lt;para&gt;
/// </span><span style="color: green">Internally, this results in the use of INVALID_HANDLE_VALUE when calling
</span><span style="color: gray">/// </span><span style="color: green">the underlying Win32 functions.
</span><span style="color: gray">/// &lt;/para&gt;
/// &lt;para&gt;
/// </span><span style="color: green">For further information, see "Concurrent Programming on Windows" (First
</span><span style="color: gray">/// </span><span style="color: green">Edition, 2009) by Joe Duffy, p. 374, 377.
</span><span style="color: gray">/// &lt;/para&gt;
/// &lt;/remarks&gt;
</span><span style="color: blue">public class </span><span style="color: #2b91af">InvalidWaitHandle </span>: <span style="color: #2b91af">WaitHandle
</span>{
    <span style="color: blue">static </span>InvalidWaitHandle()
    {
        Instance = <span style="color: blue">new </span><span style="color: #2b91af">InvalidWaitHandle</span>();
    }

    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Gets a shared instance of </span><span style="color: gray">&lt;see cref="InvalidWaitHandle"/&gt; </span><span style="color: green">which may
    </span><span style="color: gray">/// </span><span style="color: green">be re-used.
    </span><span style="color: gray">/// &lt;/summary&gt;
    /// &lt;remarks&gt;
    /// </span><span style="color: green">Using this field allows a single </span><span style="color: gray">&lt;see cref="InvalidWaitHandle"/&gt; </span><span style="color: green">to
    </span><span style="color: gray">/// </span><span style="color: green">be re-used as opposed to creating a </span><span style="color: gray">&lt;c&gt;</span><span style="color: green">new</span><span style="color: gray">&lt;/c&gt; </span><span style="color: green">instance at every call
    </span><span style="color: gray">/// </span><span style="color: green">site.
    </span><span style="color: gray">/// &lt;/remarks&gt;
    /// &lt;value&gt;</span><span style="color: green">A shared instance of </span><span style="color: gray">&lt;see cref="InvalidWaitHandle"/&gt; </span><span style="color: green">which may
    </span><span style="color: gray">/// </span><span style="color: green">be re-used.</span><span style="color: gray">&lt;/value&gt;
    </span><span style="color: blue">public static </span><span style="color: #2b91af">InvalidWaitHandle </span>Instance { <span style="color: blue">get</span>; <span style="color: blue">private set</span>; }
}
</pre>]]></description></item><item><title>I'm my own grandpa</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/01/09/im_my_own_grandpa</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/01/09/im_my_own_grandpa#Comments</comments><pubDate>2010-01-09T20:03:00Z</pubDate><category>random, obscure, joke</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2010/01/09/im_my_own_grandpa</guid><description><![CDATA[<p><a href="http://blogs.msdn.com/ericlippert/default.aspx">Eric Lippert</a> takes a short break from his usual barrage of mind-melting in-depth technical articles to provide a short <a href="http://blogs.msdn.com/ericlippert/archive/2010/01/04/first-cousins-once-removed.aspx">article on genealogy</a>, wherein he explains the simple system for naming cousin-hood and removed-ness in English.</p>
<p>This was all fairly straightforward, until he linked to a mind-melting video of a country singer asserting that he is his own grandfather, complete with an accompanying family tree diagram that updates as the song progresses to help you keep track of the facts:</p>
<p align="center">
<object data="http://www.youtube-nocookie.com/v/eYlJH81dSiw&amp;hl=en_GB&amp;fs=1&amp;rel=0&amp;color1=0x234900&amp;color2=0x4e9e00" type="application/x-shockwave-flash" width="425" height="344">
<param name="allowFullScreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="src" value="http://www.youtube-nocookie.com/v/eYlJH81dSiw&amp;hl=en_GB&amp;fs=1&amp;rel=0&amp;color1=0x234900&amp;color2=0x4e9e00" />
<param name="allowfullscreen" value="true" />
</object>
</p>
<p>So I guess the moral of the story is that directed graphs should ideally be kept acyclic if at all possible?</p>]]></description></item><item><title>Security vulnerabilities of concurrent pipeline processing - Part 2: The Reveal</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/08/24/security_vulnerabilities_of_concurrent_pipeline_processing__part_2_the_reveal</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/08/24/security_vulnerabilities_of_concurrent_pipeline_processing__part_2_the_reveal#Comments</comments><pubDate>2009-08-24T07:54:35Z</pubDate><category>security, concurrency, analogy</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/08/24/security_vulnerabilities_of_concurrent_pipeline_processing__part_2_the_reveal</guid><description><![CDATA[<p>Before we continue the exploration into security vulnerabilities of drive-through restaurants... Eric Lippert has returned from his holiday of pre-recorded posts on <a href="http://blogs.msdn.com/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx">mind-bending</a> <a href="http://blogs.msdn.com/ericlippert/archive/2009/08/17/arrays-of-arrays.aspx">C# trivia</a>, and come out with a rather <a href="http://blogs.msdn.com/ericlippert/archive/2009/08/20/queueing-theory-in-action-plus-frogs.aspx">amusing analysis of airport check-in queue strategies</a>, based on analytic queueing theory.</p> <p>I’m comforted to know that I’m not the only one to <a href="http://idioms.thefreedictionary.com/go+to+town+on">go to town on</a> analysis of <a href="http://asqui.multiply.com/reviews/item/14">everyday things</a>.</p> <p>And now, back to our regular programming...</p> <hr size="1" style="padding:0; margin:0">  <h4>Drive-through optimisation</h4> <p><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/08/18/security_vulnerabilities_of_concurrent_pipeline_processing__part_1_context">Last time</a>, I set the scene of a restaurant drive-through, with the hopes of illustrating how innocent optimisations can introduce security vulnerabilities if we’re not careful.</p> <p><strong>We forgot to do our threat modelling!</strong> Nobody stopped to ask the question “what are we changing? what does this affect? how could this compromise our security?”</p> <h4>The “null-order”</h4> <p>Let’s consider the “null-order”: An in-store customer reaches the head of the queue, walks up to the counter, and realises that she doesn’t have any money. The order transaction is safely aborted and the customer leaves. There is no need for the restaurant employee to enter the “null-order” in their computer because there is no order to be made, duh! (Note that the customer realised the lack of wallet before placing the order.)</p> <p>Now lets consider a “null-order” for the drive-through interface: A customer joins the back of the queue and is (often, but not always) in a single-lane one-way traffic system, where the only way out is to drive through the whole system: past the order point, the pay window, and the delivery window. If the drive-through is busy, there will be other cars occupying this space so the driver is stuck — they cannot escape the pipeline.</p> <p>When they realise they have no money (again, before placing the order) they will place a “null-order”. The employee will not enter the “null-order” into the computer because there is no order being made. Duh! The customer wants to leave the queue at this point but she is stuck, so has to drive to the payment window. She must then execute a “null-payment” and explain to the cashier that no payment is necessary because no order was placed. Duh! No problem. Then the customer drives to the third window, staffed by an employee whose job is to deliver food to the cars as fast as possible, preferably with a smile.</p> <h4>The exploit</h4> <p>So what does all this have to do with security vulnerabilities of concurrent pipeline processing?</p> <p>Well, one fun example of a non-software exploit is the fast-food drive-through free food scam:</p> <ol> <li>Go to the drive-through of a fast-food restaurant and make sure there is someone behind you in the queue.  <li>Don’t order anything (say you forgot to bring money).  <li>Go to the pay window and say you didn’t order anything.  <li>Proceed to the order pick-up window and collect the food that the car behind you is currently paying for.</li></ol> <p>Security specialist Bruce Schneier <a href="http://www.schneier.com/blog/archives/2007/09/how_to_get_free.html">describes this in more detail</a> and offers some solutions.</p> <h4>One solution</h4> <p>Continuing with my previous analogy: Since the order-point employee didn’t enter the “null-order” into the system (duh!) it didn’t get pushed onto the internal order queue. At that point the internal order queue was no longer synchronised with the physical queue of cars. Therefore, the cashier didn’t know she was expecting a “null-payment” from this customer, but this could be explained away by the customer. If the customer doesn’t also explain this at the order-delivery window they will be handed the order placed by the car behind (which is currently being paid for right behind them!)</p> <p>By introducing this pipeline optimisation, we’ve suddenly introduced the need for the in-sore systems to be able to process a “null-order”, which sounded like a ludicrous idea for the in-store scenario, but is actually essential for keeping the drive-through queue synchronised with the internal order queue.</p> <p>As I said before, security is a burden that you must carry through any refactoring, optimisations, or other changes; even those that seem unrelated.</p>]]></description></item><item><title>Security vulnerabilities of concurrent pipeline processing - Part 1: Context</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/08/18/security_vulnerabilities_of_concurrent_pipeline_processing__part_1_context</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/08/18/security_vulnerabilities_of_concurrent_pipeline_processing__part_1_context#Comments</comments><pubDate>2009-08-18T07:09:57Z</pubDate><category>security, concurrency, analogy</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/08/18/security_vulnerabilities_of_concurrent_pipeline_processing__part_1_context</guid><description><![CDATA[<p>In the spirit of <a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/01/13/going_quad_optimal_toiletpaper_strategies_for_the_modern_geek">software analogies to everyday things</a>, and continuing <a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/01/thoughts_on_software_security">ramblings on software security</a>, lets consider a fast-food restaurant in a software engineering context.</p> <p>The fast-food restaurant’s basic feature is the ability to go in, order some food, pay for it, and receive the order (preferably within a short period of time).</p> <h4>The fast food drive-through</h4> <p>At some point this wasn’t enough. Cars got popular, people were in a rush, and a new interface was dreamt up which could allow this workflow to be executed without having to park your car and enter the restaurant — the drive-through was born.</p> <p>Presumably the na<span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: 13px arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0" class="Apple-style-span">ïv</span>e drive-through interface would mimic the user experience of the in-store interface: There are several windows with individual queues, you pick one and join the queue with your car, drive up to the window, place your order, pay, and receive food all at the same window, then leave.</p> <p>This presents some problems because cars are not as small and agile as humans, so trying to mimic the same layout for cars as humans isn’t going to work very well. (You’d need a massive amount of room to let the car from each window drive off independently.)</p> <p>Given the amount of room available at a typical restaurant, the drive-through would need to only have a single queue. You would join the back of the queue, eventually reach the window, place and order, pay, and receive food all at the same window.</p> <h4>Optimising throughput at the drive-through</h4> <p>This design is still as secure as the in-store experience, but because there is only one window and one queue it is a lot slower than the in-store experience, where there are multiple restaurant employees serving multiple customer queues concurrently.</p> <p>So how do we make a multi-stage process at the lone drive-through window more efficient? We introduce parallelism through pipelining (as seen in most processor architectures these days). We split the multi-function order/payment/deliver window up into three separate windows (or a microphone and two windows) and specialise each window to a single task. (Fast-food restaurants are already good at efficiency through task specialisation.)</p> <p>Now the order window (or microphone) can be ready to take the next order as soon as it is done taking the current one. The payment window can be taking payment for the first order whilst the second order is being dictated. Then the third window can be delivering the order whilst the second window is taking payment for the next order, and the order window (or microphone) is taking yet a <em>third </em>order.</p> <p>Genius! We’ve improved the efficiency by allowing three customers to be served at once, whilst keeping to the space constraint of only having a single queue of cars at the drive-through.</p> <h4>Can you spot the security hole we’ve opened up with this optimisation?</h4>]]></description></item><item><title>Duck Typing</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/28/duck_typing</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/28/duck_typing#Comments</comments><pubDate>2009-07-28T07:37:05Z</pubDate><category>.net, obscure</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/28/duck_typing</guid><description><![CDATA[<h4>Duck Typing Definition</h4> <p>If it looks like a duck, and quacks like a duck, then it must be a duck!</p> <blockquote> <p><em>“In computer programming, <strong>duck typing</strong> is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.” </em>— <a href="http://en.wikipedia.org/wiki/Duck_Typing">Wikipedia</a></p></blockquote> <p>With duck-typing an interface implementation is implicit once you have implemented the relevant members. .NET does not currently have any broad support for this, however, with the emergent dynamic language features, I wouldn't be surprised if this were supported natively by the runtime in the near future.</p> <p>In the mean time, you can synthesise duck-typing via reflection, with <a href="http://www.deftflux.net/blog/page/Duck-Typing-Project.aspx" rel="nofollow"><font color="#0066cc">a library such as this</font></a>, which would allow you to do a duck-typed cast like this:</p> <blockquote> <p><font face="Courier New">IDoo myDoo = DuckTyping.Cast&lt;IDoo&gt;(myFoo)</font></p></blockquote> <h4>Duck Typing Trivia</h4> <p>Interestingly, there is one small place where duck-typing is in use in C# today — the <code>foreach</code> operator. Krzysztof Cwalina <a href="http://blogs.msdn.com/kcwalina/archive/2007/07/18/DuckNotation.aspx" rel="nofollow"><font color="#0066cc">states</font></a> that in order to be enumerable by the <code>foreach</code> operator, a class must:</p> <blockquote> <p><em>Provide a public method GetEnumerator that takes no parameters and returns a type that has two members: a) a method MoveMext that takes no parameters and return a Boolean, and b) a property Current with a getter that returns an Object.</em></p></blockquote> <p>Notice that he makes no mention of <code>IEnumerable</code> nor <code>IEnumerator</code>. Although it is common to implement these interfaces when creating an enumerable class, if you were to drop the interfaces but leave the implementation, your class would still be enumerable by <code>foreach</code>. Voila! Duck-typing!</p> <h4>Example Code</h4> <p>But don’t take my word for it. Here’s some demo code to prove it:</p> <blockquote><pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">Program
</span>{
    <span style="color: blue">public static void </span>Main()
    {
        <span style="color: blue">foreach </span>(<span style="color: blue">int </span>i <span style="color: blue">in new </span><span style="color: #2b91af">DuckEnumerable</span>())
            <span style="color: #2b91af">Console</span>.WriteLine(i);
        <span style="color: #2b91af">Console</span>.ReadKey();
    }
}

<span style="color: blue">public class </span><span style="color: #2b91af">DuckEnumerable </span><span style="color: green">// Not IEnumerable
</span>{
    <span style="color: blue">public </span><span style="color: #2b91af">Duck </span>GetEnumerator()
    {
        <span style="color: blue">return new </span><span style="color: #2b91af">Duck</span>();
    }
}

<span style="color: blue">public class </span><span style="color: #2b91af">Duck </span><span style="color: green">// Not IEnumerator
</span>{
    <span style="color: blue">private int </span>n = 0;

    <span style="color: blue">public int </span>Current
    {
        <span style="color: blue">get </span>{ <span style="color: blue">return this</span>.n; }
    }

    <span style="color: blue">public bool </span>MoveNext()
    {
        <span style="color: blue">return </span>(<span style="color: blue">this</span>.n++ &lt; 10);
    }
}</pre></blockquote>]]></description></item><item><title>XML Serialization Bug in .NET 2.0 SP2</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/10/xml_serialization_bug_in_net_20_sp2</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/10/xml_serialization_bug_in_net_20_sp2#Comments</comments><pubDate>2009-07-10T07:25:45Z</pubDate><category>bug, .net, obscure</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/10/xml_serialization_bug_in_net_20_sp2</guid><description><![CDATA[<p>It has not been a good week for me with the <tt>System.Xml</tt> namespace — I’ve found two bugs in two days! <a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/09/poor_argument_validation_in_xmldocumentloadstream">Yesterday’s discovery of poor argument validation in XmlDocument.Load(Stream)</a><tt></tt> was a pretty minor point really, a case of nice framework design style; as a contrast, today I uncovered a bona fide bug with XML Serialization in the <tt>XmlSerializer</tt> class. This is more serious: a case of compliance with the XML specification!</p> <h4>Overview</h4> <p>When generating an XML serializer for schemas that feature a free-form xs:any node, the deserialization behaviour is incorrect in some scenarios.</p> <p>For example, if you want the capability to hold an arbitrary fragment of XML configuration that is specified in a client-specific schema (which is not known up front, and cannot be included in your schema) you might address this by including a “freestyle” configuration element like this.</p><pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">xs:element </span><span style="color: red">name</span><span style="color: blue">=</span>"<span style="color: blue">config</span>" <span style="color: red">minOccurs</span><span style="color: blue">=</span>"<span style="color: blue">0</span>"<span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">xs:complexType </span><span style="color: red">mixed</span><span style="color: blue">=</span>"<span style="color: blue">true</span>"<span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">xs:sequence </span><span style="color: red">minOccurs</span><span style="color: blue">=</span>"<span style="color: blue">0</span>"<span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">xs:any </span><span style="color: red">processContents</span><span style="color: blue">=</span>"<span style="color: blue">skip</span>" <span style="color: blue">/&gt;
    &lt;/</span><span style="color: #a31515">xs:sequence</span><span style="color: blue">&gt;
  &lt;/</span><span style="color: #a31515">xs:complexType</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">xs:element</span><span style="color: blue">&gt;</span></pre>
<p>If you then use the <tt>XmlSerializer</tt> to deserialize this configuration, (or the <tt>sgen</tt> utility, which uses <tt>XmlSerializer</tt> under the covers) you may run in to the problem detailed below.</p>
<h4>Walkthrough</h4>
<p>Consider the following pair of documents:</p>
<p><u>Document A</u></p><pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">Root</span><span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">FirstChild</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">config</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">config</span><span style="color: blue">&gt;
  &lt;/</span><span style="color: #a31515">FirstChild</span><span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">SecondChild</span><span style="color: blue">/&gt;
&lt;/</span><span style="color: #a31515">Root</span><span style="color: blue">&gt;
</span></pre>
<p><u>Document B</u></p><pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">Root</span><span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">FirstChild</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">config</span><span style="color: blue">/&gt;
  &lt;/</span><span style="color: #a31515">FirstChild</span><span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">SecondChild</span><span style="color: blue">/&gt;
&lt;/</span><span style="color: #a31515">Root</span><span style="color: blue">&gt;</span></pre>
<p>These two documents should be equivalent. The only difference is between <tt><font color="#0000ff">&lt;</font><span style="color: #a31515">config</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">config</span><span style="color: blue">&gt;</span></tt> and <tt><font color="#0000ff">&lt;</font><span style="color: #a31515">config</span><span style="color: blue">/&gt;</span></tt>. Just to make sure I’m not going insane, the <a href="http://www.w3.org/TR/2008/REC-xml-20081126/#dt-empty">XML Specification</a> says that <em>“The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag.” </em>So these two documents are slightly different representations of something which should be semantically identical. The output from XML deserialization <em>should</em> be the same for both.</p>
<p>However, this is not the case (at least not in .NET 2.0 SP 2, version 2.0.50727.3053).</p>
<p><strong>Document A</strong> is deserialized successfully. The FirstNode and SecondNode elements are non-null in the resulting class instance. </p>
<p><strong>Document B</strong> is also deserialized successfully, however the <tt>XmlSerializer</tt> instance raises its <tt>UnknownNode</tt> event for the SecondChild node. In the resulting deserialized class instance, the FirstNode is accessible, but <strong>the SecondNode element is null</strong>!</p>
<h4>Root Cause</h4>
<p>The reason for this appears to be a bug in the generated serializer class which causes the method responsible for deserializing FirstChild (method <tt>Read2_RootFirstChild</tt> in the repro solution, linked below) to overrun past the end of the FirstChild element, and consume the SecondChild element from the reader, reporting it as an unrecognised element. (See 2l5qpkfr.0.cs line 251 in the repro solution.)</p>
<p>Subsequently, the method responsible for deserializing SecondChild (method <tt>Read1_Object</tt>) is unable to deserialize this element because it has already been consumed!</p>
<h4>Standalone Reproduction of the problem</h4>
<p>Here is a Visual Studio solution with a standalone repro of the problem, including an annotated copy of the generated serialization classes, showing where I believe the bug to be:</p>
<p><a style="text-decoration: none" href="http://www.danielfortunov.com/software/~Media?id=7dc3a5eb-3661-41e0-a01c-edbba9f0f687"><img border="0" align="absMiddle" src="http://www.danielfortunov.com/images/filetype/zip.png" width="16" height="16"> XmlSerializationRepro</a></p>
<p>I was unable to find this bug on Microsoft Connect so I am not sure if this issue is known to Microsoft.</p>
<p><strong>Update (12 July 2009):</strong> Reported this on <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=473967">Microsoft Connect</a>.</p>]]></description></item><item><title>Poor argument validation in XmlDocument.Load(Stream)</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/09/poor_argument_validation_in_xmldocumentloadstream</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/09/poor_argument_validation_in_xmldocumentloadstream#Comments</comments><pubDate>2009-07-09T08:41:00Z</pubDate><category>analysis, bug, .net, obscure, "visual studio", exceptions</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/09/poor_argument_validation_in_xmldocumentloadstream</guid><description><![CDATA[<p>Whilst browsing the .NET 3.5 framework source code yesterday, I came across an odd peculiarity with the <tt>XmlDocument</tt> class. The XmlDocument.Load(stream) method does not appear to be validating the input stream against a null value. I couldn’t believe it was a real bug until I had reproduced it with the following one-liner:</p><pre class="code">(<span style="color: blue">new </span><span style="color: #2b91af">XmlDocument</span>()).Load((<span style="color: #2b91af">Stream</span>)<span style="color: blue">null</span>);</pre>
<p>Which results in the following exception:</p><pre>System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Xml.XmlReader.CalcBufferSize(Stream input)
   at System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding)
   at System.Xml.XmlTextReaderImpl.InitStreamInput(Stream stream, Encoding encoding)
   at System.Xml.XmlTextReaderImpl..ctor(String url, Stream input, XmlNameTable nt)
   at System.Xml.XmlTextReader..ctor(Stream input, XmlNameTable nt)
   at System.Xml.XmlDocument.Load(Stream inStream)
   at Scratch.XmlDocumentNullCheckTest.CallLoadWithNullStream()</pre>
<p>You can see why I was unsure of myself — the rogue null stream drills in through <em>five</em> levels of functions before reaching the innocent-enough <tt>CalcBufferSize()</tt> method, which asks if the null stream if it <tt>CanSeek</tt>.</p>
<p>What <tt>XmlDocument.Load(Stream)</tt> should really do is to trap my null input right at the front door, and throw the specialised exception which exists for this exact scenario, <tt>System.ArgumentNullException</tt>, instead of exposing its implementation internals to my rogue input value.</p>
<h4>Another Culprit: <tt>XmlTextReader</tt></h4>
<p>The first thing <tt>XmlDocument.Load(Stream)</tt> does is to delegate the task to the <tt>XmlTextReader</tt> class. Since <tt>XmlTextReader</tt> is a public class, and its <tt>(Stream, XmlNameTable)</tt> constructor is also public, then <tt>XmlTextReader</tt> is also in violation. (Perhaps the author of <tt>XmlDocument.Load()</tt> felt it acceptable to not validate for a null stream because they were relying on the </tt>XmlTextReader</tt> constructor doing that check for them?)</p>
<p>[ These problems exist in System.Xml 2.0.0.0 (2.0.50727.4918) but have probably been fixed in the version that ships as part of .NET 4.0. ]</p>
<h4>Static Analysis: CA1062</h4>
<p>The other reason I refused to believe this could be true until I had reproduced it is because I know that there is a static code analysis rule built in for this exact scenario: <a href="http://msdn.microsoft.com/en-us/library/ms182182(loband).aspx">CA1062: Validate arguments of public methods</a>. This rule should trigger for both <tt>XmlDocument.Load(Stream)</tt> and <font face="Courier New">XmlTextReader..ctor<tt>(Stream, XmlNameTable)</tt></font> to remind the developer to validate any reference arguments passed in to externally visible methods.</p>
<h4>Not in VS2008</h4>
<p>I tried to validate the behaviour of this rule when I was reminded that this static analysis rule (and a few others) were actually <em><a href="http://msmvps.com/blogs/vstsblog/archive/2008/01/13/some-code-analysis-rules-gone-in-visual-studio-2008.aspx">removed in Visual Studio 2008</a></em>! One of the <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=270831&amp;wa=wsignin1.0">buggy and ill-performing</a> static analysis engines was cut loose for VS2008 (along with the rules that depended upon it) as part of a longer-term strategic move to write a <a href="http://blogs.msdn.com/fxcop/archive/2008/01/07/faq-which-rules-shipped-in-which-version.aspx">new data flow analysis engine</a> based on <a href="http://research.microsoft.com/en-us/collaboration/focus/cs/phoenix.aspx">MSR’s “Phoenix”</a>.</p>
<h4>Back for VS2010</h4>
<p>The rules that were removed for 2008 were reinstated in the Visual Studio 2010 September CTP. When they talk about “<em><a href="http://blogs.msdn.com/fxcop/archive/2008/10/30/new-code-analysis-features-in-visual-studio-2010-september-08-ctp.aspx">8 New Data Flow rules</a></em>” in the September CTP, I guess they really mean “8 <em>old</em> rules from VS2005, that were removed in VS 2008, are now back” — I confirmed this by correlating the new analysis rules listed in the VS2010 September CTP Walkthroughs document with the list of <a href="http://msmvps.com/blogs/vstsblog/archive/2008/01/13/some-code-analysis-rules-gone-in-visual-studio-2008.aspx">rules removed in VS 2008</a> due to removal of the data flow engine.</p>
<p>I was hoping that some new rules would also pop up in the Beta, but there’s been no mention of this on the <a href="http://blogs.msdn.com/fxcop/">Code Analysis Team Blog</a> so maybe they only managed to reinstate the old rules that were removed for now. This is understandable, considering they had to re-write the data flow analysis engine.</p>
<p><strong>Update (12 July 2009):</strong> Reported on <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=473970">Microsoft Connect</a>.</p>
<p><strong>Update (16 July 2009):</strong> This can’t be fixed because it might break backward compatibility for applications that are already trapping the <tt>NullReferenceException</tt>. Ah, one of the joys of strict framework versioning: Unfixable bugs!</p>]]></description></item><item><title>Compare-and-Swap in SQL: Part 3</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/04/compareandswap_in_sql_part_3</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/04/compareandswap_in_sql_part_3#Comments</comments><pubDate>2009-07-04T06:37:38Z</pubDate><category></category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/04/compareandswap_in_sql_part_3</guid><description><![CDATA[<p><em>(See </em><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/06/11/compareandswap_in_sql_part_1"><em>Compare-and-Swap in SQL: Part 1</em></a><em> and </em><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/06/12/compareandswap_in_sql_part_2"><em>Part 2</em></a><em> to set the context for this post.)</em></p> <p>So we’ve concluded that the naïve implementation of up_CompareAndSwap is woefully inadequate in achieving its main goal: performing an atomic compare-and-swap operation.</p> <h4>The Succinct Solution: Fancy Update Statements</h4> <p>One way to resolve this is to make our implementation more succinct. The programmatic CAS operation is implemented by a single compare-exchange instruction at the CPU level; maybe we can implement the core of up_CompareAndSwap with a single compare-update SQL statement?</p> <p>This would mean combining the <span style="font-family: monospace; color: blue">select</span>-check-<span style="font-family: monospace; color: blue">update</span> stages of the <a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/06/11/compareandswap_in_sql_part_1">original stored procedure</a> into a single <span style="font-family: monospace; color: blue">update</span> statement. Something along these lines:</p><pre class="code"><span style="color: blue">update </span>KeyValuePairs
<span style="color: blue">set </span>[Value] <span style="color: gray">= </span>@p_value
<span style="color: blue">where </span>[Key] <span style="color: gray">= </span>@p_key
    <span style="color: gray">and </span>[Value] <span style="color: gray">= </span>@p_comparand</pre>
<p>Then we can test <span style="color: magenta">@@ROWCOUNT</span> to find out if the row was updated or not, but sadly this is not enough. Even in this simple scenario, we need more information to fulfil the return codes contract:<pre class="code"><span style="color: green">-- RETURNS: 0 if KeyValuePairs was updated;
--          1 if KeyValuePairs was not updated;
--          2 if the specified p_key was not found.</span></pre>
<p>If <tt><span style="color: magenta">@@ROWCOUNT</span></tt> is 0 we can’t know if this was because the Key comparison failed (in which case we should return 2) or if it was because the Value part of the comparison failed (in which case we should return 1). There isn’t enough information.</p>
<p>Fortunately it turns out that the <span style="color: blue">set</span> clause can do a lot more than just assign values to columns. We can go ahead and extract the current value of the column before we update it, right in the <span style="color: blue">set</span> clause:</p><pre class="code"><span style="color: green">-- Optimistically update KeyValuePairs
</span><span style="color: blue">update </span>KeyValuePairs
<span style="color: blue">set </span>@currentValue <span style="color: gray">= </span>[Value]<span style="color: gray">,
    </span>[Value] <span style="color: gray">= </span>@p_value
<span style="color: blue">where </span>[Key] <span style="color: gray">= </span>@p_key</pre>
<p>Instead of checking the current value against <tt>@p_comparand</tt> in the <span style="color: blue">where </span>clause, we go ahead and update it optimistically.</p>
<p>Now we know that if <tt><span style="color: magenta">@@ROWCOUNT</span></tt> is 0, it must be because that Key was not found in the table, so we can return 2:</p><pre class="code"><span style="color: blue">if </span><span style="color: gray">(</span><span style="color: magenta">@@ROWCOUNT </span><span style="color: gray">!= </span>1<span style="color: gray">)
</span><span style="color: blue">begin
    raiserror</span><span style="color: gray">(</span><span style="color: red">'Key &lt;%s&gt; was not found.'</span><span style="color: gray">, </span>16<span style="color: gray">, </span>0<span style="color: gray">, </span>@p_key<span style="color: gray">)
    </span><span style="color: blue">commit transaction
    return </span>2
<span style="color: blue">end</span></pre>
<p>If the Key was found, and we did update a row, we can retrospectively validate the previous value against <tt>@p_comparand</tt>:</p><pre class="code"><span style="color: green">-- Validate the old value against p_comparand and rollback if necessary
</span><span style="color: blue">if </span><span style="color: gray">(</span>@previousValue <span style="color: gray">!= </span>@p_comparand<span style="color: gray">)
</span><span style="color: blue">begin
    print </span><span style="color: red">'Comparand does not match. Value was not updated.'
    </span><span style="color: blue">rollback transaction
    return </span>1
<span style="color: blue">end</span></pre>
<p>If the values don’t match, we undo our update by rolling back the transaction, and return 1 to signal that the entry was not updated.</p>
<p>One downside to this approach is that our optimistic update may be visible to readers using READ UNCOMMITTED isolation (e.g. via the NOLOCK hint) before being rolled back. Such readers could observe a phantom value “that never was”, but then again, that is exactly what you bargain for when you start to ignore transaction isolation boundaries.</p>
<h4>The Explicit Solution: Locking Hints</h4>
<p>My final variant on this stored procedure is to explicitly decorate the initial <span style="font-family: monospace; color: blue">select</span> statement with a locking hint. Our problem before was that the <span style="font-family: monospace; color: blue">select</span> statement only acquires a ‘Shared’ lock for the duration of the statement, and then releases this lock and leaves the row open to change.</p>
<p>We can use the HOLDLOCK hint to ensure that the ‘Shared’ lock is held until the end of the transaction. This protects us from having someone else update the row after we’ve read it and before we’ve updated it. However, this is not enough: Another instance of this same procedure could run concurrently and read the current, unchanged, value. Its validation would pass, and it would attempt to update the value, only then would it be blocked and wait for the ‘Shared’ lock to be released. After we write <em>our </em>new value, this concurrently executing instance would immediately overwrite it with <em>its</em> new value! That’s no good.</p>
<p>So we need something stronger. The XLOCK hint will force our ‘Shared’ lock to be upgraded to an ‘Exclusive’ lock, which will prevent anyone else from reading the value. This solves the problem noted above, but is a little too restrictive. We don’t have any problem with anyone reading the value, so long as they’re not reading it with the intent of later updating it.</p>
<p>SQL Server has another lock mode called ‘Update’, which communicates this exact intent of “reading with an intent to update”. We can use the UPDLOCK hint to acquire this ‘Update’ lock as part of the <span style="font-family: monospace; color: blue">select</span> statement. This will prevent anyone else acquiring an ‘Exclusive’ or ‘Update’ lock in the interim between our <span style="font-family: monospace; color: blue">select</span> and <span style="font-family: monospace; color: blue">update</span> statements. (A concurrently executing instance of the same procedure would attempt to acquire an ‘Update’ lock when reading the data, and will therefore be blocked on its <span style="font-family: monospace; color: blue">select</span> statement until our transaction is complete.) Any other read-only process, however, is still free to read the current value during this time. Once we reach the <span style="font-family: monospace; color: blue">update</span> statement the ‘Update’ lock will automatically be upgraded to an ‘Exclusive’ lock, and at this point both readers and writers would be blocked from accessing that row until the transaction completes.</p>
<p>If we want to ensure that this ‘Update’ lock is only acquired on the unique row in question, rather than an entire data page from the table, we can additionally use the ROWLOCK hint. This will ensure that if there is high contention for updating the KeyValuePairs table we won’t be needlessly serialising updates to separate rows.</p><pre class="code"><span style="color: green">/* Procedure up_CompareAndSwap

 Compares the value stored for p_key in the KeyValuePairs table
 against the p_comparand value.
   * If they match, the new p_value is stored in KeyValuePairs.
   * If they do not match, KeyValuePairs is not updated.

 Returns: 0 if KeyValuePairs was updated;
          1 if KeyValuePairs was not updated;
          2 if the specified p_key was not found.
*/
</span><span style="color: blue">create procedure </span>up_CompareAndSwap 
    @p_key <span style="color: blue">nvarchar</span><span style="color: gray">(</span>50<span style="color: gray">),
    </span>@p_value <span style="color: blue">int</span><span style="color: gray">,
    </span>@p_comparand <span style="color: blue">int
as
begin
    begin transaction

    </span><span style="color: green">-- Read current value and hold on to an Update lock on the row
    </span><span style="color: blue">declare </span>@currentValue <span style="color: blue">int
    select </span>@currentValue <span style="color: gray">= </span><span style="color: blue">Value
    from </span>KeyValuePairs <span style="color: blue">with </span><span style="color: gray">(</span>UPDLOCK<span style="color: gray">, </span>ROWLOCK<span style="color: gray">)
    </span><span style="color: blue">where </span>[Key] <span style="color: gray">= </span>@p_key
    
    <span style="color: blue">if </span><span style="color: gray">(</span><span style="color: magenta">@@ROWCOUNT </span><span style="color: gray">!= </span>1<span style="color: gray">)
    </span><span style="color: blue">begin
        raiserror</span><span style="color: gray">(</span><span style="color: red">'Key &lt;%s&gt; was not found.'</span><span style="color: gray">, </span>16<span style="color: gray">, </span>0<span style="color: gray">, </span>@p_key<span style="color: gray">)
        </span><span style="color: blue">commit transaction
        return </span>2
    <span style="color: blue">end

    </span><span style="color: green">-- Compare to the expected value (and return if they don't match)
    </span><span style="color: blue">if </span><span style="color: gray">(</span>@currentValue <span style="color: gray">!= </span>@p_comparand<span style="color: gray">)
    </span><span style="color: blue">begin
        print </span><span style="color: red">'Comparand does not match. Value was not updated.'
        </span><span style="color: blue">commit transaction
        return </span>1
    <span style="color: blue">end

    </span><span style="color: green">-- Update to the new value
    -- We hold an Update lock on this row, so it couldn't have changed
    -- since we read it. The Update lock will be upgraded to 'Exclusive'
    </span><span style="color: blue">update </span>KeyValuePairs
    <span style="color: blue">set </span>[Value] <span style="color: gray">= </span>@p_value
    <span style="color: blue">where </span>[Key] <span style="color: gray">= </span>@p_key

    <span style="color: blue">print </span><span style="color: red">'Value was updated successfully.'
    </span><span style="color: blue">commit transaction
    return </span>0
<span style="color: blue">end
</span>GO</pre>
<p>This solution has the advantage of being simpler to implement, and simpler to retrofit to the an existing procedure — all I had to change was the locking hints for the initial <span style="font-family: monospace; color: blue">select</span> statement. It also feels cleaner to have done the necessary checks before updating the row, rather than optimistically updating it ahead of the checks, and potentially rolling it back subsequently.</p>
<h4>The End</h4>
<p>I hope you’ve enjoyed learning a little bit about SQL Server with me. As you can probably tell, I’m a bit of a novice when it comes to this subject matter. If there are any mistakes in what I’ve said above (or in the previous posts in this series), please let me know in the comments. Thanks!</p>]]></description></item><item><title>Compare-and-Swap in SQL: Part 2</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/06/12/compareandswap_in_sql_part_2</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/06/12/compareandswap_in_sql_part_2#Comments</comments><pubDate>2009-06-12T08:32:04Z</pubDate><category></category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/06/12/compareandswap_in_sql_part_2</guid><description><![CDATA[<p><em>(See </em><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/06/11/compareandswap_in_sql_part_1"><em>Compare-and-Swap in SQL: Part 1</em></a><em> to set the context for this post.)</em></p> <p>There are a couple of problems with the intentionally-buggy CompareAndSwap stored procedure I published yesterday. Let’s take another look:</p> <h4>up_CompareAndSwap</h4><pre class="code"><span style="color: green">/* Procedure up_CompareAndSwap

 Compares the value stored for p_key in the KeyValuePairs table
 against the p_comparand value.
   * If they match, the new p_value is stored in KeyValuePairs.
   * If they do not match, KeyValuePairs is not updated.

 Returns: 0 if KeyValuePairs was updated;
          1 if KeyValuePairs was not updated;
          2 if the specified p_key was not found.
*/
</span><span style="color: blue">create procedure </span>up_CompareAndSwap 
    @p_key <span style="color: blue">nvarchar</span><span style="color: gray">(</span>50<span style="color: gray">),
    </span>@p_value <span style="color: blue">int</span><span style="color: gray">,
    </span>@p_comparand <span style="color: blue">int</span><span style="color: gray">
</span><span style="color: blue">as
begin
    begin transaction

    </span><span style="color: green">-- Read current value
    </span><span style="color: blue">declare </span>@currentValue <span style="color: blue">int
    select </span>@currentValue <span style="color: gray">= </span><span style="color: blue">Value
    from </span>KeyValuePairs
    <span style="color: blue">where </span>[Key] <span style="color: gray">= </span>@p_key
    
    <span style="color: blue">if </span><span style="color: gray">(</span><span style="color: magenta">@@ROWCOUNT </span><span style="color: gray">!= </span>1<span style="color: gray">)
    </span><span style="color: blue">begin
        raiserror</span><span style="color: gray">(</span><span style="color: red">'Key &lt;%s&gt; was not found.'</span><span style="color: gray">, </span>16<span style="color: gray">, </span>0<span style="color: gray">, </span>@p_key<span style="color: gray">)
        </span><span style="color: blue">commit transaction
        return </span>2
    <span style="color: blue">end

    </span><span style="color: green">-- Compare to the expected value (and return if they don't match)
    </span><span style="color: blue">if </span><span style="color: gray">(</span>@currentValue <span style="color: gray">!= </span>@p_comparand<span style="color: gray">)
    </span><span style="color: blue">begin
        print </span><span style="color: red">'Comparand does not match. Value was not updated.'
        </span><span style="color: blue">commit transaction
        return </span>1
    <span style="color: blue">end

    </span><span style="color: green">-- Update to the new value
    </span><span style="color: blue">update </span>KeyValuePairs
    <span style="color: blue">set </span>[Value] <span style="color: gray">= </span>@p_value
    <span style="color: blue">where </span>[Key] <span style="color: gray">= </span>@p_key

    <span style="color: blue">print </span><span style="color: red">'Value was updated successfully.'
    </span><span style="color: blue">commit transaction
    return </span>0
<span style="color: blue">end
</span>GO</pre>
<p></p>
<p>What’s wrong with this stored procedure?</p>
<h4>The Problem: Atomicity</h4>
<p>If we compare the naïve CompareAndSwap stored procedure above to a typical <a href="http://en.wikipedia.org/wiki/Compare-and-swap">CAS</a> operation, the glaring shortfall is that a true CAS operation is <strong>Atomic</strong>; to the extent that it is actually implemented by a single compare-exchange instruction at the CPU level.</p>
<p>The naïve CompareAndSwap stored procedure, on the other hand, has a distinct read-modify-write flow, and despite being enclosed in a SQL Transaction, there are no guarantees of atomicity (at the default READ COMMITTED transaction isolation level).</p>
<p>The read operation will acquire a Shared lock on the particular row of the KeyValuePairs table only for the duration of the <span style="font-family: monospace; color: blue">select</span> statement. After this statement is complete the lock is relinquished, leaving other transactions (including a concurrently executing instance of the same stored procedure!) free to read and modify this row before the <span style="font-family: monospace; color: blue">update</span> statement has executed.</p>
<p><pre class="code"><span style="color: red"></span></pre>Unfortunately this undermines the correctness of this procedure, which was to “make sure that nobody else has changed the value while you were busy transforming it”. If two processes execute the stored procedure simultaneously, both <span style="font-family: monospace; color: blue">select</span> statements will succeed, and then second <span style="font-family: monospace; color: blue">update</span> statement to execute will immediately overwrite the value stored by the first.</p>
<p>You can convince yourself of this by adding an artificially long delay between the <span style="font-family: monospace; color: blue">select</span> and subsequent <span style="font-family: monospace; color: blue">update</span> statement…</p><pre class="code"><span style="color: blue">if </span><span style="color: gray">(</span>@p_slow <span style="color: gray">= </span>1<span style="color: gray">)
    </span><span style="color: blue">waitfor </span>delay <span style="color: red">'00:00:10'</span></pre>
<p>…and then executing concurrent queries targeting the same key.</p>
<p><strong>This is bad!</strong> We need some way to bring atomicity to the CompareAndSave stored procedure.</p>
<p>Read on in <em><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/07/04/compareandswap_in_sql_part_3">Compare-and-Swap in SQL: Part 3</a>.</em></p>]]></description></item><item><title>Compare-and-Swap in SQL: Part 1</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/06/11/compareandswap_in_sql_part_1</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/06/11/compareandswap_in_sql_part_1#Comments</comments><pubDate>2009-06-11T08:02:00Z</pubDate><category>sql, concurrency</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/06/11/compareandswap_in_sql_part_1</guid><description><![CDATA[<p>Consider a stored procedure which implements a <a href="http://en.wikipedia.org/wiki/Compare-and-swap">Compare-and-Swap (CAS)</a> operation for a Key-Value mapping table.</p> <p>When calling the procedure you specify three things:</p> <ul> <li>A Key, which identifies a row in the table;  <li>The new Value that you would like to associate with that Key; and  <li>What you think the current Value associated with that Key is.</li></ul> <p>The procedure will swap in your new Value for that Key, but only if the current Value is still what you thought it was.</p> <p>This allows you to read the value (e.g. 42), transform it in some way (e.g. increment it to 43), then store the new value, making sure that nobody else has changed the value while you were busy transforming it.</p> <p>For example, if someone else changed the value to 123 while you were incrementing to 43, it would not be valid for you to store 43 as the incremented value because then you would be overwriting the change to 123, as if it never happened. What you would need to do is start over again, with 123, increment it to 124, and store that (so long as nobody has changed the 123 to yet another value in the mean time).</p> <p>Here’s a naïve stored procedure that implements this logic:</p><pre class="code"><span style="color: green">-- KeyValuePairs table definition
</span><span style="color: blue">create table </span>dbo<span style="color: gray">.</span>KeyValuePairs <span style="color: gray">(
    </span>[Key] <span style="color: blue">nvarchar</span><span style="color: gray">(</span>50<span style="color: gray">) not null </span><span style="color: blue">primary key</span><span style="color: gray">,
    </span><span style="color: blue">Value int </span><span style="color: gray">not null)
</span>GO</pre><pre class="code"><span style="color: green">/* Procedure up_CompareAndSwap

 Compares the value stored for p_key in the KeyValuePairs table
 against the p_comparand value.
   * If they match, the new p_value is stored in KeyValuePairs.
   * If they do not match, KeyValuePairs is not updated.

 Returns: 0 if KeyValuePairs was updated;
          1 if KeyValuePairs was not updated;
          2 if the specified p_key was not found.
*/
</span><span style="color: blue">create procedure </span>up_CompareAndSwap 
    @p_key <span style="color: blue">nvarchar</span><span style="color: gray">(</span>50<span style="color: gray">),
    </span>@p_value <span style="color: blue">int</span><span style="color: gray">,
    </span>@p_comparand <span style="color: blue">int</span><span style="color: gray">
</span><span style="color: blue">as
begin
    begin transaction

    </span><span style="color: green">-- Read current value
    </span><span style="color: blue">declare </span>@currentValue <span style="color: blue">int
    select </span>@currentValue <span style="color: gray">= </span><span style="color: blue">Value
    from </span>KeyValuePairs
    <span style="color: blue">where </span>[Key] <span style="color: gray">= </span>@p_key
    
    <span style="color: blue">if </span><span style="color: gray">(</span><span style="color: magenta">@@ROWCOUNT </span><span style="color: gray">!= </span>1<span style="color: gray">)
    </span><span style="color: blue">begin
        raiserror</span><span style="color: gray">(</span><span style="color: red">'Key &lt;%s&gt; was not found.'</span><span style="color: gray">, </span>16<span style="color: gray">, </span>0<span style="color: gray">, </span>@p_key<span style="color: gray">)
        </span><span style="color: blue">commit transaction
        return </span>2
    <span style="color: blue">end

    </span><span style="color: green">-- Compare to the expected value (and return if they don't match)
    </span><span style="color: blue">if </span><span style="color: gray">(</span>@currentValue <span style="color: gray">!= </span>@p_comparand<span style="color: gray">)
    </span><span style="color: blue">begin
        print </span><span style="color: red">'Comparand does not match. Value was not updated.'
        </span><span style="color: blue">commit transaction
        return </span>1
    <span style="color: blue">end

    </span><span style="color: green">-- Update to the new value
    </span><span style="color: blue">update </span>KeyValuePairs
    <span style="color: blue">set </span>[Value] <span style="color: gray">= </span>@p_value
    <span style="color: blue">where </span>[Key] <span style="color: gray">= </span>@p_key

    <span style="color: blue">print </span><span style="color: red">'Value was updated successfully.'
    </span><span style="color: blue">commit transaction
    return </span>0
<span style="color: blue">end
</span>GO</pre>
<p><strong>So, what’s wrong with this stored procedure?</strong></p>
<p>Specifically, what shortfall does it have when comparing it to the characteristics of CAS operations such as .NET’s <tt>Interlocked.CompareExchange()</tt>?</p>
<p><em>Read on in </em><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/06/12/compareandswap_in_sql_part_2"><em>Compare-and-Swap in SQL: Part 2</em></a><em>.</em></p>]]></description></item><item><title>Invoking batch files from MSBuild</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/21/invoking_batch_files_from_msbuild</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/21/invoking_batch_files_from_msbuild#Comments</comments><pubDate>2009-05-21T21:47:00Z</pubDate><category>bug, .net, obscure, msbuild</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/21/invoking_batch_files_from_msbuild</guid><description><![CDATA[<p>Invoking a batch file from an MSBuild script (such as any *.csproj file) is a snap with the standard <a href="http://msdn.microsoft.com/en-us/library/x8zx72cd.aspx">Exec build task</a>. However, I recently discovered a little caveat with this, and it took a little digging to get to the bottom of it.</p> <p>Consider the following MSBuild project file, which invokes a batch script:</p><pre class="nogutter:nocontrols" name="code">&lt;Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;<br>&nbsp; &lt;Target Name="Default"&gt;<br><strong>&nbsp;&nbsp;&nbsp; &lt;Exec Command="batch.cmd" /&gt;<br></strong>&nbsp; &lt;/Target&gt;<br>&lt;/Project&gt;</pre>If your batch script does anything even moderately complicated, it is reasonable that you would want failure of the batch script to cause failure of the MSBuild project. You can do this using the handy <tt>EXIT /B </tt>command to return from your batch script with an error code:<pre class="nogutter:nocontrols" name="code">@ECHO OFF<br>ECHO Doing stuff<br>ECHO Failed...<br>EXIT /B 42</pre>
<p>The only problem is that the MSBuild script above will merrily announce that the build succeeded, prompting you to scratch your head a little and wonder.</p>
<p>You might, as I did, try it without the <font face="Courier New">/B</font> switch, and you'd see that it works — MSBuild traps the non-zero return and fails the build. And if you're the sort of person that doesn't ask many questions this might suffice. Only you'd be left with a rather rude batch file.</p>
<p>Let's review the documentation for the EXIT command:</p><pre class="nogutter:nocontrols" name="code">C:\&gt;exit /?<br>Quits the CMD.EXE program (command interpreter) or the current batch script.<br> <br>EXIT [/B] [exitCode]</pre><pre>&nbsp; /B&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; specifies to exit the current batch script instead of<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CMD.EXE.&nbsp; If executed from outside a batch script, it<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; will quit CMD.EXE</pre><pre>&nbsp; exitCode&nbsp;&nbsp;&nbsp; specifies a numeric number.&nbsp; if /B is specified, sets<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ERRORLEVEL that number.&nbsp; If quitting CMD.EXE, sets the process<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit code with that number.</pre>
<p>So the <font face="Courier New">/B</font> stands for "Behave". If you call <font face="Courier New">EXIT 1</font> in the middle of your batch you immediately and unconditionally cause the command shell to exit with that error code.</p>
<ul>
<li>
<div>What if you were being called from another batch script? Maybe it wanted to do some cleanup? Or carry on regardless of your failure? Maybe it wanted to log the error? It can't do any of these things because you just pulled the plug on CMD.EXE.</div>
<li>
<div>What if you were being called interactively from a command window, by a user? They are going to see their window disappear without a trace, and with little opportunity to see any error messages that were output to the screen.</div></li></ul>
<p>So we want to be good, and have our script Behave, so we put back that <font face="Courier New">/B</font> switch, and scratch our head some more about why MSBuild is missing this.</p>
<p>Then we get bored of scratching and bust out <a href="http://www.red-gate.com/products/reflector">.NET Reflector</a> to go to town on the Exec task.</p>
<p>So it turns out that internally, Exec doesn't just call your command directly. Instead it wraps it in a batch script of its own! It generates a batch in your temp directory that looks something like this:</p><pre class="nogutter:nocontrols" name="code">setlocal<br>set errorlevel=dummy<br>set errorlevel=<br>batch.cmd<br>exit %errorlevel%</pre>
<p>Then it invokes that with <font face="Courier New">CMD /C </font>and quickly deletes the temporary file. I'm not quite sure why it goes through the trouble of this intermediate batch file, to be honest; what extra value does it add? Wouldn't <font face="Courier New">CMD /C batch.cmd</font> give the same outcome?</p>
<p>One thing that it <em>does </em>add is a little silent caveat. If you invoke a batch file from within another batch file, the first batch file will never resume execution unless you use the <font face="Courier New">CALL</font> command to invoke the second batch.</p>
<p>So what is happening here is that, because we had no idea of this undocumented batch file being created in the background by the Exec task, our command of "batch.cmd" is being plonked in the middle of this generated batch file, and then rudely prevents the generated batch from explicitly calling <font face="Courier New">EXIT</font> to return the error code to MSBuild!</p>
<p>The solution? If you're going to call a batch file with the Exec task, prefix it with a "CALL".</p><pre class="nogutter:nocontrols" name="code">&lt;Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;<br>&nbsp; &lt;Target Name="Default"&gt;<br>&nbsp;&nbsp;&nbsp; &lt;Exec Command="<strong>CALL</strong> batch.cmd" /&gt;<br>&nbsp; &lt;/Target&gt;<br>&lt;/Project&gt;</pre>
<p>This will ensure that the outer script runs to completion, and exits <font face="Courier New">CMD</font> returning the appropriate errorlevel value.</p>
<p>(Alternatively, you could upgrade to a suitabley recent operating system, and forget everything you've just read. Somewhere between Windows XP and Windows 7, <font face="Courier New">CMD.EXE</font> has acquired the elegance of picking up the latest errorlevel value and using that as its return code, so that even if that last <font face="Courier New">EXIT</font> command isn't executed, it will still pick up the value set by the inner batch script.)</p>]]></description></item><item><title>.NET Event handling responsibilities</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/12/net_event_handling_responsibilities</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/12/net_event_handling_responsibilities#Comments</comments><pubDate>2009-05-12T08:46:47Z</pubDate><category>.net, concurrency, compiler</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/12/net_event_handling_responsibilities</guid><description><![CDATA[<p>A day after my post on <a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/04/23/net_event_invocation_thread_safety">.NET Event invocation thread safety</a>, a similar question came up on <a href="http://stackoverflow.com/questions/786383/c-events-and-thread-safety">StackOverflow</a>. About a week after, Eric Lippert came out with <a href="http://blogs.msdn.com/ericlippert/archive/2009/04/29/events-and-races.aspx">this blog post</a> that discusses a related problem that is commonly rolled in to the confusion.</p> <p>Here are my summary learnings:</p> <ul> <li><strong>The problem of calling event handlers after they have been unsubscribed from the event is entirely separate from the null-protection problem.<br></strong>Eric highlights that there are two separate problems here, with separate responsibilities  <ol> <li>The <u>event publisher</u> must ensure they don’t cause a <tt>NullReferenceException</tt> by attempting to invoke an empty delegate chain after the last subscriber has unsubscribed from the event. (See my post on <a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/04/23/net_event_invocation_thread_safety">.NET Event invocation thread safety</a>)  <li>The <u>event subscriber</u> must be able to deal with the fact that a handler may be called shortly <em>after</em> it has been unsubscribed from the event. (See next point.)</li></ol> <li><strong>“Event handlers are required to be robust in the face of being called <u>even after the event has been unsubscribed</u>”</strong><br>Although not explicitly documented anywhere (yet) this conclusion is inevitable for multi-threaded code, particularly in light of the Jon Skeet’s <a href="http://stackoverflow.com/questions/786383/c-events-and-thread-safety/786455#786455">thought experiment</a>: <em>“Now suppose that the invocation list for that delegate has 1000 entries. It's perfectly possible that the action at the start of the list will have executed before another thread unsubscribes a handler near the end of the list. However, that handler will still be executed because it'll be a new list. (Delegates are immutable.) As far as I can see this is unavoidable.”</em>&nbsp; <li><strong>Juval Löwy’s concern that compiler may optimise out the temporary copy of the delegate chain is unfounded<br></strong>I avoided talking about this twist in the post on <a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/04/23/net_event_invocation_thread_safety">.NET Event invocation thread safety</a> as it sounded a little too hypothetical. In his book, after describing the classic “Richter” take-a-copy-and-check-for-null pattern, Juval&nbsp; postulates that the compiler may optimise away the temporary copy for you, and undermine the pattern. Jon Skeet <a href="http://stackoverflow.com/questions/786383/c-events-and-thread-safety/786455#786455">states</a> that <em>“The JIT isn't allowed to perform the optimization you're talking about in the first part, because of the condition. I know this was raised as a spectre a while ago, but it's not valid. (I checked it with either Joe Duffy or Vance Morrison a while ago; I can't remember which.)” </em>so that’s a comfort.</li></ul> <p>The main thrust of the <a href="http://stackoverflow.com/questions/786383/c-events-and-thread-safety">StackOverflow question</a> is now:</p> <blockquote> <p><em><strong>Why is explicit-null-check the "standard pattern"? </strong>The alternative, assigning the empty delegate, requires only <code>= delegate {}</code> to be added to the event declaration, and this eliminates those little piles of stinky ceremony from every place where the event is raised. It would be easy to make sure that the empty delegate is cheap to instantiate. Or am I still missing something?</em></p></blockquote> <p>I still have some reservations about this, and suspect that the likes of Joe Duffy might have something to say about the performance impact of this. Although my <a href="http://stackoverflow.com/questions/786383/c-events-and-thread-safety/846919#846919">performance analysis</a> certainly suggests that the invocation-time overhead of this is, in real terms, negligible, I haven’t done any testing on the initialisation-time overheads. Still, the impact of this is likely to be negligible, and as Earwicker points out <em>“Why let the ugly way be the recommended way? If we wanted premature optimisation instead of clarity, we'd be using assembler”</em></p> <p>So, should we all be replacing the copy-and-null-check pattern with the initialise-with-empty-delegate pattern?</p>]]></description></item><item><title>Sharing Is The Root Of All Contention</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/04/sharing_is_the_root_of_all_contention</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/04/sharing_is_the_root_of_all_contention#Comments</comments><pubDate>2009-05-04T11:20:00Z</pubDate><category>concurrency</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/04/sharing_is_the_root_of_all_contention</guid><description><![CDATA[<p>There is an increasing focus on software parallelisation and scalability through multi-threaded programming these days. This is most clearly evident through the number of articles on these subjects popping up in blogs, magazines, and journals.</p><p>One such article I recently read was in Dr. Dobb's, written&nbsp;by Herb Sutter, and&nbsp;entitled&nbsp;<span class="Apple-style-span" style="font-style: italic">Sharing Is The Root Of All Contention</span>. Herb talks about the obvious concurrency impediments that are directly visible in code, but he also talks in-depth about the more elusive concurrency impediments that are <span class="Apple-style-span" style="font-style: italic">hidden</span>&nbsp;in the software layers and underlying hardware.</p><p>Here is Herb's fantastic breakdown matrix of &quot;contention penalties&quot; which summarises the main thrust of his article quite well&nbsp;(I'm not sure why it was included as a blurry image rather than a table, but it's like that in the original article):</p><div style="text-align: center"><img src="http://www.danielfortunov.com/software/~Photo?id=fa3d5213-54fb-4d78-8cba-40c5aeb73bd5&amp;width=0&amp;height=0" alt="Contention penalties" border="0" hspace="8" vspace="8" /></div><p>&nbsp;</p><p>Check out the full article for some great detail on each of these areas:&nbsp;<span class="Apple-style-span" style="font-weight: bold"><a href="http://www.ddj.com/architect/214100002">Sharing Is The Root Of All Contention, by Herb Sutter</a></span></p>]]></description></item><item><title>Thoughts on software security</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/01/thoughts_on_software_security</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/01/thoughts_on_software_security#Comments</comments><pubDate>2009-05-01T08:36:22Z</pubDate><category>security</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/05/01/thoughts_on_software_security</guid><description><![CDATA[<p><a href="http://webjam-upload.s3.amazonaws.com/e4d26c1a-c9b5-45c8-bf53-ff746ea7b0cf_WindowsLiveWriterSecurityvulnerabilitiesofconcurrentpipel_745Fsecurity-feel-better-718262%5B1%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="security-feel-better-718262[1]" border="0" alt="security-feel-better-718262[1]" align="right" src="http://webjam-upload.s3.amazonaws.com/6886a22e-a225-41f7-a552-51b4edfd938d_WindowsLiveWriterSecurityvulnerabilitiesofconcurrentpipel_745Fsecurity-feel-better-718262%5B1%5D_thumb.png" width="136" height="244"></a> Security is a thorny issue that can often come back to bite you. The main problem is that people don’t inherently think about security until it becomes a problem (unless they happen to be sitting across from <a href="http://www.schneier.com">Bruce</a> <a href="http://www.schneier.com/blog/">Schneier</a> and staring him in the eyes — in which case they already have a <a href="http://geekz.co.uk/schneierfacts/fact/50">bigger problem</a>). It became a big problem for Microsoft, so now they do <a href="http://msdn.microsoft.com/en-us/security/aa570411.aspx">threat</a> <a href="http://blogs.msdn.com/larryosterman/search.aspx?q=Threat+Model&amp;p=1">modelling</a> as part of the design for every new product feature.</p> <p>A few years ago I was presenting a paper at the annual <a href="http://embs.gsbme.unsw.edu.au/">IEEE Engineering in Medicine and Biology Society</a> conference in New York, and I attended a presentation about a pacemaker device which was remotely re-programmable. I think the idea was to allow tuning and refinements without invasive surgery.<strong> There was no mention of security during the presentation.</strong> We’re talking about a remotely programmable pacemaker here… I questioned security at the end but the presenter did not seem concerned. I guess it hadn’t been a problem yet. (And I’m not sure if medical devices are subject to threat modelling, yet.)</p> <p>Security is a big issue on the internet because the interconnectedness that provides much of the value of the internet is also its Achilles’ heel in terms of <a href="http://en.wikipedia.org/wiki/Conflicker">large-scale automated attack and exploitation</a>. In areas such as internal software development, it’s not such a big consideration because the software is deemed to be isolated enough from the internet by firewalls and other protection (more on insider attacks another day).</p> <p>The problem is that security costs money, and requires expertise. It is a burden that you must carry through any refactoring, optimisations, or other changes to the software. Until lack of security becomes a problem, it is hard to trade off a hypothetical threat that most people don’t think about, against the potential goodness of not having to worry about security (be it faster completion, more features, smaller budget, etc.).</p> <p>By the time security becomes a problem, it’s often too late. (<a title="TV-B-Gone is a universal remote control to turn off any TV in sight" href="http://www.tvbgone.com/">Pacemaker-B-Gone</a>, anyone?)&nbsp; Introducing security late in the software development life-cycle is an uphill battle when it hasn’t been designed-in from the ground up. Introducing security to a pacemaker that has already been “installed” might be even more of a challenge.</p>]]></description></item><item><title>.NET Event invocation thread safety</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/04/23/net_event_invocation_thread_safety</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/04/23/net_event_invocation_thread_safety#Comments</comments><pubDate>2009-04-23T19:55:44Z</pubDate><category>.net, concurrency</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/04/23/net_event_invocation_thread_safety</guid><description><![CDATA[<h2>The Problem Statement</h2> <p>In <em><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/02/03/book_review_clr_via_c_2nd_edition">CLR Via C#</a></em> Richter points out a few subtle points about event invocation in multi-threaded classes:</p> <ul> <li>A delegate chain is immutable; a new chain is created to replace the first.  <li>A delegate chain with zero subscribers is <strong>null</strong>.  <li>That means (if your event is public) it may transition from null to non-null and vice versa, at any time.</li></ul> <p><strong><u>The Naive Approach: Not thread safe</u></strong></p> <p>So consider this invocation code which raises an event:</p><pre class="c#:nogutter:nocontrols" name="code">public static event EventHandler&lt;EventArgs&gt; NonThreadSafeEvent;<br><br>public static void OnNonThreadSafeEvent(EventArgs e)<br>{<br>    if (NonThreadSafeEvent != null)<br>    {<br>        // Event could still become null in this interim,<br>        // after the check but before the invocation<br>        NonThreadSafeEvent(null, e);<br>    }<br>}<br></pre>
<p>On a class that is being accessed by multiple threads, this could lead to a <tt>NullReferenceException</tt>, despite the well-intentioned null guard.</p>
<h2>The Solution Space</h2>
<p>There are a number of ways to overcome this problem and guarantee that multi-threaded classes will never be exposed to the risk of a <font face="Courier New">NullReferenceException</font> when attempting to invoke a delegate or event.</p>
<p><strong><u>The Classic Solution: Take a copy</u></strong></p>
<p>This is the solution that Richter proposes to achieve thread safety:</p><pre class="c#:nogutter:nocontrols" name="code">public static event EventHandler&lt;EventArgs&gt; ClassicNullCheckedEvent;<br><br>public static void OnClassicNullCheckedEvent(EventArgs e)<br>{<br>    EventHandler&lt;EventArgs&gt; localCopy = ClassicNullCheckedEvent;<br>    if (localCopy != null)<br>    {<br>        // Nobody can change our local copy so we're sure it's not null<br>        localCopy(null, e);<br>    }<br>}<br></pre>
<p><strong><u>New-Age Solution: Pre-Initialise</u></strong></p>
<p>I like to call this the Juval Löwy solution, because he proposes it in one of his books:</p>
<blockquote>
<p><em>“You can ensure that the internal invocation list always has at least one member by initializing it with a do-nothing anonymous method. Because no external party can have a reference to the anonymous method, no external party can remove the method, so the delegate will never be <tt>null</tt>”<br>&nbsp;&nbsp;&nbsp; </em>— Programming .NET Components, 2nd Edition, by Juval Löwy</p></blockquote><pre class="c#:nogutter:nocontrols" name="code">public static event EventHandler&lt;EventArgs&gt; PreInitializedEvent = delegate { };<br><br>public static void OnPreInitializedEvent(EventArgs e)<br>{<br>    // No check required - event will never be null because<br>    // we have subscribed an empty anonymous delegate which<br>    // can never be unsubscribed. (But causes some overhead.)<br>    PreInitializedEvent(null, e);<br>}<br></pre>
<p>Of course then he immediately says that “<em>initializing all delegates this way is impractical</em>” yet without explaining <em>why</em> it is impractical. Seems fine to me! Certainly more practical than remembering to copy-and-check-for-null every time you want to raise an event.</p>
<h2>Performance Implications</h2>
<p>As always, there are some subtle performance implications to each approach (particularly the last one!)</p>
<p><pre>Executing 50000000 iterations . . .<br>OnNonThreadSafeEvent took:      432ms
OnClassicNullCheckedEvent took: 490ms
OnPreInitializedEvent took:     614ms
Subscribing an empty delegate to each event . . .
Executing 50000000 iterations . . .
OnNonThreadSafeEvent took:      674ms
OnClassicNullCheckedEvent took: 674ms
OnPreInitializedEvent took:     2041ms
Subscribing another empty delegate to each event . . .
Executing 50000000 iterations . . .
OnNonThreadSafeEvent took:      2011ms
OnClassicNullCheckedEvent took: 2061ms
OnPreInitializedEvent took:     2246ms
Done</pre>
<p></p>
<p>Though you probably needn’t worry about these until your performance testing turns up a bottleneck on event invocation. i.e. probably never. (Note that the test run is for 50 million iterations.)</p>
<p>(Code samples from this post are available as a <a href="http://webjam-upload.s3.amazonaws.com/threadsafeeventinvocation___b70502ad0d1344d9877e1e4bdc43b3d4__3__.rar">VS2008 Solution</a>.)</p>]]></description></item><item><title>Explicit co-variance and contra-variance for delegate and interface type parameters in C# 4.0</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/04/19/explicit_covariance_and_contravariance_for_delegate_and_interface_type_parameters_in_c_40</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/04/19/explicit_covariance_and_contravariance_for_delegate_and_interface_type_parameters_in_c_40#Comments</comments><pubDate>2009-04-19T11:52:00Z</pubDate><category>.net, variance, c#4.0</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/04/19/explicit_covariance_and_contravariance_for_delegate_and_interface_type_parameters_in_c_40</guid><description><![CDATA[<p><span class="Apple-style-span" style="color: #333333; line-height: 14px">Last week Bart De Smet posted an excellent explanation of co-variance and contra-variance support in the .NET CLR and the new support for this in C# 4.0&nbsp;that can make our lives easier.</span></p><p>This&nbsp;is by far the most comprehensive and clear explanation of the rather in-depth field of type variance, and features lots of examples (and diagrams!) to help your understanding.</p><p>My favourite part of the article was this awesome fruit farmer metaphor:</p><blockquote class="webkit-indent-blockquote" style="border-style: none; margin: 0px 0px 0px 40px; padding: 0px">&nbsp;&quot;<span class="Apple-style-span" style="color: #333333; font-family: Verdana; font-size: 12px; line-height: 14px">This might go unnoticed if the farmer doesn&rsquo;t enforce&nbsp;<i>runtime fruit/vegetable type safety</i>.&quot;<br /></span>&nbsp;&nbsp; &mdash; Bart De Smet&nbsp;</blockquote><p>Read the full article here:</p><p><a href="http://community.bartdesmet.net/blogs/bart/archive/2009/04/13/c-4-0-feature-focus-part-4-generic-co-and-contra-variance-for-delegate-and-interface-types.aspx"><span class="Apple-style-span" style="color: #0000cc; font-family: Verdana; font-size: 12px; font-weight: bold; line-height: 14px; text-transform: uppercase">C# 4.0 FEATURE FOCUS &ndash; PART 4 &ndash; CO- AND CONTRA-VARIANCE FOR GENERIC DELEGATE AND INTERFACE TYPES</span></a>&nbsp;</p>]]></description></item><item><title>Assembly Versioning in .NET</title><link>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/03/03/assembly_versioning_in_net</link><comments>http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/03/03/assembly_versioning_in_net#Comments</comments><pubDate>2009-03-03T06:07:00Z</pubDate><category>design, .net, "release management"</category><guid isPermaLink="true">http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/03/03/assembly_versioning_in_net</guid><description><![CDATA[<p>Versioning of assemblies in .NET can be a confusing prospect given that there are currently at least three ways to specify a version for your assembly.</p> <p>Here are the three main version-related assembly attributes:</p><pre class="c#:nogutter:nocontrols" name="code">// Assembly mscorlib, Version 2.0.0.0<br>[assembly: AssemblyFileVersion("2.0.50727.3521")]<br>[assembly: AssemblyInformationalVersion("2.0.50727.3521")]<br>[assembly: AssemblyVersion("2.0.0.0")]<br></pre>
<p>By convention, the four parts of the version are referred to as the <em><strong>Major Version</strong></em>, <em><strong>Minor Version</strong></em>, <em><strong>Build</strong></em>, and <em><strong>Revision</strong></em>.</p>
<h2>The <tt>AssemblyFileVersion</tt> is intended to uniquely identify a build of the <em>individual assembly</em></h2>
<p>Typically you’ll manually set the <em>Major</em> and <em>Minor</em> <font face="Courier New">AssemblyFileVersion</font> to reflect the version of the assembly, then increment the <em>Build</em> and/or <em>Revision</em> every time your build system compiles the assembly. The <font face="Courier New">AssemblyFileVersion</font> should allow you to uniquely identify a build of the assembly, so that you can use it as a starting point for debugging any problems. </p>
<p>On my current project we have the build server encode the changelist number from our source control repository into the <em>Build</em> and <em>Revision</em> parts of the <tt>AssemblyFileVersion</tt>. This allows us to map directly from an assembly to its source code, for <em>any</em> assembly generated by the build server (without having to use labels or branches in source control, or manually keeping any records of released versions). </p>
<p>This version number is stored in the Win32 version resource and can be seen when viewing the Windows Explorer property pages for the assembly. </p>
<p><em>The CLR does not care about nor examine the </em><font face="Courier New">AssemblyFileVersion</font><em>.</em> </p>
<h2><font face="Arial">The </font><tt>AssemblyInformationalVersion</tt><font face="Arial"> is intended to represent the version of your <em>entire product</em></font></h2>
<p>The <font face="Courier New">AssemblyInformationalVersion</font> is intended to allow coherent versioning of the entire <em>product,</em> which may consist of many assemblies that are independently versioned, perhaps with differing versioning policies, and potentially developed by disparate teams.</p>
<blockquote>
<p>“For example, version 2.0 of a product might contain several assemblies; one of these assemblies is marked as version 1.0 since it’s a new assembly that didn’t ship in version 1.0 of the same product. Typically, you set the major and minor parts of this version number to represent the public version of your product. Then you increment the build and revision parts each time you package a complete product with all its assemblies.”<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; — Jeffrey Richter, <em><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/02/03/book_review_clr_via_c_2nd_edition">CLR via C# (Second Edition)</a></em> p. 57</p></blockquote>
<p><em>The CLR does not care about nor examine the </em><font face="Courier New">AssemblyInformationalVersion</font><em>.</em> </p>
<h2>The <tt>AssemblyVersion</tt> is the only version the CLR cares about (but it cares about the <em>entire</em> <tt>AssemblyVersion</tt>)</h2>
<p>The <tt>AssemblyVersion</tt> is used by the CLR to bind to strongly named assemblies. It is stored in the AssemblyDef manifest metadata table of the built assembly, and in the AssemblyRef table of any assembly that references it.</p>
<p>This is very important, because it means that when you reference a strongly named assembly, you are tightly bound to a specific <font face="Courier New">AssemblyVersion</font> of that assembly. The entire <font face="Courier New">AssemblyVersion</font> must be an <em>exact</em> match for the binding to succeed. For example, if you reference version 1.0.0.0 of a strongly named assembly at build-time, but only version 1.0.0.1 of that assembly is available at runtime, binding will fail! (You will then have to work around this using <a href="http://msdn.microsoft.com/en-us/library/2fc472t2.aspx">Assembly Binding Redirection</a>.)</p>
<h2>Confusion over whether the <em>entire</em> <tt>AssemblyVersion</tt> has to match. (Yes, it does.)</h2>
<p>There is a little confusion around whether the <em>entire </em><font face="Courier New">AssemblyVersion</font> has to be an exact match in order for an assembly to be loaded. Some people are under the false belief that only the <em>Major</em> and <em>Minor </em>parts of the <font face="Courier New">AssemblyVersion</font> have to match in order for binding to succeed. This is a sensible assumption, however it is ultimately incorrect (as of .NET 3.5), and it’s trivial to verify this for your version of the CLR. Just execute <a href="http://webjam-upload.s3.amazonaws.com/assemblybinding___fcbd12afca1d4bb3bf94bf88d1616d25__100__.rar">this sample code</a>.</p>
<p>On my machine the second assembly load fails, and the last two lines of the fusion log make it perfectly clear why:</p>
<blockquote><pre>.NET Framework Version: 2.0.50727.3521<br>---<br>Attempting to load assembly: Rhino.Mocks, Version=3.5.0.<strong><u><font color="#ff0000">1337</font></u></strong>, Culture=neutral, PublicKeyToken=0b3305902db7183f<br>Successfully loaded assembly: Rhino.Mocks, Version=3.5.0.<strong><u><font color="#ff0000">1337</font></u></strong>, Culture=neutral, PublicKeyToken=0b3305902db7183f<br>---<br>Attempting to load assembly: Rhino.Mocks, Version=3.5.0.<strong><u><font color="#ff0000">1336</font></u></strong>, Culture=neutral, PublicKeyToken=0b3305902db7183f<br>Assembly binding for <rhino.mocks , PublicKeyToken="0b3305902db7183f" Culture="neutral," Version="3.5.0.1336,"></RHINO.MOCKS,> failed:<br>System.IO.FileLoadException: Could not load file or assembly 'Rhino.Mocks, Version=3.5.0.1336, Culture=neutral, <br>PublicKeyToken=0b3305902db7183f' or one of its dependencies. <u><font color="#ff0000"><strong>The located assembly's manifest definition <br>does not match the assembly reference.</strong></font></u> (Exception from HRESULT: 0x80131040)<br>File name: 'Rhino.Mocks, Version=3.5.0.1336, Culture=neutral, PublicKeyToken=0b3305902db7183f'<br><br>=== Pre-bind state information ===<br>LOG: User = Phoenix\Dani<br>LOG: DisplayName = Rhino.Mocks, Version=3.5.0.1336, Culture=neutral, PublicKeyToken=0b3305902db7183f<br> (Fully-specified)<br>LOG: Appbase = [...]<br>LOG: Initial PrivatePath = NULL<br>Calling assembly : AssemblyBinding, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.<br>===<br>LOG: This bind starts in default load context.<br>LOG: No application configuration file found.<br>LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config.<br>LOG: Post-policy reference: Rhino.Mocks, Version=3.5.0.1336, Culture=neutral, PublicKeyToken=0b3305902db7183f<br>LOG: Attempting download of new URL [...].<br><strong><u><font color="#ff0000">WRN: Comparing the assembly name resulted in the mismatch: Revision Number</font></u></strong><br>ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.</pre></blockquote>
<p>I think the source of this confusion is probably because Microsoft originally intended to be a little more lenient on this strict matching of the full <font face="Courier New">AssemblyVersion</font>, by matching only on the <em>Major</em> and <em>Minor</em> version parts:</p>
<blockquote>
<p>“When loading an assembly, the CLR will automatically find the latest installed servicing version that matches the major/minor version of the assembly being requested.”<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; — Jeffrey Richter, <em><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/02/03/book_review_clr_via_c_2nd_edition">CLR via C# (Second Edition)</a></em> p. 56</p></blockquote>
<p>This was the behaviour in Beta 1 of the 1.0 CLR, however this feature was removed before the 1.0 release, and hasn’t managed to re-surface in .NET 2.0:</p>
<blockquote>
<p>“Note: I have just described how you should think of version numbers. Unfortunately, the CLR doesn’t treat version numbers this way. [In .NET 2.0], the CLR treats a version number as an opaque value, and if an assembly depends on version 1.2.3.4 of another assembly, the CLR tries to load version 1.2.3.4 only (unless a binding redirection is in place). However, <strong>Microsoft has plans to change the CLR’s loader in a future version so that it loads the latest build/revision for a given major/minor version of an assembly.</strong> For example, on a future version of the CLR, if the loader is trying to find version 1.2.3.4 of an assembly and version 1.2.5.0 exists, the loader with automatically pick up the latest servicing version. This will be a very welcome change to the CLR’s loader — I for one can’t wait.”<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; — Jeffrey Richter, <em><a href="http://www.danielfortunov.com/software/$daniel_fortunovs_adventures_in_software_development/2009/02/03/book_review_clr_via_c_2nd_edition">CLR via C# (Second Edition)</a></em> p. 164 (Emphasis mine)</p></blockquote>
<p>As this change still hasn’t been implemented, I think it’s safe to assume that Microsoft had back-tracked on this intent, and it is perhaps too late to change this now. I tried to search around the web to find out what happened with these plans, but I couldn’t find any answers. I still wanted to get to the bottom of it.</p>
<p>So I emailed Jeff Richter and asked him directly — I figured if anyone knew what happened, it would be him.</p>
<p>He replied&nbsp; within 12 hours, on a Saturday morning no less, and clarified that the .NET 1.0 Beta 1 loader did implement this ‘automatic roll-forward’ mechanism of picking up the latest available <em>Build </em>and <em>Revision </em>of an assembly, but this behaviour was reverted before .NET 1.0 shipped. It was later intended to revive this but it didn’t make it in before the CLR 2.0 shipped. Then came Silverlight, which took priority for the CLR team, so this functionality got delayed further. In the meantime, most of the people who were around in the days of CLR 1.0 Beta 1 have since moved on, so it’s unlikely that this will see the light of day, despite all the hard work that had already been put into it.</p>
<p>The current behaviour, it seems, is here to stay.</p>
<p>It is also worth noting from my discussion with Jeff that <font face="Courier New">AssemblyFileVersion</font> was only added after the removal of the ‘automatic roll-forward’ mechanism — because after 1.0 Beta 1, any change to the <font face="Courier New">AssemblyVersion</font> was a breaking change for your customers, there was then nowhere to safely store your build number. <font face="Courier New">AssemblyFileVersion</font> is that safe haven, since it’s never automatically examined by the CLR. Maybe it’s clearer that way, having two separate version numbers, with separate meanings, rather than trying to make that separation between the <em>Major/Minor</em> (breaking) and the <em>Build/Revision</em> (non-breaking) parts of the <font face="Courier New">AssemblyVersion</font>.</p>
<h2>The bottom line: Think carefully about when you change your <font face="Courier New">AssemblyVersion</font></h2>
<p>The moral is that if you’re shipping assemblies that other developers are going to be referencing, you need to be extremely careful about when you do (and don’t) change the <font face="Courier New">AssemblyVersion</font> of those assemblies. Any changes to the <font face="Courier New">AssemblyVersion</font> will mean that application developers will either have to re-compile against the new version (to update those AssemblyRef entries) or use assembly binding redirects to manually override the binding.</p>
<ul>
<li><strong>Do not</strong> change the <font face="Courier New">AssemblyVersion</font> for a servicing release which is intended to be backwards compatible. 
<li><strong>Do</strong> change the <font face="Courier New">AssemblyVersion</font> for a release that you know has breaking changes.</li></ul>
<p>Just take another look at the version attributes on mscorlib:</p><pre class="c#:nogutter:nocontrols" name="code">// Assembly mscorlib, Version 2.0.0.0<br>[assembly: AssemblyFileVersion("2.0.50727.3521")]<br>[assembly: AssemblyInformationalVersion("2.0.50727.3521")]<br>[assembly: AssemblyVersion("2.0.0.0")]<br></pre>
<p>Note that it’s the <font face="Courier New">AssemblyFileVersion</font> that contains all the interesting servicing information (it’s the <em>Revision</em> part of this version that tells you what&nbsp; Service Pack you’re on), meanwhile the <font face="Courier New">AssemblyVersion</font> is fixed at a boring old 2.0.0.0. Any change to the <font face="Courier New">AssemblyVersion</font> would force every .NET application referencing mscorlib.dll to re-compile against the new version!</p>]]></description></item></channel></rss>