librelist archives

« back to archive

404 on first requests in before_request

404 on first requests in before_request

From:
J Glass
Date:
2012-11-07 @ 02:44
Hi everyone,

We're trying to track down a bug where occasional early requests to our
flask app return a 404.  I installed a custom middleware logger that prints
out requests and their statuses, and it looks like the problem happens on
the first request for a process that gets simultaneous concurrent requests
(I see two requests on the same pid within 1/1000th of a second).  I used
settrace to track down what was initiating the 404, and it looks like a
call to session.get in our before_request function causes
flask.app.dispatch_request to call raise_routing_exception.  Weirdly, a
previous call to session.get worked in the same function.  I'm pretty sure
the exception that is causing the raise_routing_exception call is from the
RuntimeError('working outside of request context') raised in
globals._lookup_object.

We have mod_wsgi configured to use multiple daemon processes and threads,
so sounds like a race condition.  We could only use 1 thread, but would
prefer to find the root of the problem.

Any ideas?  We aren't spawning any threads ourselves in requests.

Here's some relevant version info:
Apache/2.2.22
Python/2.7.3
mod_wsgi/3.3
werkzeug/flask .8


Thanks!