https://bugzilla.novell.com/show_bug.cgi?id=662381

https://bugzilla.novell.com/show_bug.cgi?id=662381#c0


           Summary: C# lock () fails on latest Mono runtimes
    Classification: Mono
           Product: Mono: Runtime
           Version: 2.8.x
          Platform: x86-64
        OS/Version: Ubuntu
            Status: NEW
          Severity: Critical
          Priority: P5 - None
         Component: misc
        AssignedTo: mono-bugs@lists.ximian.com
        ReportedBy: john.coste...@petermac.org
         QAContact: mono-bugs@lists.ximian.com
          Found By: ---
           Blocker: ---


Created an attachment (id=406983)
 --> (http://bugzilla.novell.com/attachment.cgi?id=406983)
ImageResize solution including executable

User-Agent:       Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.10
(KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10

The lock () construct fails to fully lock on Mono 2.8.1 and the latest daily
build of Mono (2011-01-04): frequently two or three threads are detected inside
the locked section of code at the same time. 

The same .exe file running on Windows XP 64-bit with .NET 4.0 does NOT exhibit
this bug at all.

I am primarily using Mono 2.8.1 and MonoDevelop 2.4.1 built in a parallel
environment from the stable tarball sources. 

The CPU is a 64-bit quad-core (which appears as 8 CPUs to the system).

I have also tested the executable with the latest (2011-01-04) Mono built from
the source tarball, and the same bug occurs.

I am using TPL heavily in C#.

The attached solution is a toy application that resizes an image. 

In the file Magic.cs is the following method:


        public static ResamplerSet GetResamplerSet(IFull o)
        {
            // For the resampler set we will find or create.
            ResamplerSet resamplerSet;

            // Compute the key.
            var key = ComputeKey(o);

            // Lock the resampler sets. Block all threads at this point because
if we need to create it, the constructor does so in parallel.
            lock (resamplerSets)
            {
                // See if we can get a resampler set with the correct source
step value and sufficient destination dimension.
                if (!resamplerSets.TryGetValue(key, out resamplerSet) ||
resamplerSet.MaximumDestinationDimension < o.DestinationDimension)
                {
                    // Failed for one or the other reason. Create or recreate
it now.
                    resamplerSet = new ResamplerSet(o);

                    // *** MONO BUG CHECK ***
                    if (resamplerSets.ContainsKey(key)) Console.WriteLine("***
MONO BUG: KEY " + key + " ALREADY POPULATED BY ANOTHER THREAD VIOLATING THE
LOCK ***");

                    // Store it.
                    resamplerSets[key] = resamplerSet;
                }
            }

            // Return the resampler set we found or created.
            return resamplerSet;
        }
        static Dictionary<KeyValuePair<double, double>, ResamplerSet>
resamplerSets = new Dictionary<KeyValuePair<double, double>, ResamplerSet>();


The constructor in 'new ResamplerSet(o)' does NOT call this method
re-entrantly.

The line with "MONO BUG CHECK" should never be printed to the console. However,
running this program on arbitrary image files with arbitrary rescaling factors
frequently causes it to be emitted once per program execution.

I asked a question relating to this code on the MSDN forum and a member
confirmed that the lock should prevent this critical race. 

As I said, it never occurs when running the same executable under Windows.


Reproducible: Sometimes

Steps to Reproduce:
1. Use a Mono environment supporting .NET 4.0.
2. Get some arbitrary bitmap images (BMP, JPG, PNG, any size).
3. Get the executable ImageResize.exe from the attached tar.gz (or build the
solution with MonoDevelop) and then try the following (assume your image is
foo.bmp):

mono ImageResize.exe foo.bmp bar.bmp 0.23 0.45

If you don't get any console output after running this a few times, change the
rescale factors arbitrarily to other numbers. 

I get the bug appearing roughly one out of every two times I execute the
program, on average.
Actual Results:  
Here is an example run:

[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
*** MONO BUG: KEY [2.89855072463768, 0.949275362318841] ALREADY POPULATED BY
ANOTHER THREAD VIOLATING THE LOCK ***
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
*** MONO BUG: KEY [2.89855072463768, 0.949275362318841] ALREADY POPULATED BY
ANOTHER THREAD VIOLATING THE LOCK ***
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
*** MONO BUG: KEY [2.89855072463768, 0.949275362318841] ALREADY POPULATED BY
ANOTHER THREAD VIOLATING THE LOCK ***
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
*** MONO BUG: KEY [4.27350427350427, 1.63675213675214] ALREADY POPULATED BY
ANOTHER THREAD VIOLATING THE LOCK ***
[mono-dev] ~/temp @ mono ImageResize.exe little.bmp bar.bmp 0.234 0.345
*** MONO BUG: KEY [4.27350427350427, 1.63675213675214] ALREADY POPULATED BY
ANOTHER THREAD VIOLATING THE LOCK ***
*** MONO BUG: KEY [2.89855072463768, 0.949275362318841] ALREADY POPULATED BY
ANOTHER THREAD VIOLATING THE LOCK ***
[mono-dev] ~/temp @ 


Expected Results:  
No "MONO BUG" output.

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.
_______________________________________________
mono-bugs maillist  -  mono-bugs@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-bugs

Reply via email to