title: Why is calling a Python lambda expression from C# not thread-safe?
body: <p>I define a side-effect-free (pure) lambda expression in IronPython and assign it to a C# delegate. When invoking the delegate simultaneously from multiple threads i get exceptions of type <strong>AccessViolationException</strong>, <strong>NullReferenceException</strong> and <strong>FatalEngineExecutionError</strong>.</p>
  
  <p>The occurance of the error is non-deterministic and it mostly takes several million iterations to provoke it, which says "race condition" to me. How can i avoid it?</p>
  
  <p>The exceptions are only raised when running the process with x64 (x86 does not crash) and outside of the debugger. The test system is a Core I7 (8 threads) on Windows 7, .NET Framework 4.0 and IronPython 2.7.1.</p>
  
  <p>Here's the minimal code to produce the error:</p>
  
  <pre><code>var engine = Python.CreateEngine();
  
  double a = 1.0;
  double b = 2.0;
  
  while (true)
  {
      Func&lt;double, double, double&gt; calculate = engine.Execute("lambda a,b : a+b");
  
      System.Threading.Tasks.Parallel.For(0, 1000, _ =&gt;
      {
           for (int i = 0; i &lt; 1000; i++) { calculate(a,b); }
      });
  
      Console.Write(".");   
  }
  </code></pre>
  
  <p>Error message:</p>
  
  <blockquote>
    <p>FatalExecutionEngineError was detected</p>
    
    <p>Message: The runtime has encountered a fatal error. The address of the error was at 0xf807829e, on thread 0x3da0. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.</p>
  </blockquote>
  
  <p>Update: Even if the engine is declared as thread-local, it crashes after some time:</p>
  
  <pre><code>var calculate = new ThreadLocal&lt;Func&lt;double, double, double&gt;&gt;(() =&gt; Python.CreateEngine().Execute("lambda a,b : a+b"));
  </code></pre>