Monday, July 21, 2008

ContextSwitchDeadlock was detected

So I was creating a datamining executable in .net that would query a bunch of tables and crunch the data into a new report. While running it in Visual Studio in debug mode, I got a message...

"No Symbols are loaded for any call stack frame. The source code cannot be displayed."







Followed by...

"ContextSwitchDeadlock was detected"

















Here is why that happens....
When you are in debug mode, the CLR has some managed debug assistants (MDA's) that run around looking for bugs (or potential bugs in your code). The ContextSwitchDeadlock MDA behaves like this: a background thread runs that will (every 60 seconds) attempt to enter the STA that your code is running in. The way STA works is that a message based protocol is used to marshal calls. So in order for the MDA to enter, the marshalling message must get processed--which requires that a GetMessage or PeekMessage call be executed on the STA thread in that timeframe.

This could be caused by an infinite loop, or just a long operation. For example, you can trigger this MDA if you try to synchronously communicate with a database & end up blocking while you wait for a long query to complete.


To fix this problem: Unless you are in an infinite loop, there is nothing wrong with your code. It's just taking a long time. Don't let any pussies tell you, "well you can optimize it, blah, blah" punk motherfucker.

Go to Visual Studio... Debug, Exceptions, and expand

Managed Debugging Assistants.

Uncheck.. "ContextSwitchDeadlock"










No comments: