Exceptions quit mainloop.

Heston James - Cold Beans heston.james at coldbeans.co.uk
Fri Sep 19 01:50:12 PDT 2008


Morning Simon, Thanks for the reply mate.

>> I have a situation which I've seen arise recently within an 
>> application built in python using the dbus gobject mainloop whereby 
>> when uncaught exceptions occur within the application it doesn't crash 
>> gracefully and I get sprawls or logging data spat back to the command 
>> line and the application continues to run.
> 
> Yes. This is because of the main loop code in pygobject (not dbus-python).
> dbus-python throws out stack traces to the Python 'logging' module (which
defaults > to sending them to stderr), because the alternative would be for
the information to > be lost entirely!

Ah Ok, that's makes fair sense. I suppose the simplest way to solve it
spitting to the command line is to configure a root logger on the
application then? That should catch any output and dump it into a rolling
log file or whatever.

>> I'm looking for a sure fire way that any uncaught exceptions which 
>> occur in the application will cause the mainloop to quit and the 
>> application to stop running properly, I can then have the system relaunch
it.
>
> Sorry, I wish there was one.

You and I both my man ;-) Never mind though.

>> I'm thinking perhaps something like this might work:
>> 
>>         # Attempt to run the mainloop.
>>         try:
>>             # Call for the mainloop to run.
>>             main_loop.run()
>>         # Catch any exceptions which may occur.
>>         except Exception, e:
>>             # An exception occurred within the application.
>>             # Log the exception which has occurred.
>>             self.__logger.critical("FATAL EXCEPTION OCCURRED: %s" %
str(e))
>>             # Quit the mainloop.
>>             main_loop.quit()
>
> I wish it was that simple... but no, uncaught exceptions do not terminate
run().

Understood. So what would you say my best option is? Just ensure that I'm
wrapping all calls that are likely to cause exceptions in try/catch and
manually quit the mainloop if one occurs?

I'm happy enough with that method if that's how everyone else is handling
it, it just seems so verbose, perhaps something could be generated with a
decorator which can be placed around all the methods in the app which will
catch exceptions thrown at lower levels? The decorator could then apply for
the mainloop to be stopped?

I'm not too fussed if it's not simple, I just want to find a nice
implementation which means my application crashes properly when a critical
exception occurs rather than half-crashing.

One more question for you Simon, if you don't mind. How do you pass your
mainloop around the application? In as much as, I create my mainloop as part
of my application initialization, but say an exception occurs in one of the
lower level modules or classes which I want to catch and quit the mainloop,
how would I access the mainloop from those clases? Do you inject it in? Or
can I simply import the mainloop into those classes? Is the mainloop a
singleton? Or is that totally irrelevant and should I only be dealing with
the mainloop at the top level of the application? What would you consider
the best practice on that?

Sorry, I'm blithering now, I'll stop :-)

Cheers Simon,

Heston



More information about the dbus mailing list