So, the obvious question for me is whether or not you&#39;re using any finalizers.<br><br><div class="gmail_quote">On Wed, Nov 5, 2008 at 5:57 AM, William Reade <span dir="ltr">&lt;<a href="mailto:william@resolversystems.com">william@resolversystems.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi all<br>
<br>
While running the numpy tests, I&#39;ve come across a situation which, to the best of my knowledge, is simply impossible. I&#39;m hoping that one of the local .NET gurus will be able to tell me what I&#39;m missing, or point me somewhere I can get more insight.<br>

<br>
The 4 methods involved are as follows:<br>
-----------------------<br>
 &nbsp; &nbsp; &nbsp; public int GetThreadId()<br>
 &nbsp; &nbsp; &nbsp; {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Thread.CurrentThread.ManagedThreadId;<br>
 &nbsp; &nbsp; &nbsp; }<br>
<br>
 &nbsp; &nbsp; &nbsp; public void WriteFlush(string info)<br>
 &nbsp; &nbsp; &nbsp; {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(info);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Out.Flush();<br>
 &nbsp; &nbsp; &nbsp; }<br>
<br>
 &nbsp; &nbsp; &nbsp; public void EnsureGIL()<br>
 &nbsp; &nbsp; &nbsp; {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Monitor.Enter(this.dispatcherLock);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.WriteFlush(String.Format(<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;EnsureGIL ({1}) {0}&quot;, this.GetThreadId(), Builtin.id(this.dispatcherLock)));<br>
 &nbsp; &nbsp; &nbsp; }<br>
<br>
 &nbsp; &nbsp; &nbsp; public void ReleaseGIL()<br>
 &nbsp; &nbsp; &nbsp; {<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.WriteFlush(String.Format(<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;ReleaseGIL ({1}) {0}\n&quot;, this.GetThreadId(), Builtin.id(this.dispatcherLock)));<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Monitor.Exit(this.dispatcherLock);<br>
 &nbsp; &nbsp; &nbsp; }<br>
-----------------------<br>
...and they can, and do, occasionally produce output as follows:<br>
-----------------------<br>
EnsureGIL (443) 2<br>
EnsureGIL (443) 1 &nbsp; &nbsp; &nbsp;&lt;- omg, wtf, bbq, etc.<br>
ReleaseGIL (443) 2<br>
<br>
EnsureGIL (443) 2<br>
ReleaseGIL (443) 1<br>
<br>
ReleaseGIL (443) 2<br>
-----------------------<br>
When this happens, the process continues happily for a short time and then falls over in a later call to ReleaseGIL (after successfully calling it several times). The error is &quot; Object synchronization method was called from an unsynchronized block of code&quot;, which I understand to mean &quot;you can&#39;t release this lock because you don&#39;t hold it&quot;.<br>

<br>
It doesn&#39;t happen very often, but I can usually reproduce it by running test_multiarray.TestFromToFile.test_malformed a few hundred times. It may be relevant to note that thread 2 is the GC thread, and thread 1 is the main thread. I have considered the following possibilities:<br>

<br>
(1) That I&#39;m locking on the wrong object. I believe that isn&#39;t the case, because it&#39;s constructed only once, as a &quot;new Object()&quot; (ie, a reference type), and is only subsequently used for locking; and, because it keeps the same ipy id throughout.<br>

<br>
(2) That Monitor.Enter occasionally allows two different threads to acquire the same lock. I consider this extremely unlikely, because... well, how many multithreaded .NET apps already exist? If Monitor really were broken, I think we&#39;d probably know about it by now.<br>

<br>
(3) That calling Flush() on a SyncTextWriter (the type of Console.Out) doesn&#39;t actually do anything, and the output is somehow wrongly ordered (although I can&#39;t imagine how this could actually be: if the locking is really working, then my console writes are strictly sequential). I don&#39;t have access to the code, so I have no idea how it&#39;s implemented, but even if this is the case it doesn&#39;t help much with the fundamental problem (the synchronisation error which follows).<br>

<br>
Apart from the above, I&#39;m out of ideas. Can anyone suggest what I&#39;ve missed?<br>
<br>
William<br>
_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br>
<a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
</blockquote></div><br>