dangerousThreadingAPI MDA

Note

This article is specific to .NET Framework. It doesn't apply to newer implementations of .NET, including .NET 6 and later versions.

The dangerousThreadingAPI managed debugging assistant (MDA) is activated when the Thread.Suspend method is called on a thread other than the current thread.

Symptoms

An application is unresponsive. System or application data might be left in an unpredictable state temporarily or even after an application has been shut down. Some operations are not completing as expected.

Symptoms can vary widely due to the randomness inherent to the problem.

Cause

A thread is asynchronously suspended by another thread using the Suspend method. There is no way to determine when it is safe to suspend another thread which might be in the middle of an operation. Suspending the thread can result in the corruption of data or the breaking of invariants. Should a thread be placed into a suspended state and never resumed using the Resume method, the application can stop responding and possibly damage application data. These methods have been marked as obsolete.

If synchronization primitives are held by the target thread, they remain held during suspension. This can lead to deadlocks should another thread, for example the thread performing the Suspend, attempt to acquire a lock on the primitive. In this situation, the problem manifests itself as a deadlock.

Resolution

Avoid designs that require the use of Suspend and Resume. For cooperation between threads, use synchronization primitives such as Lock, Monitor, ReaderWriterLock, Mutex, or the C# lock statement. If you must use these methods, reduce the window of time and minimize the amount of code that executes while the thread is in a suspended state.

Effect on the Runtime

This MDA has no effect on the CLR. It only reports data about dangerous threading operations.

Output

The MDA identifies the dangerous threading method that caused it to be activated.

Configuration

<mdaConfig>
  <assistants>
    <dangerousThreadingAPI />
  </assistants>
</mdaConfig>

Example

The following code example demonstrates a call to the Suspend method that causes the activation of the dangerousThreadingAPI.

using System.Threading;
void FireMda()
{
Thread t = new Thread(delegate() { Thread.Sleep(1000); });
    t.Start();
    // The following line activates the MDA.
    t.Suspend();
    t.Resume();
    t.Join();
}

See also