﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com/blog/</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2012 (c) 2013</copyright><ttl>60</ttl><item><title>This is debugging, old school</title><description>&lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/This-is-debugging-old-school_982/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/This-is-debugging-old-school_982/image_thumb.png" width="772" height="265"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Dear god in heaven, how much I did not miss that.&lt;/p&gt;</description><link>http://ayende.com/blog/161762/this-is-debugging-old-school?key=7a5c7af6-1e66-448e-9d77-832bd1859ca3</link><guid>http://ayende.com/blog/161762/this-is-debugging-old-school?key=7a5c7af6-1e66-448e-9d77-832bd1859ca3</guid><pubDate>Wed, 01 May 2013 09:00:00 GMT</pubDate></item><item><title>Bug hunting in a massively multi threaded environment</title><description>&lt;p&gt;&lt;img style="display: inline; float: right" align="right" src="http://studio3music.com/wp-content/uploads/2009/12/bug-hunting.jpg" width="88" height="112"&gt;&lt;/p&gt; &lt;p&gt;We got a really nasty bug report from a user. Sometimes, out of the blue, RavenDB would throw an error:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;System.InvalidOperationException: Collection was modified; enumeration operation may not execute.&lt;br&gt;   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()&lt;br&gt;   at Raven.Json.Linq.RavenJArray.WriteTo(JsonWriter writer, JsonConverter[] converters) &lt;span class="kwrd"&gt;in&lt;/span&gt; c:\Builds\RavenDB-Stable\Raven.Abstractions\Json\Linq\RavenJArray.cs:line 174&lt;br&gt;   at Raven.Json.Linq.RavenJObject.WriteTo(JsonWriter writer, JsonConverter[] converters) &lt;span class="kwrd"&gt;in&lt;/span&gt; c:\Builds\RavenDB-Stable\Raven.Abstractions\Json\Linq\RavenJObject.cs:line 275
   at Raven.Json.Linq.RavenJObject.WriteTo(JsonWriter writer, JsonConverter[] converters) &lt;span class="kwrd"&gt;in&lt;/span&gt; c:\Builds\RavenDB-Stable\Raven.Abstractions\Json\Linq\RavenJObject.cs:line 275&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;Unfortunately, this error only happened once in a while, usually when the system was under load. But they weren’t able to provide a repro for that.&lt;/p&gt;
&lt;p&gt;Luckily, they &lt;em&gt;were&lt;/em&gt; able to tell us that they suspected that this is related to the replication support. I quickly setup a database with replication and wrote the following code to try to reproduce this:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt;(var store = &lt;span class="kwrd"&gt;new&lt;/span&gt; DocumentStore
{
    Url = &lt;span class="str"&gt;"http://localhost:8080"&lt;/span&gt;,
    DefaultDatabase = &lt;span class="str"&gt;"hello"&lt;/span&gt;
}.Initialize())
{
    &lt;span class="kwrd"&gt;using&lt;/span&gt;(var session = store.OpenSession())
    {
        session.Store(&lt;span class="kwrd"&gt;new&lt;/span&gt; ReadingList
        {
            UserId = &lt;span class="str"&gt;"test/1"&lt;/span&gt;,
            Id = &lt;span class="str"&gt;"lists/1"&lt;/span&gt;,
            Books = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;ReadingList.ReadBook&amp;gt;()
        });
        session.SaveChanges();
    }
    Parallel.For(0, 100, i =&amp;gt;
    {
        &lt;span class="kwrd"&gt;while&lt;/span&gt; (&lt;span class="kwrd"&gt;true&lt;/span&gt;)
        {
            &lt;span class="kwrd"&gt;try&lt;/span&gt;
            {
                &lt;span class="kwrd"&gt;using&lt;/span&gt; (var session = store.OpenSession())
                {
                    session.Advanced.UseOptimisticConcurrency = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
                    session.Load&amp;lt;ReadingList&amp;gt;(&lt;span class="str"&gt;"lists/1"&lt;/span&gt;)
                            .Books.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; ReadingList.ReadBook
                            {
                                ReadAt = DateTime.Now,
                                Title = &lt;span class="str"&gt;"test "&lt;/span&gt; + i
                            });
                    session.SaveChanges();
                }
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            }
            &lt;span class="kwrd"&gt;catch&lt;/span&gt; (ConcurrencyException)
            {
                
            }
        }
    });
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;And that reproduced the bug! Hurrah! Done deal, we can move on, right?&lt;/p&gt;
&lt;p&gt;Except that the bug was only there when we have &lt;em&gt;massive&lt;/em&gt; amount of threads hitting the db at once, and trying to figure out what is actually going on there was next to impossible using standard debugging tools. Instead, I reached down to my tracing toolbelt and starting pulling stuff out. First, we identified that the problem occurred when iterating over RavenJArray, which is our own object, so we added the following:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;        ConcurrentQueue&amp;lt;StackTrace&amp;gt;  addStackTraces = &lt;span class="kwrd"&gt;new&lt;/span&gt; ConcurrentQueue&amp;lt;StackTrace&amp;gt;();

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Add(RavenJToken token)
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (isSnapshot)
                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; InvalidOperationException(&lt;span class="str"&gt;"Cannot modify a snapshot, this is probably a bug"&lt;/span&gt;);

            addStackTraces.Enqueue(&lt;span class="kwrd"&gt;new&lt;/span&gt; StackTrace(&lt;span class="kwrd"&gt;true&lt;/span&gt;));

            Items.Add(token);
        }
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;And this one (where the exception is raised):&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; WriteTo(JsonWriter writer, &lt;span class="kwrd"&gt;params&lt;/span&gt; JsonConverter[] converters)
{
    writer.WriteStartArray();

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (Items != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
    {
        &lt;span class="kwrd"&gt;try&lt;/span&gt;
        {
            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var token &lt;span class="kwrd"&gt;in&lt;/span&gt; Items)
            {
                token.WriteTo(writer, converters);
            }
        }
        &lt;span class="kwrd"&gt;catch&lt;/span&gt; (InvalidOperationException e)
        {
            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var stackTrace &lt;span class="kwrd"&gt;in&lt;/span&gt; addStackTraces)
            {
                Console.WriteLine(stackTrace);
            }
            &lt;span class="kwrd"&gt;throw&lt;/span&gt;;
        }
    }

    writer.WriteEndArray();
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;With the idea that we would actually be able to &lt;em&gt;get&lt;/em&gt; what is going on there. By tracking down who added items to this particular instance, I hoped that I would be able to figure out why we have an instance that is shared among multiple threads.&lt;/p&gt;
&lt;p&gt;When I had &lt;em&gt;that&lt;/em&gt;, it was pretty easy to see that it was indeed the replication bundle that was causing the issue. The problem was that the replication bundle was modifying an inner array inside the document metadata. We protected the root properties of the metadata from concurrent modifications, and most of the time, it works just fine. But the problem was that now we had a bundle that was modifying a &lt;em&gt;nested&lt;/em&gt; array, which &lt;em&gt;wasn’t&lt;/em&gt; protected.&lt;/p&gt;
&lt;p&gt;This is one of those bugs that are really hard to catch:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;My understanding of the code said that this is not possible, since I believed that we protected the nested properties as well*.&lt;/li&gt;
&lt;li&gt;This bug will only surface if and only if:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;You have the replication bundle enabled.&lt;/li&gt;
&lt;li&gt;You have a great deal of concurrent modifications (with optimistic concurrency enabled) to the same document.&lt;/li&gt;
&lt;li&gt;You are unlucky.&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;
&lt;p&gt;I was grateful that the user figured out the replication connection, because I already sat on that bug previously, and there was no way I could figure out what is going on unless I had the trace to point me to where the actual problem was.&lt;/p&gt;</description><link>http://ayende.com/blog/160903/bug-hunting-in-a-massively-multi-threaded-environment?key=64348966-76a9-4818-841c-c7fda20b5d1d</link><guid>http://ayende.com/blog/160903/bug-hunting-in-a-massively-multi-threaded-environment?key=64348966-76a9-4818-841c-c7fda20b5d1d</guid><pubDate>Mon, 18 Feb 2013 10:00:00 GMT</pubDate></item><item><title>Production issue: ASP.Net Cache kills the application</title><description>&lt;p&gt;
	In one of our production deployments, we occasionally get a complete server process crash. Investigating the event log, we have this:&lt;/p&gt;
&lt;blockquote&gt;
	&lt;p&gt;
		Exception: System.InvalidOperationException&lt;/p&gt;
	&lt;p&gt;
		Message: Collection was modified; enumeration operation may not execute.&lt;/p&gt;
	&lt;p&gt;
		StackTrace:&amp;nbsp;&amp;nbsp;&amp;nbsp; at System.Collections.Generic.Dictionary`2.KeyCollection.Enumerator.MoveNext()&lt;/p&gt;
	&lt;p&gt;
		&amp;nbsp;&amp;nbsp; at System.Web.Hosting.ObjectCacheHost.TrimCache(Int32 percent)&lt;/p&gt;
	&lt;p&gt;
		&amp;nbsp;&amp;nbsp; at System.Web.Hosting.HostingEnvironment.TrimCache(Int32 percent)&lt;/p&gt;
	&lt;p&gt;
		&amp;nbsp;&amp;nbsp; at System.Web.Hosting.HostingEnvironment.TrimCache(Int32 percent)&lt;/p&gt;
	&lt;p&gt;
		&amp;nbsp;&amp;nbsp; at System.Web.Hosting.ApplicationManager.TrimCaches(Int32 percent)&lt;/p&gt;
	&lt;p&gt;
		&amp;nbsp;&amp;nbsp; at System.Web.Hosting.CacheManager.CollectInfrequently(Int64 privateBytes)&lt;/p&gt;
	&lt;p&gt;
		&amp;nbsp;&amp;nbsp; at System.Web.Hosting.CacheManager.PBytesMonitorThread(Object state)&lt;/p&gt;
	&lt;p&gt;
		&amp;nbsp;&amp;nbsp; at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)&lt;/p&gt;
	&lt;p&gt;
		&amp;nbsp;&amp;nbsp; at System.Threading._TimerCallback.PerformTimerCallback(Object state)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
	As you can see, this is a case of what appears to be a run of the mill race condition, translated to a process killing exception because it was thrown from a separate thread.&lt;/p&gt;
&lt;p&gt;
	This thread, by the way, is the ASP.Net Cache cleanup thread, and we have no control whatsoever over that. To make things worse, this application &lt;em&gt;doesn&amp;rsquo;t even use&lt;/em&gt; the ASP.NET Cache in any way shape or form.&lt;/p&gt;
&lt;p&gt;
	Any ideas how to resolve this would be very welcome.&lt;/p&gt;
</description><link>http://ayende.com/blog/159937/production-issue-asp-net-cache-kills-the-application?key=20ad711f-f8d9-42d1-a42b-3bf453f325cd</link><guid>http://ayende.com/blog/159937/production-issue-asp-net-cache-kills-the-application?key=20ad711f-f8d9-42d1-a42b-3bf453f325cd</guid><pubDate>Fri, 16 Nov 2012 14:00:00 GMT</pubDate></item><item><title>Silverlight streaming - the race condition is already included</title><description>&lt;p&gt;
	I am not sure how to call this issue, except maddening. For a simple repro, you can check &lt;a href="https://github.com/ayende/Silverlight.Streaming.Repro"&gt;this github repository&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
	The story is quite simple, let us assume that you need to send a set of values from the server to the client. For example, they might be tick values, or updates, or anything of this sort.&lt;/p&gt;
&lt;p&gt;
	You can do this by keeping an HTTP connection open and sending data periodically. This is a well known technique and it works quite well. Except in Silverlight, where it works, but only if you put the appropriate Thread.Sleep() in crucial places.&lt;/p&gt;
&lt;p&gt;
	Here is an example of the server behavior:&lt;/p&gt;
&lt;blockquote&gt;
	&lt;pre class="csharpcode"&gt;
var listener = &lt;span class="kwrd"&gt;new&lt;/span&gt; HttpListener
{
    Prefixes = {&lt;span class="str"&gt;&amp;quot;http://+:8080/&amp;quot;&lt;/span&gt;}
};
listener.Start();
&lt;span class="kwrd"&gt;while&lt;/span&gt; (&lt;span class="kwrd"&gt;true&lt;/span&gt;)
{
    var ctx = listener.GetContext();
    &lt;span class="kwrd"&gt;using&lt;/span&gt; (var writer = &lt;span class="kwrd"&gt;new&lt;/span&gt; StreamWriter(ctx.Response.OutputStream))
    {
        writer.WriteLine(&lt;span class="str"&gt;&amp;quot;first\r\nsecond&amp;quot;&lt;/span&gt;);
        writer.Flush();
    }
    Console.ReadKey();
}
&lt;/pre&gt;
	&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }	&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;
	In this case, note that we are explicitly flushing the response, then just wait. If you look at the actual network traffic, you can see that this will actually be sent, the connection will remain open, and we can actually send additional data as well.&lt;/p&gt;
&lt;p&gt;
	But how do you consume such a thing in Silverlight?&lt;/p&gt;
&lt;blockquote&gt;
	&lt;pre class="csharpcode"&gt;
var webRequest = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(&lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(&lt;span class="str"&gt;&amp;quot;http://localhost:8080/&amp;quot;&lt;/span&gt;));
webRequest.AllowReadStreamBuffering = &lt;span class="kwrd"&gt;false&lt;/span&gt;;
webRequest.Method = &lt;span class="str"&gt;&amp;quot;GET&amp;quot;&lt;/span&gt;;

Task.Factory.FromAsync&amp;lt;WebResponse&amp;gt;(webRequest.BeginGetResponse, webRequest.EndGetResponse, &lt;span class="kwrd"&gt;null&lt;/span&gt;)
    .ContinueWith(task =&amp;gt;
    {
        var responseStream = task.Result.GetResponseStream();
        ReadAsync(responseStream);
    });
&lt;/pre&gt;
	&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }	&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;
	&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
	We start by making sure that we disable read buffering, then we get the response and start reading from it. The read method is a bit complex, because is has to deal with partial response, but it should still be fairly obvious what is going on:&lt;/p&gt;
&lt;blockquote&gt;
	&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;byte&lt;/span&gt;[] buffer = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[128];
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; posInBuffer;
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ReadAsync(Stream responseStream)
{
    Task.Factory.FromAsync&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;(
        (callback, o) =&amp;gt; responseStream.BeginRead(buffer, posInBuffer, buffer.Length - posInBuffer, callback, o),
        responseStream.EndRead, &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        .ContinueWith(task =&amp;gt;
        {
            var read = task.Result;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (read == 0) 
                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; EndOfStreamException();
            &lt;span class="rem"&gt;// find \r\n in newly read range&lt;/span&gt;

            var startPos = 0;
            &lt;span class="kwrd"&gt;byte&lt;/span&gt; prev = 0;
            &lt;span class="kwrd"&gt;bool&lt;/span&gt; foundLines = &lt;span class="kwrd"&gt;false&lt;/span&gt;;
            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = posInBuffer; i &amp;lt; posInBuffer + read; i++)
            {
                &lt;span class="kwrd"&gt;if&lt;/span&gt; (prev == &lt;span class="str"&gt;&amp;#39;\r&amp;#39;&lt;/span&gt; &amp;amp;&amp;amp; buffer[i] == &lt;span class="str"&gt;&amp;#39;\n&amp;#39;&lt;/span&gt;)
                {
                    foundLines = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
                    &lt;span class="rem"&gt;// yeah, we found a line, let us give it to the users&lt;/span&gt;
                    var data = Encoding.UTF8.GetString(buffer, startPos, i - 1 - startPos);
                    startPos = i + 1;
                    Dispatcher.BeginInvoke(() =&amp;gt;
                    {
                        ServerResults.Text += data + Environment.NewLine;
                    });
                }
                prev = buffer[i];
            }
            posInBuffer += read;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (startPos &amp;gt;= posInBuffer) &lt;span class="rem"&gt;// read to end&lt;/span&gt;
            {
                posInBuffer = 0;
                &lt;span class="kwrd"&gt;return&lt;/span&gt;;
            }
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (foundLines == &lt;span class="kwrd"&gt;false&lt;/span&gt;)
                &lt;span class="kwrd"&gt;return&lt;/span&gt;;

            &lt;span class="rem"&gt;// move remaining to the start of buffer, then reset&lt;/span&gt;
            Array.Copy(buffer, startPos, buffer, 0, posInBuffer - startPos);
            posInBuffer -= startPos;
        })
        .ContinueWith(task =&amp;gt;
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (task.IsFaulted)
                &lt;span class="kwrd"&gt;return&lt;/span&gt;;
            ReadAsync(responseStream);
        });
}
&lt;/pre&gt;
	&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }	&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;
	While I am sure that you could find bugs in this code, that isn&amp;rsquo;t the crucial point.&lt;/p&gt;
&lt;p&gt;
	If we run the server, then run the SL client, we could see that we get just &lt;em&gt;one lousy byte, and that is it&lt;/em&gt;. Now, reading about this, it appears that in some versions of some browsers, you need to send 4KB of data to get the connection going. But that isn&amp;rsquo;t what I have observed. I tried sending 4KB+ of data, and I still saw the exact same behavior, we got called for the first byte, and nothing else.&lt;/p&gt;
&lt;p&gt;
	Eventually, I boiled it down to the following &lt;strong&gt;non working&lt;/strong&gt;&lt;em&gt; &lt;/em&gt;example:&lt;/p&gt;
&lt;blockquote&gt;
	&lt;pre class="csharpcode"&gt;
writer.WriteLine(&lt;span class="str"&gt;&amp;quot;first&amp;quot;&lt;/span&gt;);
writer.Flush();
writer.WriteLine(&lt;span class="str"&gt;&amp;quot;second&amp;quot;&lt;/span&gt;);
writer.Flush();&lt;/pre&gt;
	&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }	&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;
	Versus this &lt;strong&gt;working &lt;/strong&gt;example:&lt;/p&gt;
&lt;blockquote&gt;
	&lt;pre class="csharpcode"&gt;
writer.WriteLine(&lt;span class="str"&gt;&amp;quot;first&amp;quot;&lt;/span&gt;);
writer.Flush();
Thread.Sleep(50);
writer.WriteLine(&lt;span class="str"&gt;&amp;quot;second&amp;quot;&lt;/span&gt;);
writer.Flush();&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;
	Yes, you got it right, if I put the thread sleep in the server, I&amp;rsquo;ll get both values in the client. &lt;em&gt;Without&lt;/em&gt; the Thread.Sleep, we get only the first byte. It seems like it isn&amp;rsquo;t an issue of size, but rather of time, and I am at an utter loss to explain what is going on.&lt;/p&gt;
&lt;p&gt;
	Oh, and I am currently awake for 27 hours straight, most of them trying to figure out what the )(&amp;amp;#@!)(DASFPOJDA(FYQ@YREQPOIJFDQ#@R(AHFDS:OKJASPIFHDAPSYUDQ)(RE is going on.&lt;/p&gt;
</description><link>http://ayende.com/blog/157377/silverlight-streaming-the-race-condition-is-already-included?key=360b72c8-c810-4d1f-85bc-ce4f2a9d8763</link><guid>http://ayende.com/blog/157377/silverlight-streaming-the-race-condition-is-already-included?key=360b72c8-c810-4d1f-85bc-ce4f2a9d8763</guid><pubDate>Wed, 25 Jul 2012 09:00:00 GMT</pubDate></item><item><title>The HIGH cost of ConcurrentBag in .NET 4.0</title><description>&lt;p&gt;I got some strange results when using concurrent collections, so I decided to try to track it down, and wrote the following code:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;var count = ?;
var list = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;(count);
var sp = Stopwatch.StartNew();
&lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; count; i++)
{
    list.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; ConcurrentBag&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;());
}
sp.Stop();
Console.WriteLine(&lt;span class="str"&gt;"{0} {2} items in {1:#,#;;0}ms = {3:#,#;;0}ms per item"&lt;/span&gt;,
    sp.Elapsed, sp.ElapsedMilliseconds, count, sp.ElapsedMilliseconds / count);
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;And then I started to play with the numbers, and it is not good.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;10 items in 2ms = 0ms per item&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;This is incredibly high number, you have to understand. Just to compare, List&amp;lt;int&amp;gt; takes 8 ms to create 100,000 items.&lt;/p&gt;
&lt;p&gt;Let us see how it works when we use more of this.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;100 items in 5ms = 0ms per item 
&lt;li&gt;1,000 items in 37ms = 0ms per item 
&lt;li&gt;10,000 items in 2,319ms = 0ms per item&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Note the numbers, will you?&lt;/p&gt;
&lt;p&gt;1,000 items in 37 ms, but 10,000 items? 2.3 &lt;em&gt;seconds&lt;/em&gt;!&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;20,000 items in 21,331ms = 1ms per item&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;And doubling the amount took &lt;em&gt;ten times as long&lt;/em&gt;?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;25,000 items in 32,588ms = 1ms per item&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;And at this point, I stopped trying, because I didn’t have the patience.&lt;/p&gt;
&lt;p&gt;Note that the other concurrent collection, ConcurrentStack, ConcurrentQueue and ConcurrentDictionary do not suffer from the same problem.&lt;/p&gt;
&lt;p&gt;I contacted Microsoft about this, and this is already resolved in .NET 4.5. The underlying issue was that ThreadLocal, which ConcurrentBag uses, didn’t expect to have a lot of instances. That has been fixed, and now can run fairly fast.&lt;/p&gt;</description><link>http://ayende.com/blog/156097/the-high-cost-of-concurrentbag-in-net-4-0?key=742a9bdb-d633-4779-8a43-a5393c97c75a</link><guid>http://ayende.com/blog/156097/the-high-cost-of-concurrentbag-in-net-4-0?key=742a9bdb-d633-4779-8a43-a5393c97c75a</guid><pubDate>Thu, 07 Jun 2012 09:00:00 GMT</pubDate></item><item><title>And this is Ironic</title><description>&lt;p&gt;I was trying to press the Yes button, and this happened:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/And-this-is-I_C621/image_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/And-this-is-I_C621/image_thumb.png" width="1524" height="248"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;To be fair, I am trying to get it to read my mail, and it is a bit… largish, I guess:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/And-this-is-I_C621/image_6.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/And-this-is-I_C621/image_thumb_2.png" width="341" height="22"&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://ayende.com/blog/155777/and-this-is-ironic?key=0421a4fc-ed9c-4ec3-b17b-e5d679e62cee</link><guid>http://ayende.com/blog/155777/and-this-is-ironic?key=0421a4fc-ed9c-4ec3-b17b-e5d679e62cee</guid><pubDate>Fri, 25 May 2012 09:00:00 GMT</pubDate></item><item><title>A bad test</title><description>&lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/A-bad-test_14ECA/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/A-bad-test_14ECA/image_thumb.png" width="953" height="327"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;This is a bad test, because what it does is ensuring that something &lt;em&gt;does not works&lt;/em&gt;. I just finished implementing the session.Advaned.Defer support, and this test got my attention by failing the build.&lt;/p&gt; &lt;p&gt;&lt;em&gt;Bad &lt;/em&gt;test, you should be telling me when I broke something, not when I added new functionality.&lt;/p&gt;</description><link>http://ayende.com/blog/155617/a-bad-test?key=681dcdab-232a-40a7-bb7a-50def0640d9b</link><guid>http://ayende.com/blog/155617/a-bad-test?key=681dcdab-232a-40a7-bb7a-50def0640d9b</guid><pubDate>Fri, 18 May 2012 09:00:00 GMT</pubDate></item><item><title>Non overlapping time periods–because I like the pain of 2 AM wakeup calls</title><description>&lt;p&gt;
	This post is partly in response for &lt;a href="http://codebetter.com/johnvpetersen/2012/03/13/229-the-case-of-the-missing-data-point-and-the-curious-thing-a-leap-year-is/"&gt;this post&lt;/a&gt;, discussing the Azure problem with leap year. But it is actually a bit more general than that.&lt;/p&gt;
&lt;p&gt;
	In my code, here is how I define &amp;ldquo;one year from now&amp;rdquo;:&lt;/p&gt;
&lt;blockquote&gt;
	&lt;p&gt;
		DateTime.Today.AddYears(1).AddDays(3);&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
	As it turned out, this tend to have a lot of implications on your business, most of them are actually pretty good ones.&lt;/p&gt;
&lt;p&gt;
	For a start, you will never get hit with a leap year bug, but more importantly, you are never going to have to deal with an immediate cutoff. This is important because it gives you time. Mostly, it gives you time to screw up, but having the time to do so without having an egg all of your face is a really nice thing.&lt;/p&gt;
&lt;p&gt;
	For example, all of our subscriptions are using a similar method of calculation, and this is why we can take the ordering system down for a few hours or even a day or two and no one will actually notice. We have a big grace period in which we can work things out.&lt;/p&gt;
&lt;p&gt;
	Sure, a user gets 3 &amp;ldquo;extra&amp;rdquo; days for free out of this, but frankly, I don&amp;rsquo;t give a damn. It is more important that I get the buffer, and most users like it much better when you don&amp;rsquo;t slam the doors in their faces on the first chance.&lt;/p&gt;
&lt;p&gt;
	One of the things that is important is &lt;em&gt;style&lt;/em&gt;, and giving a grace period for those sort of things is crucial.&lt;/p&gt;
</description><link>http://ayende.com/blog/155521/non-overlapping-time-periods-because-i-like-the-pain-of-2-am-wakeup-calls?key=d3ef0b84-4bb5-4f6a-9679-6759e89eef46</link><guid>http://ayende.com/blog/155521/non-overlapping-time-periods-because-i-like-the-pain-of-2-am-wakeup-calls?key=d3ef0b84-4bb5-4f6a-9679-6759e89eef46</guid><pubDate>Tue, 15 May 2012 09:00:00 GMT</pubDate></item><item><title>Rotten Scheduling: Don’t roll your own</title><description>&lt;p&gt;“We need to run a specific task every 72 hours, I thought about this approach…”&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TimedTask
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Timer Timer;
  
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Init()
  {
    Timer = &lt;span class="kwrd"&gt;new&lt;/span&gt; Timer(ExecuteEvery72Hours, &lt;span class="kwrd"&gt;null&lt;/span&gt;, TimeSpan.FromHours(72), TimeSpan.FromHours(72));
  }
  
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ExecuteEvery72Hours()
  {
    &lt;span class="rem"&gt;// do something important&lt;/span&gt;
  }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is a bloody rotten idea, let us see why…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What happens if your application is recycled every 29 hours?&lt;/li&gt;
&lt;li&gt;What happens if your application is always on, but during that 72 hour call, it was offline?&lt;/li&gt;
&lt;li&gt;What happens if your task actually takes more than 72 hours to run?&lt;/li&gt;
&lt;li&gt;What happens if the task fails? &lt;/li&gt;
&lt;li&gt;How do you report errors, warnings, etc?&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Scheduling is a &lt;em&gt;hard &lt;/em&gt;problem. There are a &lt;em&gt;lot&lt;/em&gt; of things that you actually need to consider. And the code above is really considering none of them. I would be very surprised if something like that &lt;em&gt;ever&lt;/em&gt; run. in production. It most certainly can’t be made to run reliably.&lt;/p&gt;
&lt;p&gt; Things that run every X time, where X is a long time (hours / days) tend to be pretty important. In some of the systems that we wrote, that include doing things like updating VAT and interest rates, pulling from external source, generating the weekly report, etc.&lt;/p&gt;
&lt;p&gt;You do &lt;em&gt;not&lt;/em&gt; want this to be messed up.&lt;/p&gt;
&lt;p&gt;If you need to do anything like that, make use of the builtin scheduling features of the OS you are running on (Windows Task Scheduler is an amazingly full featured, and cron isn’t bad if you are running on Linux). If you still insist on doing this in code, at the very least do something like this:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TimedTask
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Timer Timer;
  
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Init()
  {
    Timer = &lt;span class="kwrd"&gt;new&lt;/span&gt; Timer(()=&amp;gt;
    {
      &lt;span class="kwrd"&gt;if&lt;/span&gt;( (DateTime.UtcNow - GetLastExecutedTime()) &amp;gt; 72)
        ExecuteEvery72Hours();
    }, &lt;span class="kwrd"&gt;null&lt;/span&gt;, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
  }
  
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ExecuteEvery72Hours()
  {
    &lt;span class="rem"&gt;// do something important&lt;/span&gt;
  }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;This still has a lot of problems, but at least it it solving some crucial problems for you (note that GetLastExecutedTime has to be a persisted value).&lt;/p&gt;
&lt;p&gt;Of course, if you need something like this in your code, you have better use something like Quartz, instead. Don’t roll your own. Sure, this is ten lines of code to do so, but as I said, this is the very basics, and it gets complex &lt;em&gt;really &lt;/em&gt;fast.&lt;/p&gt;</description><link>http://ayende.com/blog/155489/rotten-scheduling-dont-roll-your-own?key=89b941e1-86d7-443e-a93c-28ed0703bfc2</link><guid>http://ayende.com/blog/155489/rotten-scheduling-dont-roll-your-own?key=89b941e1-86d7-443e-a93c-28ed0703bfc2</guid><pubDate>Mon, 14 May 2012 09:00:00 GMT</pubDate></item><item><title>Rotten Scheduling</title><description>&lt;p&gt;“We need to run a specific task every 72 hours, I thought about this approach…”&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TimedTask
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Timer Timer;
  
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Init()
  {
    Timer = &lt;span class="kwrd"&gt;new&lt;/span&gt; Timer(ExecuteEvery72Hours, &lt;span class="kwrd"&gt;null&lt;/span&gt;, TimeSpan.FromHours(72), TimeSpan.FromHours(72));
  }
  
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ExecuteEvery72Hours()
  {
    &lt;span class="rem"&gt;// do something important&lt;/span&gt;
  }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;Let us assume that this code is being called properly. Why is this a bloody rotten idea?&lt;/p&gt;</description><link>http://ayende.com/blog/155457/rotten-scheduling?key=47a203dc-eb07-448a-82e3-2c1919704db3</link><guid>http://ayende.com/blog/155457/rotten-scheduling?key=47a203dc-eb07-448a-82e3-2c1919704db3</guid><pubDate>Fri, 11 May 2012 09:00:00 GMT</pubDate></item><item><title>The best multi threading debugging tool is Microsoft Excel</title><description>&lt;p&gt;In any system that gets to a certain size, especially one that is multi threaded, there are a certain class of bugs that are &lt;em&gt;really &lt;/em&gt;a bitch to figure out. &lt;/p&gt; &lt;p&gt;They are usually boil down to some sort of a race condition. I have been doing that recently, trying to fix a bunch of race conditions in RavenDB. The problem with race conditions is that they are incredibly hard to reproduce, and even when you can reproduce them, you can’t really &lt;em&gt;debug them&lt;/em&gt;. &lt;/p&gt; &lt;p&gt;I came up with the following test harness to try to &lt;a href="https://github.com/ayende/ravendb/blob/27564b0e543051b298f8716e5934c7bea812a4df/Raven.Tryouts/Program.cs"&gt;narrow things down&lt;/a&gt;. Basically, create a test case that sometimes fails, and run it a thousand times.&lt;/p&gt; &lt;p&gt;Odds are that you’ll get the failure, but that isn’t the important bit. The important bit is that you setup logging properly. In my case, I set things up so the logging output to CSV and each test run had a different file.&lt;/p&gt; &lt;p&gt;This gives me output that looks like this:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;23:54:34.5952,Raven.Database.Server.HttpServer,Debug,Request #&amp;nbsp; 10: GET&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -&amp;nbsp;&amp;nbsp;&amp;nbsp; 12 ms - &amp;lt;default&amp;gt;&amp;nbsp; - 200 - /indexes/dynamic/Companies?query=&amp;amp;start=0&amp;amp;pageSize=1&amp;amp;aggregation=None,,11&lt;br&gt;23:54:34.5952,Raven.Client.Document.SessionOperations.QueryOperation,Debug,"Stale query results on non stale query '' on index 'dynamic/Companies' in '&lt;a href="http://reduction:8079/'"&gt;http://reduction:8079/'&lt;/a&gt;, query will be retried, index etag is: bcf0fed1-d975-43b4-bfb7-65221ef06b99",,1&lt;br&gt;23:54:34.5952,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 10, for: TasksExecuter, will wait for additional work",,6&lt;br&gt;23:54:34.5952,Raven.Database.Indexing.WorkContext,Debug,Incremented work counter to 11 because: WORK BY IndexingExecuter,,12&lt;br&gt;23:54:34.5952,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 11, for: TasksExecuter, will wait for additional work",,6&lt;br&gt;23:54:34.5952,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 11, for: ReducingExecuter, will wait for additional work",,21&lt;br&gt;23:54:34.5952,Raven.Database.Indexing.WorkContext,Debug,"No work was found, workerWorkCounter: 11, for: IndexingExecuter, will wait for additional work",,12&lt;br&gt;23:54:34.6952,Raven.Client.Document.SessionOperations.QueryOperation,Debug,Executing query '' on index 'dynamic/Companies' in '&lt;a href="http://reduction:8079/',,1"&gt;http://reduction:8079/',,1&lt;/a&gt;&lt;br&gt;23:54:34.8172,Raven.Database.Indexing.Index.Querying,Debug,Issuing query on index Temp/Companies for all documents,,11&lt;br&gt;23:54:34.8172,Raven.Storage.Esent.StorageActions.DocumentStorageActions,Debug,Document with key 'companies/1' was found,,11&lt;br&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Not really helpful in figure out what is going on, right? Except for one tiny thing, we load them to Excel, and we use conditional formatting to get things to look like this:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/The-best-multi-threading-debugging-tool_1502F/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/The-best-multi-threading-debugging-tool_1502F/image_thumb.png" width="1239" height="544"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;The reason this is so helpful? You can actually see the threads interleaving. This usually help me get to roughly the right place, and then I can add additional logging so I can figure out better what is &lt;em&gt;actually&lt;/em&gt; going on.&lt;/p&gt; &lt;p&gt;I was able to detect and fix several race conditions using this approach. &lt;/p&gt; &lt;p&gt;And yes, I know that this is basically printf() debugging. But at least it is printf() debugging with pretty colors.&lt;/p&gt;</description><link>http://ayende.com/blog/154817/the-best-multi-threading-debugging-tool-is-microsoft-excel?key=4868300a-e80b-4c00-9fc8-84f988cf16a4</link><guid>http://ayende.com/blog/154817/the-best-multi-threading-debugging-tool-is-microsoft-excel?key=4868300a-e80b-4c00-9fc8-84f988cf16a4</guid><pubDate>Wed, 09 May 2012 09:00:00 GMT</pubDate></item><item><title>Thou shall not delete</title><description>&lt;p&gt;Ouch!&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult DeleteComment(&lt;span class="kwrd"&gt;int&lt;/span&gt; id)
{
  var userComment = RavenSession.Load&amp;lt;UserComment&amp;gt;(id);

  &lt;span class="kwrd"&gt;if&lt;/span&gt; (userComment == &lt;span class="kwrd"&gt;null&lt;/span&gt;)
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; HttpStatusCodeResult(204);

  var user = RavenSession.GetUser(User.Identity.Name);
  &lt;span class="kwrd"&gt;if&lt;/span&gt;(user == &lt;span class="kwrd"&gt;null&lt;/span&gt; || (user.Role != UserRole.Moderator &amp;amp;&amp;amp; user.Role != UserRole.Admin))
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; HttpStatusCodeResult(403, &lt;span class="str"&gt;"You must be logged in as moderator or admin to be able to delete comments"&lt;/span&gt;);

  RavenSession.Delete(user);

  &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; HttpStatusCodeResult(204);
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;</description><link>http://ayende.com/blog/154977/thou-shall-not-delete?key=a86334f4-49a8-4d6d-aca1-848c98741867</link><guid>http://ayende.com/blog/154977/thou-shall-not-delete?key=a86334f4-49a8-4d6d-aca1-848c98741867</guid><pubDate>Wed, 02 May 2012 09:00:00 GMT</pubDate></item><item><title>What is wrong here? Solution</title><description>&lt;p&gt;In my previous post, I showed the database schema and the UI and asked what was wrong with that. Before we move on, here is what I showed.&lt;/p&gt; &lt;p&gt;&lt;img alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/What-is-wrong-here_8F02/image_thumb_1.png"&gt;&lt;/p&gt; &lt;p&gt;&lt;img alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/What-is-wrong-here_8F02/image_thumb_3.png"&gt;&lt;/p&gt;  &lt;p&gt;If you looked carefully, you might have noticed that there are duplicate PO# and Tracking# in the UI. More than that, we somehow double charged the customer for shipping.&lt;/p&gt; &lt;p&gt;What is going on?&lt;/p&gt; &lt;p&gt;It is actually fairly obvious, when you think about it. Look at the schema, there isn’t actually &lt;em&gt;any&lt;/em&gt; association between the Tracking # and the PO #. In most orders, we have only 1 PO #, so it was easy to just add this information by just pulling it from the DB and adding a few columns. But when we got an order that has multiple POs… that is when all hell breaks lose.&lt;/p&gt; &lt;p&gt;This is a classic Cartesian Product problem.&lt;/p&gt; &lt;p&gt;The solution?&amp;nbsp; Actually model the UI to avoid suggesting that there is a relationship between the tracking and purchase orders, like this:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/What-is-wrong-here-solution_919F/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/What-is-wrong-here-solution_919F/image_thumb.png" width="611" height="892"&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://ayende.com/blog/153313/what-is-wrong-here-solution?key=4e108bbb-a95a-4e8e-9006-e300403e7f3d</link><guid>http://ayende.com/blog/153313/what-is-wrong-here-solution?key=4e108bbb-a95a-4e8e-9006-e300403e7f3d</guid><pubDate>Sun, 26 Feb 2012 10:00:00 GMT</pubDate></item><item><title>What is wrong here?</title><description>&lt;p&gt;This case, it isn’t code that I am going to show, rather, I am going to show the final UI and the database structure, and let you figure:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;What is wrong.&lt;/li&gt; &lt;li&gt;How to fix this.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Here is the database schema:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/What-is-wrong-here_8F02/image_4.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/What-is-wrong-here_8F02/image_thumb_1.png" width="449" height="497"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;And here is the problem:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/What-is-wrong-here_8F02/image_8.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/What-is-wrong-here_8F02/image_thumb_3.png" width="611" height="892"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Hints, this used to work for a long time, and suddenly it doesn’t, and the customer is &lt;em&gt;pissed, &lt;/em&gt;annoyed and threatening to sue.&lt;/p&gt;</description><link>http://ayende.com/blog/153281/what-is-wrong-here?key=8b24b408-1be2-4d67-9945-5be9e2978949</link><guid>http://ayende.com/blog/153281/what-is-wrong-here?key=8b24b408-1be2-4d67-9945-5be9e2978949</guid><pubDate>Fri, 24 Feb 2012 10:00:00 GMT</pubDate></item><item><title>Ask Ayende: What about the QA env?</title><description>&lt;p&gt;&lt;a&gt;Matthew Bonig&lt;/a&gt; asks, with regards to &lt;a href="http://ayende.com/blog/152738/bug-hunt-what-made-this-blog-slow"&gt;a bug&lt;/a&gt; in RavenDB MVC Integration (RavenDB Profiler) that caused major slow down on this blog.: &lt;blockquote&gt; &lt;p&gt;I'd be very curious to know how this code got published to a production environment without getting caught. I would have thought this problem would have occurred in any testing environment as well as it did here. Ayende, can you comment on where the process broke down and how such an obvious bug was able to slip through?&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Well, the answer for that comes in two parts. The first part&amp;nbsp; is that no process broke down. We use our own assets for final testing of all our software, that means that whenever there is a stable RavenDB release pending (and sometimes just when we feel like it) we move our infrastructure to the latest and greatest. &lt;p&gt;&lt;em&gt;Why? &lt;/em&gt; &lt;p&gt;Because as hard as you try testing, you will never be able to catch everything. Production is the final test ground, and we have obvious incentives of trying to make sure that everything works.&amp;nbsp; It is dogfooding, basically. Except that if we get a lemon, that is a very public one. &lt;p&gt;It means that whenever we make a stable release, we can do that with high degree of confidence that everything is going to work, not just because all the tests are passing, but because our production systems had days to actually see if things are right. &lt;p&gt;The second part of this answer is that this is neither an obvious bug nor one that is easy to catch. Put simply, things &lt;em&gt;worked&lt;/em&gt;. There wasn’t even an infinite loop that would make it obvious that something is wrong, it is just that there was a lot of network traffic that you would notice only if you either had a tracer running, or were trying to figure out why the browser was suddenly so busy. &lt;p&gt;Here is a challenge, try to devise some form of an automated test that would catch something like this error, but do so without actually testing for &lt;em&gt;this specific issue&lt;/em&gt;. After all, it is unlikely that someone would have written a test for this unless they run into the error in the first place. So I would be really interested in seeing what sort of automated approaches would have caught that.&lt;/p&gt;</description><link>http://ayende.com/blog/153825/ask-ayende-what-about-the-qa-env?key=6d32f8c5-ef61-45ef-90b3-edb437ee2333</link><guid>http://ayende.com/blog/153825/ask-ayende-what-about-the-qa-env?key=6d32f8c5-ef61-45ef-90b3-edb437ee2333</guid><pubDate>Tue, 31 Jan 2012 07:33:00 GMT</pubDate></item><item><title>Bug Hunt: What made this blog slow?</title><description>&lt;p&gt;A while ago the blog start taking 100% CPU on the client machines. Obviously we were doing something very wrong there, but what exactly was it?&lt;/p&gt; &lt;p&gt;We tracked down the problem to the following code:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Bug-Hunt-What-made-this-blog-slow_FB72/image_thumb_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image_thumb" border="0" alt="image_thumb" src="http://ayende.com/blog/Images/Windows-Live-Writer/Bug-Hunt-What-made-this-blog-slow_FB72/image_thumb_thumb.png" width="587" height="224"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Bug-Hunt-What-made-this-blog-slow_FB72/image_thumb%5B1%5D_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image_thumb[1]" border="0" alt="image_thumb[1]" src="http://ayende.com/blog/Images/Windows-Live-Writer/Bug-Hunt-What-made-this-blog-slow_FB72/image_thumb%5B1%5D_thumb.png" width="392" height="113"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;As you can probably guess, the problem is that we have what is effective an infinite loop. On any Ajax request, we will generate a new Ajax request. And that applies to &lt;em&gt;our own requests as well&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;The fix was pretty obvious when we figured out what was going on, but until then…&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Bug-Hunt-What-made-this-blog-slow_FB72/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Bug-Hunt-What-made-this-blog-slow_FB72/image_thumb_3.png" width="538" height="142"&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://ayende.com/blog/152738/bug-hunt-what-made-this-blog-slow?key=a6c5d835-aac9-4e13-bfcf-df39626ed5dc</link><guid>http://ayende.com/blog/152738/bug-hunt-what-made-this-blog-slow?key=a6c5d835-aac9-4e13-bfcf-df39626ed5dc</guid><pubDate>Mon, 30 Jan 2012 10:00:00 GMT</pubDate></item><item><title>Bug Hunt: What made this blog slow?</title><description>&lt;p&gt;A while ago the blog start taking 100% CPU on the client machines. Obviously we were doing something very wrong there, but what exactly was it?&lt;/p&gt; &lt;p&gt;We track down the problem to the following code, can you figure out what the problem?&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Bug-Hunt-What-made-this-blog-slow_FB38/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Bug-Hunt-What-made-this-blog-slow_FB38/image_thumb.png" width="587" height="224"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Bug-Hunt-What-made-this-blog-slow_FB38/image_4.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Bug-Hunt-What-made-this-blog-slow_FB38/image_thumb_1.png" width="392" height="113"&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://ayende.com/blog/152737/bug-hunt-what-made-this-blog-slow?key=9aca2476-1a93-4c84-9284-020ee76f79a5</link><guid>http://ayende.com/blog/152737/bug-hunt-what-made-this-blog-slow?key=9aca2476-1a93-4c84-9284-020ee76f79a5</guid><pubDate>Fri, 27 Jan 2012 10:00:00 GMT</pubDate></item><item><title>What is up with all those 404?</title><description>&lt;p&gt;Recently I had a spate of posts showing up, and then reporting 404. The actual reason behind that was that the server and the database disagreed with one another with regards to the timezone of the post (one was using UTC, the other local). Sorry for the trouble, and this shouldn’t happen any longer.&lt;/p&gt;</description><link>http://ayende.com/blog/152609/what-is-up-with-all-those-404?key=84e3baa6-c032-4e33-88ab-7b9745f29bfc</link><guid>http://ayende.com/blog/152609/what-is-up-with-all-those-404?key=84e3baa6-c032-4e33-88ab-7b9745f29bfc</guid><pubDate>Thu, 22 Dec 2011 20:00:00 GMT</pubDate></item><item><title>Mixing Integrated Authentication and Anonymous Authentication with PreAuthenticated = true doesn’t work</title><description>&lt;p&gt;This &lt;a href="http://stackoverflow.com/questions/1547661/ie-iis-integrated-authentication-problem"&gt;StackOverflow question&lt;/a&gt; indicate that it is half a bug and half a feature, but that it sure as hell looks like a bug to me.&lt;/p&gt; &lt;p&gt;Let us assume that we have a couple of endpoints in our application, called &lt;strong&gt;&lt;a href="http://localhost:8080/secure"&gt;http://localhost:8080/secure&lt;/a&gt; &lt;/strong&gt; and &lt;a href="http://localhost:8080/public"&gt;&lt;strong&gt;http://localhost:8080/public&lt;/strong&gt;&lt;/a&gt;. As you can imagine, the secure endpoint is… well, secure, and requires authentication. The public endpoint does not.&lt;/p&gt; &lt;p&gt;We want to optimize the number of request we make, so we specify PreAuthenticated = true; And that is where all hell break lose. &lt;/p&gt; &lt;p&gt;The problem is that it appears that when using request with entity body (in other words, PUT / POST) with PreAuthenticate = true, the .NET framework will issue a PUT / POST request with &lt;em&gt;empty body&lt;/em&gt; to the server. Presumably to get the 401 authentication information. At that point, if the endpoint that it happened to have reached is public, it will be accepted as a standard request, and processing will be tried. The problem here is that it has an &lt;em&gt;empty body&lt;/em&gt;, so that has a very strong likelihood of failing.&lt;/p&gt; &lt;p&gt;This error cost me a day and a half or so. Here is the full repro:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main()
{
    &lt;span class="kwrd"&gt;new&lt;/span&gt; Thread(Server)
    {
        IsBackground = &lt;span class="kwrd"&gt;true&lt;/span&gt;
    }.Start();

    Thread.Sleep(500); &lt;span class="rem"&gt;// let the server start&lt;/span&gt;

    &lt;span class="kwrd"&gt;bool&lt;/span&gt; secure = &lt;span class="kwrd"&gt;false&lt;/span&gt;;
    &lt;span class="kwrd"&gt;while&lt;/span&gt; (&lt;span class="kwrd"&gt;true&lt;/span&gt;)
    {
        secure = !secure;
        Console.Write(&lt;span class="str"&gt;"Sending: "&lt;/span&gt;);
        var str = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;(&lt;span class="str"&gt;'a'&lt;/span&gt;, 621);
        var req = WebRequest.Create(secure ? &lt;span class="str"&gt;"http://localhost:8080/secure"&lt;/span&gt; : &lt;span class="str"&gt;"http://localhost:8080/public"&lt;/span&gt;);
        req.Method = &lt;span class="str"&gt;"PUT"&lt;/span&gt;;

        var byteCount = Encoding.UTF8.GetByteCount(str);
        req.UseDefaultCredentials = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
        req.Credentials = CredentialCache.DefaultCredentials;
        req.PreAuthenticate = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
        req.ContentLength = byteCount;

        &lt;span class="kwrd"&gt;using&lt;/span&gt;(var stream = req.GetRequestStream())
        {
            var bytes = Encoding.UTF8.GetBytes(str);
            stream.Write(bytes, 0, bytes.Length);
            stream.Flush();
        }

        req.GetResponse().Close();

    }

}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;And the server code:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Server()
{
    var listener = &lt;span class="kwrd"&gt;new&lt;/span&gt; HttpListener();
    listener.Prefixes.Add(&lt;span class="str"&gt;"http://+:8080/"&lt;/span&gt;);
    listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication | AuthenticationSchemes.Anonymous;
    listener.AuthenticationSchemeSelectorDelegate = request =&amp;gt;
    {

        &lt;span class="kwrd"&gt;return&lt;/span&gt; request.RawUrl.Contains(&lt;span class="str"&gt;"public"&lt;/span&gt;) ? AuthenticationSchemes.Anonymous : AuthenticationSchemes.IntegratedWindowsAuthentication;
    };

    listener.Start();

    &lt;span class="kwrd"&gt;while&lt;/span&gt; (&lt;span class="kwrd"&gt;true&lt;/span&gt;)
    {
        var context = listener.GetContext();
        Console.WriteLine(context.User != &lt;span class="kwrd"&gt;null&lt;/span&gt; ? context.User.Identity.Name : &lt;span class="str"&gt;"Anonymous"&lt;/span&gt;);
        &lt;span class="kwrd"&gt;using&lt;/span&gt;(var reader = &lt;span class="kwrd"&gt;new&lt;/span&gt; StreamReader(context.Request.InputStream))
        {
            var readToEnd = reader.ReadToEnd();
            &lt;span class="kwrd"&gt;if&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(readToEnd))
            {
                Console.WriteLine(&lt;span class="str"&gt;"WTF?!"&lt;/span&gt;);
                Environment.Exit(1);
            }
        }

        context.Response.StatusCode = 200;
        context.Response.Close();
    }
}
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;If we remove pre authenticate is set to false, everything works, but then we have twice as many requests. The annoying thing is that if it would be trying to authenticate to a public endpoint, nothing would happen, &lt;em&gt;if it were sending the bloody entity body along as well&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This is &lt;em&gt;quite &lt;/em&gt;annoying.&lt;/p&gt;</description><link>http://ayende.com/blog/149505/mixing-integrated-authentication-and-anonymous-authentication-with-preauthenticated-true-doesnt-work?key=0604af72-398f-4bb3-a504-e576207acbb8</link><guid>http://ayende.com/blog/149505/mixing-integrated-authentication-and-anonymous-authentication-with-preauthenticated-true-doesnt-work?key=0604af72-398f-4bb3-a504-e576207acbb8</guid><pubDate>Thu, 22 Dec 2011 10:00:00 GMT</pubDate></item><item><title>Dynamic resolution rules joy</title><description>&lt;p&gt;In the following code, what do you believe the output should be?&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Program
{
    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
    {
        dynamic stupid = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stupid{Age = 3};

        Console.WriteLine(stupid.Age);
    }
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Stupid : DynamicObject
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Age { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; TryGetMember(GetMemberBinder binder, &lt;span class="kwrd"&gt;out&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; result)
    {
        result = 1;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;
    }
}
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;Argh!&lt;/p&gt;
&lt;p&gt;The default is to use the actual type members, and then fall back to the dynamic behavior. Whereas I would expect it to first check the dynamic behavior and then fall back to the actual members if it can’t find dynamic stuff.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Quite&lt;/em&gt; annoying.&lt;/p&gt;</description><link>http://ayende.com/blog/119809/dynamic-resolution-rules-joy?key=d1416f3b-99af-410e-9345-d2b368a3c961</link><guid>http://ayende.com/blog/119809/dynamic-resolution-rules-joy?key=d1416f3b-99af-410e-9345-d2b368a3c961</guid><pubDate>Fri, 21 Oct 2011 10:00:00 GMT</pubDate></item></channel></rss>