<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Haugerns Development Escapades &#187; mystery code</title>
	<atom:link href="http://www.haugern.net/blog/category/mystery-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.haugern.net/blog</link>
	<description>Morten Haug on development and other curiosities</description>
	<lastBuildDate>Sun, 31 May 2009 21:56:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mystery Code I &#8211; Demystified</title>
		<link>http://www.haugern.net/blog/mystery-code-i-demystified/</link>
		<comments>http://www.haugern.net/blog/mystery-code-i-demystified/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 23:10:51 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
				<category><![CDATA[mystery code]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/mystery-code-i-demystified/</guid>
		<description><![CDATA[Either no one reads my blog or I didn’t present the problem good enough. The first is probably the safest bet. But here’s the answer anyway.
I made a small hint about it was run on a terminal (meaning I’m logged in as a user on a Windows Server with Terminal Services running (just to be [...]]]></description>
			<content:encoded><![CDATA[<p>Either no one reads my blog or I didn’t present the problem good enough. The first is probably the safest bet. But here’s the answer anyway.</p>
<p>I made a small hint about it was run on a terminal (meaning I’m logged in as a user on a Windows Server with Terminal Services running (just to be clear)). </p>
<h3>The Smells</h3>
<p>So lets look at what possible smells we can find in the code:</p>
<p><a href="http://www.haugern.net/blog/wp-content/image2.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="104" alt="image" src="http://www.haugern.net/blog/wp-content/image-thumb2.png" width="473" border="0" /></a> </p>
<p>That application specific GUID sure seems like it can be trouble.</p>
<p><a href="http://www.haugern.net/blog/wp-content/image3.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="104" alt="image" src="http://www.haugern.net/blog/wp-content/image-thumb3.png" width="456" border="0" /></a> </p>
</p>
<p>Hmm, the GUID is now used as the name for this Mutex. And now the last piece of the puzzle:</p>
<p><a href="http://www.haugern.net/blog/wp-content/image4.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="90" alt="image" src="http://www.haugern.net/blog/wp-content/image-thumb4.png" width="623" border="0" /></a> </p>
<p>Ok, this sure works when I’m sitting alone on my computer with my application, but what happens when multiple users on a terminal server tries to start the same application? </p>
<h3>The Problem</h3>
<p>Multiple <a href="http://msdn.microsoft.com/en-us/library/system.io.pipes.namedpipeserverstream.aspx" target="_blank">NamedPipeServerStreams</a> with the same identifier will be tried made, and then you will get: <em>“IOException:&#160; All pipe instances are busy.”</em> But shouldn’t it stop at the Mutex before it tries to make the server? After all, the named Mutex is using the same GUID as identifier as well.</p>
<p><a href="http://www.haugern.net/blog/wp-content/image5.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="191" alt="image" src="http://www.haugern.net/blog/wp-content/image-thumb5.png" width="604" border="0" /></a> </p>
<p>It seems that the assumption that the Mutex and the named pipe operated on the same system level are flat out wrong. They don’t!<strong> </strong></p>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx" target="_blank">Mutexes defaults to session locality when run on terminal services and named pipes does not!</a></strong></p>
<p>You can prefix your mutex identifier with Global\ to ensure a system wide lock even on a terminal services system, but this isn’t really what we want in this situation. We would really like all users to get a single instance each.</p>
<h3>The Solution</h3>
<p>With the problem identified, the solution was easy. We just incorporated the logged in username into the identifier, and everything was humming along.</p>
<p><em>Until next mystery, go solve some yourselves.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/mystery-code-i-demystified/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mystery Code I</title>
		<link>http://www.haugern.net/blog/mystery-code-i/</link>
		<comments>http://www.haugern.net/blog/mystery-code-i/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 20:50:23 +0000</pubDate>
		<dc:creator>haugern</dc:creator>
				<category><![CDATA[mystery code]]></category>

		<guid isPermaLink="false">http://www.haugern.net/blog/mystery-code-i/</guid>
		<description><![CDATA[There are a lot of ways to make sure your .NET Windows application just has one instance running, and we picked the one described here. It was working flawlessly until someone tried running it on a terminal where it crashed mysteriously.
What happened? Can you spot the error?
]]></description>
			<content:encoded><![CDATA[<p>There are a lot of ways to make sure your .NET Windows application just has one instance running, and we picked the <a href="http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx" target="_blank">one described here</a>. It was working flawlessly until someone tried running it on a terminal where it crashed mysteriously.</p>
<p>What happened? Can you spot the error?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haugern.net/blog/mystery-code-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
