Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat could be causing a deadlock or otherwise causing this concurrency test to inconsistently fail?
    primarykey
    data
    text
    <p>I've been fiddling around with <a href="http://research.microsoft.com/en-us/projects/chess/" rel="nofollow noreferrer">CHESS</a>, which seems like an incredibly useful tool. However, ironically, I seem to be dealing with a Heisenbug in one of my test methods. The results reported by CHESS when I run this test are unpredictable:</p> <ul> <li>Sometimes the test will pass</li> <li>Sometimes the test will fail, with no further description (simply: "Test failed")</li> <li>Sometimes the test will fail with instructions to duplicate*</li> <li>Sometimes the test will indicate "CHESS detected deadlock"</li> </ul> <p>Initially, I thought this inconsistency must be due to the fact that the test involves the use of <code>Random</code> objects. It must have been that different seed values were yielding different outcomes, right?</p> <p>So I updated the test to simply run for a predefined set of seed values (0 to 10). Thread-local <code>Random</code> objects get seeded by a (pseudo-)random value produced by a shared <code>Random</code> within a lock. The code looks <strike>basically</strike> <strong>exactly</strong> like this:</p> <p><img src="https://i.stack.imgur.com/hzk82.png" alt="Screenshot of CHESS reporting a deadlock"></p> <p>(<strong>Update</strong>: I am running this on .NET 3.5, as CHESS only supports VS 2008. I wonder if the problem could have something to do with <a href="http://blogs.msdn.com/b/ericlippert/archive/2009/03/06/locks-and-exceptions-do-not-mix.aspx" rel="nofollow noreferrer">this</a>?)</p> <p>As I understand it, the above code should actually be pretty deterministic. Since <code>sharedRandom</code> is initialized with a known seed (between 0 and 10), the values produced by the <code>localRandom</code> object belonging to each thread running the code inside the <code>Parallel.For</code> call should be consistent from one test run to the next (which thread gets which seed from <code>sharedRandom</code> may differ between runs, but among the 5 iterations within <code>Parallel.For</code>, the same 5 seeds should be used for <code>localRandom</code>).</p> <p>That's how <em>I</em> understand it. But from the CHESS results, I'm inclined to believe I must be missing something.</p> <ol> <li>Is there a deadlock in the above code that I'm too dumb to see?</li> <li>Should I not be using the <code>Random</code> class in concurrency-related tests?</li> <li>For those who have experience using CHESS: is it a reliable tool? Does it sometimes give false positives? This is actually a big one, as if it turns out that this scenario is common (inconsistent test results), then perhaps it'd be advisable for me to hold off on using CHESS at all for the time being.</li> </ol> <p><sub>*...which I haven't been able to figure out how to use -- but that's a separate issue.</sub></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload