41

Is there a tool that analyses .NET code and finds race conditions?

I have a bit of code that has a public static property that gets or creates a private static field. It also has a public static method that sets this field to null (...yes, I know!..)

As there are no locks around either of these methods, it's a safe bet that things'll go horribly wrong in the future. I need a tool that'll recursively go through things that call either of these methods and see if anything was spawned on another thread.

I'm looking for a tool or perhaps an nDepend SQL script (if this is possible).

Steve Dunn
  • 21,044
  • 11
  • 62
  • 87

6 Answers6

20

You're probably looking for one of these:


NOTE: This answer is from 2010. As with all recommendations answers, recommendations tend to change over time. There may be other products out there now, CHESS which was a Microsoft Research Labs project may have evolved into a final product or been scrapped altogether. Please take this answer with a grain of salt and conduct new research into which products are suitable now.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • Thanks Lasse, I'd heard of CHESS but not TypeMock Racer. I was really looking for a tool that did static analysis of the code. I'm using ReSharper 5 which has a nice feature that inspects code and shows all callers of a particular method and drills down through them recursively. What I need is something that'd flag a method as being instantiated on a worker thread. I'll investiage more on the CQL approach, as I know there's an upstream callers script, so I'm sure there's a way of finding out if any methods are the result of a thread invocation call. – Steve Dunn Mar 04 '10 at 14:00
  • 1
    This [fork of chess](https://github.com/LeeSanderson/Chess) seems to be up to date and working. – zejuel Feb 22 '18 at 16:40
4

Jinx will do this at runtime (not statically) but it may be worth looking at.

steve cook
  • 3,116
  • 3
  • 30
  • 51
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
  • Nice. Runtime analysis is far superior to static analysis of concurrency issues. There's just too much runtime convention for the static analyzers to produce a good signal-to-noise ratio. – Michael Donohue Mar 04 '10 at 14:25
  • looks like Jinx is toast. – tofutim Aug 18 '16 at 03:04
  • 1
    Wikipedia: Jinx was a concurrency debugger that deterministically controls the interleaving of workloads across processor cores, focusing on shared memory interactions. Using this deterministic approach, Jinx aimed to increase the frequency of occurrence of elusive shared memory bugs, sometimes called Heisenbugs. Jinx is no longer available. Corensic, the company that was developing Jinx, was bought by F5 Networks and the Jinx project was cancelled. – tofutim Aug 18 '16 at 03:06
4

I have been experimenting on how to easily track those. I've been working to track some deadlocks, specially on scenarios where many different lock statements are used.

My goal is to detect deadlocks before they happen, e.g. if you have two resources, you know you have to always use them in the same order, otherwise a deadlock might occur.

lock (lockObj1) 
lock (lockObj2) 
{ 
    // some code
} 

... somewhere else in the app ...

lock (lockObj2) 
lock (lockObj1) // <- I expect some "possible deadlock" detection here 
{ 
    // some code
} 

In this case I'm using lockObj1 then lockObj2 in one place and using them in the opposite order in another place, this is something you will like to avoid in an application Of course, lock statements don't need to be used one after the other like in the example, your complex application might have several complex objects interacting with each other

I have uploaded the code with the test cases here https://github.com/glmnet/LockTracer

Guillermo Ruffino
  • 2,940
  • 1
  • 26
  • 23
  • 2
    This is a very interesting idea. Inspired by your code I wrote something similar, assigning a "priority number" to each lock, and then checking that whenever I got multiple locks that I got them in "priority order". Sure enough, that immediately revealed that there was one place in my program where I'd broken my own rule about the ordering of lock acquisitions, and fixing that fixed my deadlock vulnerability. – RenniePet Sep 15 '15 at 23:09
  • 1
    That looks simpler, yet effective! Could you share it on GitHub? Don't forget to upvote if you didn't. Thanks! – Guillermo Ruffino Sep 16 '15 at 12:17
3

You might want to check out CHESS.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
2

See the answers here: What static analysis tools are available for C#?

Some static analysis tools can do deadlock detection.

Also, try FxCop from Microsoft.

Community
  • 1
  • 1
David d C e Freitas
  • 7,481
  • 4
  • 58
  • 67
0

Have you looked at Red-Gate Ants? I'm not sure if it will do everything you need but it is a good product to:

  • Identify performance bottlenecks within minutes
  • Optimize .NET application performance
  • Drill down to slow lines of code with line-level timings
  • Profile aspx, ASP.NET, C# code, and VB.NET applications
Keith Barrows
  • 24,802
  • 26
  • 88
  • 134