Skip to content

copy_current_request_context creates new global app context #3306

Closed

Description

@kaos

Related to #2935 but for g.

Expected Behavior

When I use @copy_current_request_context it should preserve the contents in flask.g.

I would expect that the g object is transparent to whether I'm in a normal request context, or one that has been copied using the copy_current_request_context decorator in another greenlet.

I borrowed the very nice test-script from #2935 and adapted it for g instead.

import concurrent.futures
import random

from flask import Flask, g, copy_current_request_context, request, session

app = Flask(__name__)
app.config['SECRET_KEY'] = 'test'
executor = concurrent.futures.ThreadPoolExecutor()

@app.route('/')
def session_context():
    test_value = random.randint(1, 1001)
    g.test_value = test_value

    original_context = (
        ('g', getattr(g, 'test_value', None))
    )

    @copy_current_request_context
    def debug_session():
        return (
            ('g', getattr(g, 'test_value', None))
        )

    future = executor.submit(debug_session)
    print('original_context:', original_context)
    print('copied_context:', future.result())
    return 'ok'


if __name__ == '__main__':
    client = app.test_client()
    print('### Request ###')
    client.get('/')

Actual Behavior

I get a new empty g object in the decorated function.

### Request ###
original_context: ('g', 750)
copied_context: ('g', None)

Environment

  • Python version: 3.7.3
  • Flask version: 1.1.1
  • Werkzeug version: 0.15.5

Activity

  1. davidism commented on Jul 23, 2019

    @davidism
    Member

    I can see why it's confusing, but I'm not sure how we would change this. You copied the request context, not the app context. Pushing a request context pushes an app context if there isn't one, and that's what g is tied to.

  2. kaos commented on Jul 24, 2019

    @kaos
    Author

    Confusing indeed. What is the purpose of the app context, then?
    How are you supposed to use it, that isn't solved by using the request context?
    I've adapted, and now use the request context rather than the app context, but it wasn't really clear to me that we could use the request context for storing arbitrary data. Also, it lacks some of the get/set methods present on g, adding to the assumption that g is the object to put your data in, rather than the request context.

    Wouldn't it make sense to keep the same app context, also for the newly pushed (copied) request context?

  3. dchevell commented on Sep 29, 2019

    @dchevell
    Contributor

    @kaos for what it's worth, I discovered this when I was raising #2935 and g being bound to the app context caught be by surprise too. I haven't found a great solution, because pushing/copying the app context a) doesn't have a built in decorator (leading me to believe it's not something we're expected to be doing) and b) tends to have weird side effects when used in combination with important extensions like Flask-SQLAlchemy.

    I'm currently trying to untangle one of these side effects in my own Flask-Executor extension: dchevell/flask-executor#15. FWIW, that extension does what you're trying to do automatically - by pushing the app context with every request it supports accessing g from threads, and I suspect your example would succeed there.

    Assuming this is closed as "not a bug", perhaps you could pop over there and see if it solves your problem.

  4. kaos commented on Dec 30, 2019

    @kaos
    Author

    Thanks @dchevell will have a look at what you have done.

    Closing as "not a bug", seeing that we should rely on flask.request rather than on g for request time globals.

  5. locked as resolved and limited conversation to collaborators on Nov 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions