librelist archives

« back to archive

Global object breaks while running by mod_wsgi

Global object breaks while running by mod_wsgi

From:
Peter Cai
Date:
2011-08-19 @ 16:25
Hi all,

I'm using global object to store db object like this:

@app.before_request
def before_request():
    g.db = bsddb.hashopen('/tmp/my_db', 'c')

@app.teardown_request
def teardown_request(excpetion):
    g.db.sync()
    g.db.close()

This approach works fine in debug mode, but when it comes to mod_wsgi, it
keeps reporting that g.db  doesn't exist in teardown_request.

I'm running mod_wsgi in embedded mode, could that be the cause of the
problem?

Re: [flask] Global object breaks while running by mod_wsgi

From:
Armin Ronacher
Date:
2011-08-19 @ 16:37
Hi,

On 8/19/11 6:25 PM, Peter Cai wrote:
> I'm running mod_wsgi in embedded mode, could that be the cause of the
> problem?
Does the problem go away in non embedded mode?  I have to admit that I
never even tried to use the embedded mode due to the fact that it is
embedded :)


Regards,
Armin

Re: [flask] Global object breaks while running by mod_wsgi

From:
Peter Cai
Date:
2011-08-19 @ 16:48
I changed the code to check if g.db exists:


if g.db:
    g.db.sync()


But I couldn't even check it:

[Fri Aug 19 12:45:32 2011] [error] [client 10.122.121.44]     if g.db:
[Fri Aug 19 12:45:32 2011] [error] [client 10.122.121.44]   File
"/home/peter.cai/env/tran++/lib/python2.5/site-packages/werkzeug/local.py",
line 357, in __getattr__
[Fri Aug 19 12:45:32 2011] [error] [client 10.122.121.44]     return
getattr(self._get_current_object(), name)
[Fri Aug 19 12:45:32 2011] [error] [client 10.122.121.44] AttributeError:
'_RequestGlobals' object has no attribute 'db'



On Fri, Aug 19, 2011 at 12:37 PM, Armin Ronacher <
armin.ronacher@active-4.com> wrote:

> Hi,
>
> On 8/19/11 6:25 PM, Peter Cai wrote:
> > I'm running mod_wsgi in embedded mode, could that be the cause of the
> > problem?
> Does the problem go away in non embedded mode?  I have to admit that I
> never even tried to use the embedded mode due to the fact that it is
> embedded :)
>
>
> Regards,
> Armin
>

Re: [flask] Global object breaks while running by mod_wsgi

From:
Armin Ronacher
Date:
2011-08-19 @ 19:45
Hi,

On 8/19/11 6:48 PM, Peter Cai wrote:
> I changed the code to check if g.db exists:
> 
> 
>     if g.db:
>         g.db.sync()
Well.  Not sure if that's a solution you want to work with, but what the
actual problem is, is the absence of a request context.  To test for
that you can either use

if flask.has_request_context():
    g.db.sync()

Or check if the g object is truthy:

if g:
    g.db.sync()


However neither should be necessary since we guarantee that
before_request is always executed except for if a before_request handler
returns a response.  In that case that might actually be the culprit.
Unless the code is 100% confidential I would recommend pasting the code
so we can help pinpointing the problem.


Regards,
Armin

Re: [flask] Global object breaks while running by mod_wsgi

From:
Peter Cai
Date:
2011-08-19 @ 21:05
Hi,

I figured it out myself.

When the daemon process has no permission to write /tmp/my_db, the code
below

    g.db = bsddb.hashopen('/tmp/my_db', 'c')

raises an exception.

But somehow the code in tear down is still executed.

So I only saw the exception that g.db doesn't exist in log, but I missed the
exception before it, which really caused the problem.


On Fri, Aug 19, 2011 at 3:45 PM, Armin Ronacher <armin.ronacher@active-4.com
> wrote:

> Hi,
>
> On 8/19/11 6:48 PM, Peter Cai wrote:
> > I changed the code to check if g.db exists:
> >
> >
> >     if g.db:
> >         g.db.sync()
> Well.  Not sure if that's a solution you want to work with, but what the
> actual problem is, is the absence of a request context.  To test for
> that you can either use
>
> if flask.has_request_context():
>    g.db.sync()
>
> Or check if the g object is truthy:
>
> if g:
>    g.db.sync()
>
>
> However neither should be necessary since we guarantee that
> before_request is always executed except for if a before_request handler
> returns a response.  In that case that might actually be the culprit.
> Unless the code is 100% confidential I would recommend pasting the code
> so we can help pinpointing the problem.
>
>
> Regards,
> Armin
>

Re: [flask] Global object breaks while running by mod_wsgi

From:
Armin Ronacher
Date:
2011-08-20 @ 09:03
Hi,

On 8/19/11 11:05 PM, Peter Cai wrote:
> But somehow the code in tear down is still executed.
Ah yes, that is correct and expected behavior.


Regards,
Armin

Re: [flask] Global object breaks while running by mod_wsgi

From:
Peter Cai
Date:
2011-08-19 @ 16:42
No it's still there.   I switched to daemon mode.  Still got this exception
in log:

[ .. skipped ..]
[Fri Aug 19 12:40:31 2011] [error] [client 10.122.121.44]     g.db.sync()
[Fri Aug 19 12:40:31 2011] [error] [client 10.122.121.44]   File
"/home/peter.cai/env/tran++/lib/python2.5/site-packages/werkzeug/local.py",
line 357, in __getattr__
[Fri Aug 19 12:40:31 2011] [error] [client 10.122.121.44]     return
getattr(self._get_current_object(), name)
[Fri Aug 19 12:40:31 2011] [error] [client 10.122.121.44] AttributeError:
'_RequestGlobals' object has no attribute 'db'


On Fri, Aug 19, 2011 at 12:37 PM, Armin Ronacher <
armin.ronacher@active-4.com> wrote:

> Hi,
>
> On 8/19/11 6:25 PM, Peter Cai wrote:
> > I'm running mod_wsgi in embedded mode, could that be the cause of the
> > problem?
> Does the problem go away in non embedded mode?  I have to admit that I
> never even tried to use the embedded mode due to the fact that it is
> embedded :)
>
>
> Regards,
> Armin
>