Correct usage of StackContext?

425 views
Skip to first unread message

Michael Russo

unread,
Jan 12, 2011, 11:21:57 PM1/12/11
to python-tornado
I am working on modifying the Python (Avro RPC-based) HBase library to support Tornado's IOLoop (using AsyncHTTPClient under-the-covers).

Basic exception handling (via ExceptionStackContext) was added in the following commit:


The intention is to catch any thrown exception, return None to the caller, and stop the exception from bubbling up to other exception handlers.  (Possible improvements include returning an object with non-None error attribute instead of returning None; the current approach is simply a starting point.)

I have a RequestHandler that performs several hundred calls using this library, waits for all calls to return, and then continues on to perform some additional computation before returning a response.  In general, this works properly, regardless of whether any exceptions are thrown in any of these asynchronous Avro requests.

However, if an exception is thrown in any code that is run **after** all Avro responses are collected, an exception handler for one of the Avro requests is invoked.  This, in turn, suppresses the exception (since the Avro exception handler explicitly stops the exception from propagating), which causes other very bad things to happen.

Where am I going wrong?

Thanks,
Michael

Ben Darnell

unread,
Jan 13, 2011, 4:01:27 PM1/13/11
to python-...@googlegroups.com
Once a StackContext is added to the stack, it stays with you until its branch of execution dies out (this is not ideal, but I haven't come up with a clean way to pop a context off the stack).  The solution is to capture the context before entering your ExceptionStackContext:  "callback = stack_context.wrap(callback)" at line 33.  This will restore the state as of the time wrap() was called when you go back to the caller's code.  

-Ben

Michael Russo

unread,
Jan 13, 2011, 5:05:16 PM1/13/11
to python-...@googlegroups.com
Ben, that worked perfectly.  Thank you!

-Michael
Reply all
Reply to author
Forward
0 new messages