[IronPython] [python] IP2A4 - Bug when Exception thrown on worker thread

Davy Mitchell daftspaniel at gmail.com
Tue Oct 9 22:02:45 CEST 2007


Thanks Dino and Michael. Just remembered I had the opposite issue with
the WinForms Timer a while ago so I guess it makes sense :-) !

Looks like I should be something like...

import clr
from System.Threading import ApartmentState,Thread, ThreadStart

def Run():
    try:
        print wibble
    except:
        print "phew!"

t = Thread(ThreadStart(Run))
t.ApartmentState = ApartmentState.STA
t.Start()
Thread.Sleep(90000)

Thanks,
Davy Mitchell

On 10/9/07, Dino Viehland <dinov at exchange.microsoft.com> wrote:
> Well, just to be pedantic, in CPython you get:
>
> >>> import clr
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: No module named clr
> >>> from System.Threading import ApartmentState,Thread, ThreadStart
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: No module named System.Threading
> >>>
> >>> def Run():
> ...     print wibble
> ...
> >>> t = Thread(ThreadStart(Run))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'Thread' is not defined
> >>> t.ApartmentState = ApartmentState.STA
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'ApartmentState' is not defined
> >>> t.Start()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 't' is not defined
> >>> Thread.Sleep(90000)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'Thread' is not defined
>
> On the other hand if you do the same thing in both IronPython & CPython:
>
> import thread
> def Run():
>     print wibble
>
> thread.start_new_thread(Run, ())
>
> you get the same results.
>
> This is because in .NET threads don't have a backstop for unhandled exceptions (and it's generally considered a good thing, and a positive change in v2.0 - this helps prevent unhandled exceptions from going unnoticed).  Obviously Python has made a different choice here (as did .NET v1.x) and so we respect that choice when you use the standard Python functionality.
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
> Sent: Tuesday, October 09, 2007 12:07 PM
> To: Discussion of IronPython
> Subject: Re: [IronPython] [python] IP2A4 - Bug when Exception thrown on worker thread
>
> Davy Mitchell wrote:
> > Hi - the following snippit crashes IP2A4.
> >
>
> It crashes 1.1 as well. We just kind of accepted that unhandled
> exceptions on threads cause app domain errors.
>
> In CPython it just raises an exception and terminates the thread.
>
> Michael
> http://www.manning.com/foord
>
> > import clr
> > from System.Threading import ApartmentState,Thread, ThreadStart
> >
> > def Run():
> >     print wibble
> >
> > t = Thread(ThreadStart(Run))
> > t.ApartmentState = ApartmentState.STA
> > t.Start()
> > Thread.Sleep(90000)
> >
> > Unhandled Exception: Microsoft.Scripting.UnboundNameException: name 'wibble' is
> > not defined
> >    at IronPython.Runtime.PythonContext.MissingName(SymbolId name)
> >    at Microsoft.Scripting.ModuleGlobalWrapper.GetCachedValue()
> >    at Microsoft.Scripting.ModuleGlobalWrapper.get_CurrentValue()
> >    at __main__$mod_2.Run$1() in bug.py:line 5
> >    at _stub_##17(Object[] , DynamicSite`2 , CodeContext , Object )
> >    at Microsoft.Scripting.Actions.DynamicSite`2.UpdateBindingAndInvoke(CodeConte
> > xt context, T0 arg0)
> >    at Microsoft.Scripting.Actions.DynamicSiteHelpers.UninitializedTargetHelper`7
> > .Invoke1(DynamicSite`2 site, CodeContext context, T0 arg0)
> >    at Microsoft.Scripting.Actions.DynamicSite`2.Invoke(CodeContext context, T0 a
> > rg0)
> >    at System.Void(), using PythonBinder##9(Object[] )
> >    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
> >    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
> > ontextCallback callback, Object state)
> >    at System.Threading.ThreadHelper.ThreadStart()
> >
> >
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>


-- 
Davy Mitchell
Blog - http://www.latedecember.co.uk/sites/personal/davy/
Twitter - http://twitter.com/daftspaniel
Skype - daftspaniel
needgod.com



More information about the Ironpython-users mailing list