title: 0x80010100: System call failed" exception, ContextSwitchDeadlock
body: <p>Long story short: in a C# application that works with COM inproc-server (dll), I encounter "0x80010100: System call failed" exception, and in debug mode also ContextSwitchDeadlock exception.</p>
  
  <p>Now more in details:</p>
  
  <p>1) C# app initializes STA, creates a COM object (registered as "Apartment"); then in subscribes to its connection-point, and begins working with the object.</p>
  
  <p>2) At some stage the COM object generates a lot of events, passing as an argument a very big collection of COM objects, which are created in the same apartment.</p>
  
  <p>3) The event-handler on C# side processes the above collection, occasionally calling some methods of the objects. At some stage the latter calls begin to fail with the above exceptions.</p>
  
  <p>On the COM side the apartment uses a hidden window whose winproc looks like this:</p>
  
  <pre><code>typedef std::function&lt;void(void)&gt; Functor;
  LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)   
  {   
    switch(msg)   
    {   
      case AM_FUNCTOR:
      {
        Functor *f = reinterpret_cast&lt;Functor *&gt;(lParam);
        (*f)();
        delete f;
      }
      break;   
      case WM_CLOSE:   
        DestroyWindow(hwnd);   
      break;   
      default:   
        return DefWindowProc(hwnd, msg, wParam, lParam);   
    }   
    return 0;   
  } 
  </code></pre>
  
  <p>The events are posted to this window from other parts of the COM server:</p>
  
  <pre><code>void post(const Functor &amp;func)
  {
    Functor *f = new Functor(func);
    PostMessage(hWind_, AM_FUNCTOR, 0, reinterpret_cast&lt;LPARAM&gt;(f));
  }
  </code></pre>
  
  <p>The events are standard ATL CP implementations bound with the actual params, and they boil down to something like this:</p>
  
  <pre><code>pConnection-&gt;Invoke(id, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &amp;params, &amp;varResult, NULL, NULL);
  </code></pre>
  
  <p>In C# the handler looks like this:</p>
  
  <pre><code>private void onEvent(IMyCollection objs)
  {
    int len = objs.Count; // usually 10000 - 25000
    foreach (IMyObj obj in objs)
    {
      // some of the following calls fail with 0x80010100
      int id = obj.id;
      string name = obj.name;
      // etc...
    }
  }
  </code></pre>
  
  <p>==================</p>
  
  <p>So, can the above problem happen just because the message-queue of the apartment is too loaded with the events it tries to deliver? Or the message loop should be totally blocked to cause such a behaviour?</p>
  
  <p>Lets assume that the message-queue has 2 sequential events that evaluate to "onEvent" call. The first one enters C# managed code, which attempts to re-enter the unmanaged code, the same apartment. Usually, this is allowed, and we do this a lot. When, under what circumstances can it fail?</p>
  
  <p>Thanks.</p>