I was in looking at some code today in httplistener and didn't understand some of the stuff going on (was hitting a performance penalty due to it).<div><br></div><div>In particular I was a bit confused when looking at Begin and EndGetContext (included below).. It would seem harmful to call ares.Complete below while holding a lock to wait_queue. I just wanted to see what someone may have been thinking before spending time on a patch</div>
<div><br></div><div><pre style="margin-top:0px;margin-bottom:0px;padding:0px;border:0px;font-size:12px;font-family:Consolas,'Liberation Mono',Courier,monospace;color:rgb(51,51,51);line-height:16px"><div class="line" id="LC175" style="margin:0px;padding:0px 0px 0px 10px;border:0px">
                <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">public</span> <span class="n" style="margin:0px;padding:0px;border:0px">IAsyncResult</span> <span class="nf" style="margin:0px;padding:0px;border:0px;color:rgb(153,0,0);font-weight:bold">BeginGetContext</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">AsyncCallback</span> <span class="n" style="margin:0px;padding:0px;border:0px">callback</span><span class="p" style="margin:0px;padding:0px;border:0px">,</span> <span class="n" style="margin:0px;padding:0px;border:0px">Object</span> <span class="n" style="margin:0px;padding:0px;border:0px">state</span><span class="p" style="margin:0px;padding:0px;border:0px">)</span></div>
<div class="line" id="LC176" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                <span class="p" style="margin:0px;padding:0px;border:0px">{</span></div><div class="line" id="LC177" style="margin:0px;padding:0px 0px 0px 10px;border:0px">
                        <span class="n" style="margin:0px;padding:0px;border:0px">CheckDisposed</span> <span class="p" style="margin:0px;padding:0px;border:0px">();</span></div><div class="line" id="LC178" style="margin:0px;padding:0px 0px 0px 10px;border:0px">
                        <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">if</span> <span class="p" style="margin:0px;padding:0px;border:0px">(!</span><span class="n" style="margin:0px;padding:0px;border:0px">listening</span><span class="p" style="margin:0px;padding:0px;border:0px">)</span></div>
<div class="line" id="LC179" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">throw</span> <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">new</span> <span class="nf" style="margin:0px;padding:0px;border:0px;color:rgb(153,0,0);font-weight:bold">InvalidOperationException</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="s" style="margin:0px;padding:0px;border:0px;color:rgb(221,17,68)">"Please, call Start before using this method."</span><span class="p" style="margin:0px;padding:0px;border:0px">);</span></div>
<div class="line" id="LC180" style="margin:0px;padding:0px 0px 0px 10px;border:0px"><br></div><div class="line" id="LC181" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                  <span class="n" style="margin:0px;padding:0px;border:0px">ListenerAsyncResult</span> <span class="n" style="margin:0px;padding:0px;border:0px">ares</span> <span class="p" style="margin:0px;padding:0px;border:0px">=</span> <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">new</span> <span class="n" style="margin:0px;padding:0px;border:0px">ListenerAsyncResult</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">callback</span><span class="p" style="margin:0px;padding:0px;border:0px">,</span> <span class="n" style="margin:0px;padding:0px;border:0px">state</span><span class="p" style="margin:0px;padding:0px;border:0px">);</span></div>
<div class="line" id="LC182" style="margin:0px;padding:0px 0px 0px 10px;border:0px"><br></div><div class="line" id="LC183" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                  <span class="c1" style="margin:0px;padding:0px;border:0px;color:rgb(153,153,136);font-style:italic">// lock wait_queue early to avoid race conditions</span></div>
<div class="line" id="LC184" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                        <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">lock</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">wait_queue</span><span class="p" style="margin:0px;padding:0px;border:0px">)</span> <span class="p" style="margin:0px;padding:0px;border:0px">{</span></div>
<div class="line" id="LC185" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">lock</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">ctx_queue</span><span class="p" style="margin:0px;padding:0px;border:0px">)</span> <span class="p" style="margin:0px;padding:0px;border:0px">{</span></div>
<div class="line" id="LC186" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                        <span class="n" style="margin:0px;padding:0px;border:0px">HttpListenerContext</span> <span class="n" style="margin:0px;padding:0px;border:0px">ctx</span> <span class="p" style="margin:0px;padding:0px;border:0px">=</span> <span class="n" style="margin:0px;padding:0px;border:0px">GetContextFromQueue</span> <span class="p" style="margin:0px;padding:0px;border:0px">();</span></div>
<div class="line" id="LC187" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                        <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">if</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">ctx</span> <span class="p" style="margin:0px;padding:0px;border:0px">!=</span> <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">null</span><span class="p" style="margin:0px;padding:0px;border:0px">)</span> <span class="p" style="margin:0px;padding:0px;border:0px">{</span></div>
<div class="line" id="LC188" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                                <span class="n" style="margin:0px;padding:0px;border:0px">ares</span><span class="p" style="margin:0px;padding:0px;border:0px">.</span><span class="n" style="margin:0px;padding:0px;border:0px">Complete</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">ctx</span><span class="p" style="margin:0px;padding:0px;border:0px">,</span> <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">true</span><span class="p" style="margin:0px;padding:0px;border:0px">);</span></div>
<div class="line" id="LC189" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                                <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">return</span> <span class="n" style="margin:0px;padding:0px;border:0px">ares</span><span class="p" style="margin:0px;padding:0px;border:0px">;</span></div>
<div class="line" id="LC190" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                        <span class="p" style="margin:0px;padding:0px;border:0px">}</span></div><div class="line" id="LC191" style="margin:0px;padding:0px 0px 0px 10px;border:0px">
                                <span class="p" style="margin:0px;padding:0px;border:0px">}</span></div><div class="line" id="LC192" style="margin:0px;padding:0px 0px 0px 10px;border:0px"><br></div><div class="line" id="LC193" style="margin:0px;padding:0px 0px 0px 10px;border:0px">
                                <span class="n" style="margin:0px;padding:0px;border:0px">wait_queue</span><span class="p" style="margin:0px;padding:0px;border:0px">.</span><span class="n" style="margin:0px;padding:0px;border:0px">Add</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">ares</span><span class="p" style="margin:0px;padding:0px;border:0px">);</span></div>
<div class="line" id="LC194" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                        <span class="p" style="margin:0px;padding:0px;border:0px">}</span></div><div class="line" id="LC195" style="margin:0px;padding:0px 0px 0px 10px;border:0px">
<br></div><div class="line" id="LC196" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                  <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">return</span> <span class="n" style="margin:0px;padding:0px;border:0px">ares</span><span class="p" style="margin:0px;padding:0px;border:0px">;</span></div>
<div class="line" id="LC197" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                <span class="p" style="margin:0px;padding:0px;border:0px">}</span></div><div class="line" id="LC198" style="margin:0px;padding:0px 0px 0px 10px;border:0px">
<br></div><div class="line" id="LC199" style="margin:0px;padding:0px 0px 0px 10px;border:0px">          <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">public</span> <span class="n" style="margin:0px;padding:0px;border:0px">HttpListenerContext</span> <span class="nf" style="margin:0px;padding:0px;border:0px;color:rgb(153,0,0);font-weight:bold">EndGetContext</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">IAsyncResult</span> <span class="n" style="margin:0px;padding:0px;border:0px">asyncResult</span><span class="p" style="margin:0px;padding:0px;border:0px">)</span></div>
<div class="line" id="LC200" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                <span class="p" style="margin:0px;padding:0px;border:0px">{</span></div><div class="line" id="LC201" style="margin:0px;padding:0px 0px 0px 10px;border:0px">
                        <span class="n" style="margin:0px;padding:0px;border:0px">CheckDisposed</span> <span class="p" style="margin:0px;padding:0px;border:0px">();</span></div><div class="line" id="LC202" style="margin:0px;padding:0px 0px 0px 10px;border:0px">
                        <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">if</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">asyncResult</span> <span class="p" style="margin:0px;padding:0px;border:0px">==</span> <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">null</span><span class="p" style="margin:0px;padding:0px;border:0px">)</span></div>
<div class="line" id="LC203" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">throw</span> <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">new</span> <span class="nf" style="margin:0px;padding:0px;border:0px;color:rgb(153,0,0);font-weight:bold">ArgumentNullException</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="s" style="margin:0px;padding:0px;border:0px;color:rgb(221,17,68)">"asyncResult"</span><span class="p" style="margin:0px;padding:0px;border:0px">);</span></div>
<div class="line" id="LC204" style="margin:0px;padding:0px 0px 0px 10px;border:0px"><br></div><div class="line" id="LC205" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                  <span class="n" style="margin:0px;padding:0px;border:0px">ListenerAsyncResult</span> <span class="n" style="margin:0px;padding:0px;border:0px">ares</span> <span class="p" style="margin:0px;padding:0px;border:0px">=</span> <span class="n" style="margin:0px;padding:0px;border:0px">asyncResult</span> <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">as</span> <span class="n" style="margin:0px;padding:0px;border:0px">ListenerAsyncResult</span><span class="p" style="margin:0px;padding:0px;border:0px">;</span></div>
<div class="line" id="LC206" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                        <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">if</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">ares</span> <span class="p" style="margin:0px;padding:0px;border:0px">==</span> <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">null</span><span class="p" style="margin:0px;padding:0px;border:0px">)</span></div>
<div class="line" id="LC207" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">throw</span> <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">new</span> <span class="nf" style="margin:0px;padding:0px;border:0px;color:rgb(153,0,0);font-weight:bold">ArgumentException</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="s" style="margin:0px;padding:0px;border:0px;color:rgb(221,17,68)">"Wrong IAsyncResult."</span><span class="p" style="margin:0px;padding:0px;border:0px">,</span> <span class="s" style="margin:0px;padding:0px;border:0px;color:rgb(221,17,68)">"asyncResult"</span><span class="p" style="margin:0px;padding:0px;border:0px">);</span></div>
<div class="line" id="LC208" style="margin:0px;padding:0px 0px 0px 10px;border:0px"><br></div><div class="line" id="LC209" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                  <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">if</span> <span class="p" style="margin:0px;padding:0px;border:0px">(!</span><span class="n" style="margin:0px;padding:0px;border:0px">ares</span><span class="p" style="margin:0px;padding:0px;border:0px">.</span><span class="n" style="margin:0px;padding:0px;border:0px">IsCompleted</span><span class="p" style="margin:0px;padding:0px;border:0px">)</span></div>
<div class="line" id="LC210" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                <span class="n" style="margin:0px;padding:0px;border:0px">ares</span><span class="p" style="margin:0px;padding:0px;border:0px">.</span><span class="n" style="margin:0px;padding:0px;border:0px">AsyncWaitHandle</span><span class="p" style="margin:0px;padding:0px;border:0px">.</span><span class="n" style="margin:0px;padding:0px;border:0px">WaitOne</span> <span class="p" style="margin:0px;padding:0px;border:0px">();</span></div>
<div class="line" id="LC211" style="margin:0px;padding:0px 0px 0px 10px;border:0px"><br></div><div class="line" id="LC212" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                  <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">lock</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">wait_queue</span><span class="p" style="margin:0px;padding:0px;border:0px">)</span> <span class="p" style="margin:0px;padding:0px;border:0px">{</span></div>
<div class="line" id="LC213" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                <span class="kt" style="margin:0px;padding:0px;border:0px;color:rgb(68,85,136);font-weight:bold">int</span> <span class="n" style="margin:0px;padding:0px;border:0px">idx</span> <span class="p" style="margin:0px;padding:0px;border:0px">=</span> <span class="n" style="margin:0px;padding:0px;border:0px">wait_queue</span><span class="p" style="margin:0px;padding:0px;border:0px">.</span><span class="n" style="margin:0px;padding:0px;border:0px">IndexOf</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">ares</span><span class="p" style="margin:0px;padding:0px;border:0px">);</span></div>
<div class="line" id="LC214" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">if</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">idx</span> <span class="p" style="margin:0px;padding:0px;border:0px">>=</span> <span class="m" style="margin:0px;padding:0px;border:0px;color:rgb(0,153,153)">0</span><span class="p" style="margin:0px;padding:0px;border:0px">)</span></div>
<div class="line" id="LC215" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                                        <span class="n" style="margin:0px;padding:0px;border:0px">wait_queue</span><span class="p" style="margin:0px;padding:0px;border:0px">.</span><span class="n" style="margin:0px;padding:0px;border:0px">RemoveAt</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">idx</span><span class="p" style="margin:0px;padding:0px;border:0px">);</span></div>
<div class="line" id="LC216" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                        <span class="p" style="margin:0px;padding:0px;border:0px">}</span></div><div class="line" id="LC217" style="margin:0px;padding:0px 0px 0px 10px;border:0px">
<br></div><div class="line" id="LC218" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                  <span class="n" style="margin:0px;padding:0px;border:0px">HttpListenerContext</span> <span class="n" style="margin:0px;padding:0px;border:0px">context</span> <span class="p" style="margin:0px;padding:0px;border:0px">=</span> <span class="n" style="margin:0px;padding:0px;border:0px">ares</span><span class="p" style="margin:0px;padding:0px;border:0px">.</span><span class="n" style="margin:0px;padding:0px;border:0px">GetContext</span> <span class="p" style="margin:0px;padding:0px;border:0px">();</span></div>
<div class="line" id="LC219" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                        <span class="n" style="margin:0px;padding:0px;border:0px">context</span><span class="p" style="margin:0px;padding:0px;border:0px">.</span><span class="n" style="margin:0px;padding:0px;border:0px">ParseAuthentication</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">SelectAuthenticationScheme</span> <span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">context</span><span class="p" style="margin:0px;padding:0px;border:0px">));</span></div>
<div class="line" id="LC220" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                        <span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">return</span> <span class="n" style="margin:0px;padding:0px;border:0px">context</span><span class="p" style="margin:0px;padding:0px;border:0px">;</span> <span class="c1" style="margin:0px;padding:0px;border:0px;color:rgb(153,153,136);font-style:italic">// This will throw on error.</span></div>
<div class="line" id="LC221" style="margin:0px;padding:0px 0px 0px 10px;border:0px">                <span class="p" style="margin:0px;padding:0px;border:0px">}</span></div><div><span class="p" style="margin:0px;padding:0px;border:0px"><br>
</span></div></pre><div><br></div>-- <br>Le doute n'est pas une condition agréable, mais la certitude est absurde.<br>
</div>