title: Cannot access a disposed object: System.Threading.ManualResetEvent
body: <p>I have a small application which starts up several <code>async</code> workflows that read from and write to Kafka topics.  When I start up more than 3 or 4 of these async processes I get the following error:</p>
  
  <pre><code>System.ObjectDisposedException: Cannot access a disposed object.
  Object name: 'System.Threading.ManualResetEvent'.
  </code></pre>
  
  <p>I can't figure out what's causing this exception to get thrown or what resource is actually triggering it.  I do not use a ManualResetEvent anywhere in my code.  The program works for 3 or for async tasks but when I try more the exception is thrown so it's not consistent as to what triggers it.  There is also nothing to point to where in my code the exception is coming from.</p>
  
  <p>This is the code used to create my async task:</p>
  
  <pre><code>let service name readsFrom writeTo (delay:int) =
  let producer = createKafkaProducer name
  let subscription = createKafkaSubscriber name readsFrom
  async{
      printfn "Starting %s" name
      try
          subscription |&gt; Seq.iter
              (fun m -&gt; 
                  try
                      let e = m.Message
                      // biz logic
                      producer.Send(new Kafka.Client.Producers.KeyedMessage&lt;string,string&gt;(t, message))
                      Thread.Sleep 500 
                   with
                   | e -&gt; printfn "%s" (e.Message))
        with
        | e -&gt; printfn "%s" (e.Message)}
  </code></pre>
  
  <p>The stack trace for the exception is as follows, event with try/catch blocks around all of the async logic this is all I am able to get.  I believe that this may be an issue with the jroland Kafka client library I am using:</p>
  
  <pre><code>Unhandled Exception:
  System.ObjectDisposedException: Cannot access a disposed object.
  Object name: 'System.Threading.ManualResetEvent'.
    at System.Threading.WaitHandle.CheckDisposed () &lt;0x7fac94651690 + 0x00050&gt; in &lt;filename unknown&gt;:0
    at System.Threading.EventWaitHandle.Set () &lt;0x7fac9464b490 + 0x0003a&gt; in &lt;filename unknown&gt;:0
    at (wrapper remoting-invoke-with-check) System.Threading.EventWaitHandle:Set ()
    at System.Net.Sockets.SocketAsyncResult.set_IsCompleted (Boolean value) &lt;0x414118b0 + 0x00112&gt; in &lt;filename unknown&gt;:0
    at System.Net.Sockets.SocketAsyncResult.Complete () &lt;0x414114a0 + 0x000df&gt; in &lt;filename unknown&gt;:0
    at System.Net.Sockets.SocketAsyncResult.Complete (System.Exception e) &lt;0x41411420 + 0x0004b&gt; in &lt;filename unknown&gt;:0
    at System.Net.Sockets.SocketAsyncWorker.Connect () &lt;0x41410d80 + 0x003eb&gt; in &lt;filename unknown&gt;:0
    at System.Net.Sockets.SocketAsyncWorker.DispatcherCB (System.Net.Sockets.SocketAsyncResult sar) &lt;0x41410ab0 + 0x000af&gt; in &lt;filename unknown&gt;:0
    at (wrapper managed-to-native) System.Runtime.Remoting.Messaging.AsyncResult:Invoke (System.Runtime.Remoting.Messaging.AsyncResult)
    at System.Net.Sockets.SocketAsyncResult.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () &lt;0x41410760 + 0x00024&gt; in &lt;filename unknown&gt;:0
    at System.Threading.ThreadPoolWorkQueue.Dispatch () &lt;0x7fac944b3e50 + 0x001d6&gt; in &lt;filename unknown&gt;:0
    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () &lt;0x7fac944b56c0 + 0x00008&gt; in &lt;filename unknown&gt;:0
  </code></pre>
  
  <p>This behavior would seem to violate my understanding of how closures work within .Net.  Because the object is used within a lambda, shouldn't it be effectively captured within the closure and kept alive until the lambda is disposed of?</p>