M
Michael Kennedy
Hi,
I have been looking into the claim that the keyword lock is not safe
when exceptions are possible. That lead me to try the following code,
which I think has uncovered a serious error in the .NET 2.0 framework.
Note that this runs better, but not perfectly, on .NET 1.1.
Note: The numbers are line numbers needed to match the callstack of the
exception below.
// Class WorkItem ...
34 public void DoItSafeLock()
35 {
36 try
37 {
38 Monitor.Enter(this);
39 Thread.Sleep(10);
40 }
41 finally
42 {
43 Monitor.Exit(this); // <-- SynchronizationLockException!
44 }
45 }
Here's what I get when I run this code on a different thread than the
one that created the object holding this method (WorkItem class):
System.Threading.SynchronizationLockException: Object synchronization
method was called from an unsynchronized block of code.
at WorkItems.WorkItem.DoItSafeLock() in
D:\LockTest\WorkItems\WorkItem.cs:line 43
at WorkItems.Worker.ExecuteWorkItem() in
D:\LockTest\WorkItems\Worker.cs:line 127
Notice that the error is that on line 43, the object does not hold a
lock on itself (this). How is this possible given that it made it
through the Monitor.Enter(this) call on line 38?!?
Does any one know what's going on here? How can the monitor be entered
and yet no lock is acquired?
I have a sample application demonstrating the error if any one wants to
see the code, it can be download here:
http://www.unitedbinary.com/TEMP-001-2005-05-28/LockTest.zip
Thanks in advance,
Michael
Michael Kennedy
PS - This has been reposted to these newsgroups. It was originally
posted to the C# newsgroup only.
I have been looking into the claim that the keyword lock is not safe
when exceptions are possible. That lead me to try the following code,
which I think has uncovered a serious error in the .NET 2.0 framework.
Note that this runs better, but not perfectly, on .NET 1.1.
Note: The numbers are line numbers needed to match the callstack of the
exception below.
// Class WorkItem ...
34 public void DoItSafeLock()
35 {
36 try
37 {
38 Monitor.Enter(this);
39 Thread.Sleep(10);
40 }
41 finally
42 {
43 Monitor.Exit(this); // <-- SynchronizationLockException!
44 }
45 }
Here's what I get when I run this code on a different thread than the
one that created the object holding this method (WorkItem class):
System.Threading.SynchronizationLockException: Object synchronization
method was called from an unsynchronized block of code.
at WorkItems.WorkItem.DoItSafeLock() in
D:\LockTest\WorkItems\WorkItem.cs:line 43
at WorkItems.Worker.ExecuteWorkItem() in
D:\LockTest\WorkItems\Worker.cs:line 127
Notice that the error is that on line 43, the object does not hold a
lock on itself (this). How is this possible given that it made it
through the Monitor.Enter(this) call on line 38?!?
Does any one know what's going on here? How can the monitor be entered
and yet no lock is acquired?
I have a sample application demonstrating the error if any one wants to
see the code, it can be download here:
http://www.unitedbinary.com/TEMP-001-2005-05-28/LockTest.zip
Thanks in advance,
Michael
Michael Kennedy
PS - This has been reposted to these newsgroups. It was originally
posted to the C# newsgroup only.