[Mono-list] Serious bug in lock statement

Nikki Locke nikki at trumphurst.com
Tue Jun 13 10:30:03 EDT 2006


I have reproduced a situation where an Exception is thrown inside a lock 
statement, and the statement is exited without releasing the lock.

I have reported it in 

http://bugzilla.ximian.com/show_bug.cgi?id=78633

Here is the code...

using System;
using System.Threading; 

namespace SpecSoft.Lib {

  public class Test {

    private static Object _error = new Object();

    public static void Main() {
      Thread _errorThread = new Thread(new ThreadStart(errorThread));

      _errorThread.IsBackground = true;
      _errorThread.Name = "Error reporting thread";
      _errorThread.Start();
      System.Console.WriteLine("Main thread initial sleep");
      Thread.Sleep(2000);
      System.Console.WriteLine("Trying");
      System.Console.WriteLine("Trying to lock error {0}", _error.GetHashCode());
      lock(_error) {
        System.Console.WriteLine("Locked error");
      }
      System.Console.WriteLine("Unlocked error");
    }

    private static void errorThread() {
      System.Console.WriteLine("Error thread initial sleep");
      Thread.Sleep(1000);
      for(int i = 0; i < 10; i++) {
        NextError();
        Thread.Sleep(1000);
      }
      System.Console.WriteLine("Error thread exiting");
    }

    public static string NextError() {
      try {
        lock(_error) {
          string [] files = new string[1];

          return files[2];    // Throws an exception
        }
      } catch {
      }
      System.Console.WriteLine("Returning null from NextError");
      return null;
    }

  }
}


-- 
Nikki Locke, Trumphurst Ltd.      PC & Unix consultancy & programming
http://www.trumphurst.com/




More information about the Mono-list mailing list