title: F# MailboxProcessor memory leak in try/catch block
body: <p><strong>Updated</strong> after obvious error pointed out by John Palmer in the comments.</p>
  
  <p>The following code results in <code>OutOfMemoryException</code>:</p>
  
  <pre><code>let agent = MailboxProcessor&lt;string&gt;.Start(fun agent -&gt;
  
      let maxLength = 1000
  
      let rec loop (state: string list) i = async {
          let! msg = agent.Receive()
  
          try        
              printfn "received message: %s, iteration: %i, length: %i" msg i state.Length
              let newState = state |&gt; Seq.truncate maxLength |&gt; Seq.toList
              return! loop (msg::newState) (i+1)
          with
          | ex -&gt; 
              printfn "%A" ex
              return! loop state (i+1)
      }
  
      loop [] 0
  )
  
  let greeting = "hello"
  
  while true do
      agent.Post greeting
      System.Threading.Thread.Sleep(1) // avoid piling up greetings before they are output
  </code></pre>
  
  <p>The error is gone if I don't use try/catch block.</p>
  
  <p>Increasing the sleep time only postpones the error.</p>
  
  <p><strong>Update 2:</strong> I guess the issue here is that the function stops being tail recursive as the recursive call is no longer the last one to execute. Would be nice for somebody with more F# experience to desugar it as I'm sure this is a common memory-leak situation in F# agents as the code is very simple and generic.</p>