Intermittent RPC_E_DISCONNECTED from in-process cross apartment calls

  apartments, c++, com

I am struggling with COM functions returning RPC_E_DISCONNECTED intermittently when performing cross apartment function calls from a client to a COM server. Most of the time, the problematic calls returns S_OK as expected, but once in a while, we get the RPC_E_DISCONNECTED error.

This happens in a native C++ application with two single threaded apartments (in addition to numerous other unrelated threads), and the COM server is implemented using ATL and resides on the main thread that is also hosting C# code, but there is no managed code involved in the implementation of the problematic function on the server. Both client and server resides in the same process, so there is no network or inter-process communication going on here.

There are no uncaught exceptions on the server implementation when this happens, and the server recovers by itself. This means that after some retries, we are finally getting the expected return code from the same function and the same instance of the server as previously returned failure.

Has anyone experienced similar behavior, or could assist with some "psychic debugging" to shed some light on what could go on here? I suspect there could be some thread in the COM runtime that for some reason is starved, and fails to respond to some watchdog ping, but this is pure speculation.

Source: Windows Questions C++