Finding Deadlocks with ReaderWriterLock and Typemock Racer

This feature was just added to the Racer Alpha.

Given the following method:

WindowClipping (14)

Can you see a potential for deadlock in this method?

Running the latest build of Typemock Racer using the following test should reveal an answer:

WindowClipping (15)

 

When running this (using TestDriven.NET) we get a DeadlockFoundException, with the following listing:

WindowClipping (16)

basically, if two threads were to run this method, the following could happen:

- Both threads will have a reader lock acquired

- one thread will, inside a lock on sync, try to upgrade to a write lock, but will block because the other thread is still holding a reader lock (no readers should lock for writers to be used)

- the other thread, at the same time, is trying to lock sync as well, but is waiting for the first thread to released the locked sync.

since the first thread is waiting for the second thread to release the reader lock, and the second thread is waiting for the first thread to release the lock on sync, a deadlock occurs.

Ayende, how about trying this thing out on your Deadlock Challenge?

  • Omer Mor

    I think the monitor lock ("lock (sync)") is not needed for this deadlock to occur. A simple "acquire read" –> "acquire write" approach would suffice.

    – Omer

  • Anonymous

    Omer,
    the lock() is required for a deadlock so that one thread will block waiting for the other one to release.

  • Omer Mor

    It will block because it waits for the other thread to release its reader lock. No need for a second lock.

TOP