How so? I understand the problem from your first mail, but don't see
how it's exacerbated by multiple requests.
> The patch I referenced below fixes the issue for me. Also, more simply,
> wrapping the callback in _run_callback in iostream.py in a NullContext()
> also fixes the issue (at least it appears to).
> My overall concern, however, is that StackContexts aren't the right
> solution. While wrapping callbacks in AsyncCallback was tedious previously,
> it was less tedious than wrapping callbacks with stack_context.wrap and then
> creating and maintaining all the contexts. It was certainly less
> error-prone.
StackContext is a deliberate tradeoff to make application code less
error prone at the expense of more complexity in the core libraries.
It's rare for an application to interact with StackContext directly (I
only use it once in Brizzly/Picnics, and I've never had to call
stack_context.wrap outside of Tornado itself).
However, you have highlighted an issue I hadn't thought of, which is
that if any exception handlers are not StackContexts, they not only
may be skipped on subsequent callbacks, but they may be applied out of
order and break things. I don't like the fact that this makes
StackContext effectively mandatory in some cases.
I'm not sure what the right error-handling semantics are for IOStream.
Since HTTPClient is not IOStream-based, most apps only interact with
a single IOStream per request, and that in a relatively limited way.
If HTTPClient used IOStream, would it be right for an uncaught
exception to close both the client and server sockets? Maybe, if the
connection could not be verified to be in a sane state and returned to
the pool, but this suggests that error handling at a deeper level than
IOStream will always be required and IOStream's error handler is just
a last resort.
Error handling is hard. Life would be so much easier if nothing ever failed. :)
-Ben
> After looking into the issue some more, I have another suggestion. What wasHow so? I understand the problem from your first mail, but don't see
> causing issues is that when multiple requests are being handled at the same
> time the stack_contexts start to conflict.
how it's exacerbated by multiple requests.
Yes, IOStream._run_callback should definitely log the exception.
Using NullContext doesn't quite feel right here (I had envisioned it
as just being for truly request-independent code like
AsyncHTTPClient._process_queue), but it does kind of seem like the
StackContext magic should happen at the application/tornado boundary.
A NullContext in _add_io_state is probably the right solution.
-Ben
Hmm.. Do you think that that NullContext is the right thing to do, or
is it just something that empirically works but you're not sure why?
> Does wrapping add_handler with NullContext cause handle_events to run in
> that NullContext?
Yes, that's the idea. IOLoop and IOStream (and AsyncHTTPClient) exist
outside of any StackContexts and then recreate the contexts when they
return control to the application.
-Ben