<div>Locking during finalization is often considered to be a bad idea. In particular, locking without a timeout introduces the possibility that you will hang the finalization thread, preventing further objects from being finalized. But clearly, that's not what's happening here.</div>
<div><br></div><div>Other questions that probably don't matter but might be interesting to know:</div><div><br></div><div>Can we assume that the finalization thread isn't the first place where this lock is required? That your log starts somewhere in the middle?</div>
<div><br></div><div>Is this under x86 or x64 or both?</div><div><br></div><div>Are you creating any additional AppDomains in the process?</div><div><br></div><br><div class="gmail_quote">On Wed, Nov 5, 2008 at 10:15 AM, William Reade <span dir="ltr"><<a href="mailto:william@resolversystems.com">william@resolversystems.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi Curt<br>
<br>
I am indeed; that's how I know thread 2 is the GC thread. Is locking during GC forbidden?<br>
<br>
William<br>
<br>
Curt Hagenlocher wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="Ih2E3d">
...or, for that matter, any __del__ methods from within Python -- which ultimately are handled by finalization.<br>
<br></div><div class="Ih2E3d">
On Wed, Nov 5, 2008 at 9:37 AM, Curt Hagenlocher <<a href="mailto:curt@hagenlocher.org" target="_blank">curt@hagenlocher.org</a> <mailto:<a href="mailto:curt@hagenlocher.org" target="_blank">curt@hagenlocher.org</a>>> wrote:<br>
<br>
So, the obvious question for me is whether or not you're using any<br>
finalizers.<br>
<br>
<br>
On Wed, Nov 5, 2008 at 5:57 AM, William Reade<br></div>
<<a href="mailto:william@resolversystems.com" target="_blank">william@resolversystems.com</a> <mailto:<a href="mailto:william@resolversystems.com" target="_blank">william@resolversystems.com</a>>><div><div>
</div><div class="Wj3C7c"><br>
wrote:<br>
<br>
Hi all<br>
<br>
While running the numpy tests, I've come across a situation<br>
which, to the best of my knowledge, is simply impossible. I'm<br>
hoping that one of the local .NET gurus will be able to tell<br>
me what I'm missing, or point me somewhere I can get more insight.<br>
<br>
The 4 methods involved are as follows:<br>
-----------------------<br>
public int GetThreadId()<br>
{<br>
return Thread.CurrentThread.ManagedThreadId;<br>
}<br>
<br>
public void WriteFlush(string info)<br>
{<br>
Console.WriteLine(info);<br>
Console.Out.Flush();<br>
}<br>
<br>
public void EnsureGIL()<br>
{<br>
Monitor.Enter(this.dispatcherLock);<br>
this.WriteFlush(String.Format(<br>
"EnsureGIL ({1}) {0}", this.GetThreadId(),<br>
Builtin.id(this.dispatcherLock)));<br>
}<br>
<br>
public void ReleaseGIL()<br>
{<br>
this.WriteFlush(String.Format(<br>
"ReleaseGIL ({1}) {0}\n", this.GetThreadId(),<br>
Builtin.id(this.dispatcherLock)));<br>
Monitor.Exit(this.dispatcherLock);<br>
}<br>
-----------------------<br>
...and they can, and do, occasionally produce output as follows:<br>
-----------------------<br>
EnsureGIL (443) 2<br>
EnsureGIL (443) 1 <- 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<br>
time and then falls over in a later call to ReleaseGIL (after<br>
successfully calling it several times). The error is " Object<br>
synchronization method was called from an unsynchronized block<br>
of code", which I understand to mean "you can't release this<br>
lock because you don't hold it".<br>
<br>
It doesn't happen very often, but I can usually reproduce it<br>
by running test_multiarray.TestFromToFile.test_malformed a few<br>
hundred times. It may be relevant to note that thread 2 is the<br>
GC thread, and thread 1 is the main thread. I have considered<br>
the following possibilities:<br>
<br>
(1) That I'm locking on the wrong object. I believe that isn't<br>
the case, because it's constructed only once, as a "new<br>
Object()" (ie, a reference type), and is only subsequently<br>
used for locking; and, because it keeps the same ipy id<br>
throughout.<br>
<br>
(2) That Monitor.Enter occasionally allows two different<br>
threads to acquire the same lock. I consider this extremely<br>
unlikely, because... well, how many multithreaded .NET apps<br>
already exist? If Monitor really were broken, I think we'd<br>
probably know about it by now.<br>
<br>
(3) That calling Flush() on a SyncTextWriter (the type of<br>
Console.Out) doesn't actually do anything, and the output is<br>
somehow wrongly ordered (although I can't imagine how this<br>
could actually be: if the locking is really working, then my<br>
console writes are strictly sequential). I don't have access<br>
to the code, so I have no idea how it's implemented, but even<br>
if this is the case it doesn't help much with the fundamental<br>
problem (the synchronisation error which follows).<br>
<br>
Apart from the above, I'm out of ideas. Can anyone suggest<br>
what I've missed?<br>
<br>
William<br>
_______________________________________________<br>
Users mailing list<br></div></div>
<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a> <mailto:<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a>><div class="Ih2E3d">
<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>
<br>
<br>
<br></div>
------------------------------------------------------------------------<div class="Ih2E3d"><br>
<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>
<br>
</div></blockquote><div><div></div><div class="Wj3C7c">
<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>
</div></div></blockquote></div><br>