Skip to content

Commit d3ca551

Browse files
committed
Updated the docs and examples to non-failing teardown handlers
1 parent 485a6c3 commit d3ca551

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

docs/patterns/sqlite3.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ So here is a simple example of how you can use SQLite 3 with Flask::
2424

2525
@app.teardown_request
2626
def teardown_request(exception):
27-
g.db.close()
27+
if hasattr(g, 'db'):
28+
g.db.close()
29+
30+
.. note::
31+
32+
Please keep in mind that the teardown request functions are always
33+
executed, even if a before-request handler failed or was never
34+
executed. Because of this we have to make sure here that the database
35+
is there before we close it.
2836

2937
Connect on Demand
3038
-----------------

docs/reqcontext.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ understand what is actually happening. The new behavior is quite simple:
131131

132132
4. At the end of the request the :meth:`~flask.Flask.teardown_request`
133133
functions are executed. This always happens, even in case of an
134-
unhandled exception down the road.
134+
unhandled exception down the road or if a before-request handler was
135+
not executed yet or at all (for example in test environments sometimes
136+
you might want to not execute before-request callbacks).
135137

136138
Now what happens on errors? In production mode if an exception is not
137139
caught, the 500 internal server handler is called. In development mode
@@ -183,6 +185,12 @@ It's easy to see the behavior from the command line:
183185
this runs after request
184186
>>>
185187

188+
Keep in mind that teardown callbacks are always executed, even if
189+
before-request callbacks were not executed yet but an exception happened.
190+
Certain parts of the test system might also temporarily create a request
191+
context without calling the before-request handlers. Make sure to write
192+
your teardown-request handlers in a way that they will never fail.
193+
186194
.. _notes-on-proxies:
187195

188196
Notes On Proxies

docs/upgrading.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ You are now encouraged to use this instead::
142142

143143
@app.teardown_request
144144
def after_request(exception):
145-
g.db.close()
145+
if hasattr(g, 'db'):
146+
g.db.close()
146147

147148
On the upside this change greatly improves the internal code flow and
148149
makes it easier to customize the dispatching and error handling. This

examples/flaskr/flaskr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def before_request():
5050
@app.teardown_request
5151
def teardown_request(exception):
5252
"""Closes the database again at the end of the request."""
53-
g.db.close()
53+
if hasattr(g, 'db'):
54+
g.db.close()
5455

5556

5657
@app.route('/')

examples/minitwit/minitwit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def before_request():
8585
@app.teardown_request
8686
def teardown_request(exception):
8787
"""Closes the database again at the end of the request."""
88-
g.db.close()
88+
if hasattr(g, 'db'):
89+
g.db.close()
8990

9091

9192
@app.route('/')

0 commit comments

Comments
 (0)