1

I am running into a strange behavior of my C# application.

I am running the application in debug mode via VS2010 and after several hours the application just waits for a lock release but the only thread which is alive is the one that waits upon the lock to be released and there is not recursion in that function:

class ProductionNode        {  
   private readonly object _lock = new object();  
   public bool Activate( long jobId )  
   {  
       lock(_lock) // Doesn't go through here  
       {  
         return DAL.InsertJobIfNotExists(jobId)>0; //SQL PLAIN INSERT IF NOT EXIST COMMAND  
       }  
  }
  public void HasJobs()  
  {  
       lock(_lock)
       {
           return DAL.HasProductionJobs();
       }  
  }
 }    

Again, when I am pausing the application using the VS2010 the only thread that uses the ProductionNode is the one that waits for the _lock object to be released.

To make things clear, the application might activate the production node in several threads but the in the given scenario where the deadlock occur the VS only displays a single thread which uses the ProductionNode object, the other threads uses other object types.

Any ideas?

Cheers, Doron

DoronBM
  • 515
  • 1
  • 6
  • 17

2 Answers2

3

I'd recommend using windbg (Dbugging Tools for Windows, included in the Windows SDK) and psscor2 to debug into your problem a little deeper.

Basically, in windbg try to:

1 . Load psscor2:

.load C:\tools\psscor2\amd64\psscor2 [replace with the path where you put the psscor2 dll]

2 . Try finding which threads are holding what locks using

!syncblk

3 . Look at what threads are running and compare to the threads that are holding locks

!threads

4 . Switch between threads

~Xs *[where X is the thread id you got from !threads and that you are interested in because it's holding a lock]*

  1. look at the managed call stack of the current thread to find out what happened before the lock was acquired

!clrstack -a

EDIT:

Since you're presumably using .net 4, you might want to use SOSEX instead of psscor2. I haven't really used SOSEX so far, but I'd have a look at the deadlock command: !dlk

Ben Schwehn
  • 4,505
  • 1
  • 27
  • 45
1

Some hints that may help. I would go these steps:

  1. Right click on _lock and find all references using Visual Studio context menu. Maybe you are using the lock somewhere else (you did in your update).
  2. Make sure the lock is not static and ProductionNode class is not singleton
  3. It is possible your thread is trapped inside DAL.InsertJobIfNotExists so logging it's entrance and exit would help tracing the problem root.
Xaqron
  • 29,931
  • 42
  • 140
  • 205