ContextSwitchDeadlock Exception in the Coded UI Tests
Coded UI test, a new type of test in Visual Studio 2010, can be created to navigate through an application’s User Interface (UI), which can be useful to verify that the functionalities users might perform through an application are working properly. As such, coded UI tests can bring the improvement of regression testing capabilities to the UI layer, especially for time consuming scenarios.
I have tried to create a coded UI test on such an application scenario, which performs an upload of a significant number of documents and because of that it takes a couple of minutes to be finished. In order to find a moment when the upload is completed, I created a loop in the code which verifies Enabled property of one of the UI objects every 2 seconds. The problem was that Visual Studio occasionally fired MDA ContextSwitchDeadlock exception:
ContextSwitchDeadlock was detected
Message: The CLR has been unable to transition from COM context 0x2e33a0 to COM context 0x2e35c8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.
A Managed Debugging Assistant (MDA), ContextSwitchDeadlock, was created to monitor for deadlocks induced by cross-Apartment transitions. If a transition takes longer than 60 seconds to complete, the CLR assumes the receiving STA is deadlocked and fires this MDA. Processing messages during long operations can be paused or even failed due to many different factors such as CPU, available RAM, context switching, as well as other factors…
What is really positive with MDA is that the message isn’t a test stopper; there is no real loss in ignoring it. The MDA is fired because my loop runs for longer than 60 seconds. Essentially, it is a warning that a code is completely blocking the application.
The resolution can be:
- If you want to ignore the MDA, simply uncheck the ContextSwitchDeadlock exception (Debug> Exceptuions> Managed Debugging Assistants > ContextSwitchDeadlock, uncheck column Thrown). This can be considered as “I know that test/application is going to be busy for a significant amount of time and it will appear as not responding“. Unfortunately, this will open the possibility that a test or application is really hung.
- If a ContextSwitchDeadlock message is important for you for any reason, the problem can be resolved by pumping messages at the end of each pass through the loop:
while (PeekMessage(…))
{
TranslateMessage(…);
DispatchMessage(…);
}
This will clear the box of any unprocessed messages that arrive while a loop is running.
April 2nd, 2010 at 2:57 am
[...] processing on the messages … Name (required) Mail (will not be published) (required) Website …ContextSwitchDeadlock Exception in the Coded UI Tests …Coded UI test, a new type of test in Visual Studio 2010, can be created to navigate through an [...]