current_user indirectly breaks cron jobs by requiring request context #140

Closed
yannk opened this Issue Feb 11, 2014 · 6 comments

Comments

Projects
None yet
2 participants
@yannk

yannk commented Feb 11, 2014

(edited: missing first line of traceback)
I have a very classical cron for sending the email.

in 0.2.7 I would setup a with block with app_context and happily send my emails. This stopped working in 0.2.9 (see trace below) because of this change in get_user():

https://github.com/maxcountryman/flask-login/pull/124/files#diff-8bd3f6872cdafc9bc4c75795c161c717R711

 File "/Users/yann/dev/tugboat/tugboat/util/email.py", line 27, in render_email_template
    rendered = render_template("email/%s" % template, url_for=url_for_email, **kwargs)
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask/templating.py", line 126, in render_template
    ctx.app.update_template_context(context)
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask/app.py", line 716, in update_template_context
    context.update(func())
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 799, in _user_context_processor
    return dict(current_user=_get_user())
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 768, in _get_user
    current_app.login_manager._load_user()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 327, in _load_user
    deleted = self._session_protection()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 351, in _session_protection
    sess = session._get_current_object()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
    return self.__local()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask/globals.py", line 20, in _lookup_req_object
    raise RuntimeError('working outside of request context')

This is a shot in the dark, but maybe adding:

    if flask.has_request_context():

in front of that block would fix it?

@maxcountryman

This comment has been minimized.

Show comment Hide comment
@maxcountryman

maxcountryman Feb 17, 2014

Owner

I'm curious what part of your email method relies on the request context?

Owner

maxcountryman commented Feb 17, 2014

I'm curious what part of your email method relies on the request context?

@yannk

This comment has been minimized.

Show comment Hide comment
@yannk

yannk Feb 17, 2014

none... that's the problem, it used to work with just app context but 0.2.9 (because of the line pointed above) broke that.

yannk commented Feb 17, 2014

none... that's the problem, it used to work with just app context but 0.2.9 (because of the line pointed above) broke that.

@maxcountryman

This comment has been minimized.

Show comment Hide comment
@maxcountryman

maxcountryman Feb 17, 2014

Owner

Yes but why is your code running in an application context to begin with?

Owner

maxcountryman commented Feb 17, 2014

Yes but why is your code running in an application context to begin with?

@yannk

This comment has been minimized.

Show comment Hide comment
@yannk

yannk Feb 17, 2014

The cron sends emails, and the templates make use of url_for()

yannk commented Feb 17, 2014

The cron sends emails, and the templates make use of url_for()

@maxcountryman

This comment has been minimized.

Show comment Hide comment
@maxcountryman

maxcountryman Feb 17, 2014

Owner

Can you paste the full traceback?

You can also disable the context processor if you have no need of the current user in your template.

Owner

maxcountryman commented Feb 17, 2014

Can you paste the full traceback?

You can also disable the context processor if you have no need of the current user in your template.

@yannk

This comment has been minimized.

Show comment Hide comment
@yannk

yannk Feb 17, 2014

Oh you mean changing the argument to init_app(), just in the cron context?

Here is the traceback from the point we catch the exception:

  File "/Users/yann/dev/tugboat/tugboat/util/email.py", line 27, in render_email_template
    rendered = render_template("email/%s" % template, url_for=url_for_email, **kwargs)
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask/templating.py", line 126, in render_template
    ctx.app.update_template_context(context)
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask/app.py", line 716, in update_template_context
    context.update(func())
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 799, in _user_context_processor
    return dict(current_user=_get_user())
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 768, in _get_user
    current_app.login_manager._load_user()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 327, in _load_user
    deleted = self._session_protection()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 351, in _session_protection
    sess = session._get_current_object()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
    return self.__local()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask/globals.py", line 20, in _lookup_req_object
    raise RuntimeError('working outside of request context')

yannk commented Feb 17, 2014

Oh you mean changing the argument to init_app(), just in the cron context?

Here is the traceback from the point we catch the exception:

  File "/Users/yann/dev/tugboat/tugboat/util/email.py", line 27, in render_email_template
    rendered = render_template("email/%s" % template, url_for=url_for_email, **kwargs)
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask/templating.py", line 126, in render_template
    ctx.app.update_template_context(context)
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask/app.py", line 716, in update_template_context
    context.update(func())
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 799, in _user_context_processor
    return dict(current_user=_get_user())
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 768, in _get_user
    current_app.login_manager._load_user()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 327, in _load_user
    deleted = self._session_protection()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask_login.py", line 351, in _session_protection
    sess = session._get_current_object()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
    return self.__local()
  File "/Users/yann/envs/tugboat/lib/python2.7/site-packages/flask/globals.py", line 20, in _lookup_req_object
    raise RuntimeError('working outside of request context')

xsleonard added a commit to xsleonard/flask-login that referenced this issue Feb 17, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment