question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A Flask app holds a reference to itself

See original GitHub issue
from flask import Flask
import objgraph

def make_app():
    app = Flask(__name__)

    @app.route('/')
    def hello_world():
        return 'Hello, World!'

    return app

def test():
    myapp = make_app()
    objgraph.show_backrefs([myapp], filename='backrefs.png', max_depth=6)

test()

Here’s what the produced reference graph looks like: backrefs

Is it the intended behavior? In our library a Flask app is created dynamically in a closure, and as a result it retains anything else that was created in that closure. We work around this by having a trampoline function and passing heavy objects as weakrefs, but it was still unexpected.

Environment

  • Python version: 3.7.5
  • Flask version: 1.1.2
  • Werkzeug version: 1.0.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jabcommented, Oct 3, 2020

@davidism and I discussed further and agreed to accept a fix for this, so I just merged #3762 after making some cleanups.

Thank you for the issue report and the PR!

0reactions
davidismcommented, May 17, 2022

The test that you added is now preventing me from implementing other legitimate things. I want to have a self.json = JSONProvider(self) assignment, but the test still fails. Composition like this is a very common pattern, and it would be meaningless to do it without the self in this case. I’m very likely to remove the test to support that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API — Flask Documentation (2.2.x)
The flask object implements a WSGI application and acts as the central object. It is passed the name of the module or package...
Read more >
How to get a reference to a module inside the module itself?
The module reference can be found in the sys.modules dictionary. See the Python documentation · Share.
Read more >
How To Handle Errors in a Flask Application - DigitalOcean
In this step, you'll create an application that has a few errors and run it without debug mode to see how the application...
Read more >
1. Application Object — Flask API - GitHub Pages
The flask object implements a WSGI application and acts as the central object. It is passed the name of the module or package...
Read more >
Developing Python Web Applications with Flask
It is strongly recommended to develop Python application under a virtual ... The from-import statement allows us to reference the Flask class directly...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found