Challenge - 5 Problems
Teardown Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a Flask teardown hook raises an exception?
Consider a Flask app with a teardown hook that raises an exception. What is the behavior of the app after a request finishes?
Flask
from flask import Flask app = Flask(__name__) @app.teardown_request def teardown(exception): raise Exception('Error in teardown') @app.route('/') def index(): return 'Hello World' if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Think about how Flask handles errors in teardown hooks after the response is sent.
✗ Incorrect
Flask runs teardown hooks after the response is sent. If a teardown hook raises an exception, Flask logs it but does not change the response or crash the server.
❓ state_output
intermediate2:00remaining
What is the output count of teardown calls after multiple requests?
Given this Flask app, how many times will the teardown function print 'Teardown called' after 3 requests to '/'?
Flask
from flask import Flask app = Flask(__name__) count = 0 @app.teardown_request def teardown(exception): global count count += 1 print('Teardown called') @app.route('/') def index(): return 'Hi' if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Teardown hooks run once per request after the response.
✗ Incorrect
The teardown function runs once after each request finishes, so after 3 requests it runs 3 times.
📝 Syntax
advanced2:00remaining
Which option correctly registers a teardown hook for the application context?
Select the correct way to register a teardown function that runs when the Flask application context ends.
Attempts:
2 left
💡 Hint
Check the exact decorator name for application context teardown.
✗ Incorrect
The correct decorator is @app.teardown_appcontext. The others are either for request teardown or invalid.
🔧 Debug
advanced2:00remaining
Why does this teardown hook not run after a request?
Identify the reason why the teardown function below never executes after requests.
Flask
from flask import Flask app = Flask(__name__) @app.teardown_request def teardown(): print('Teardown executed') @app.route('/') def index(): return 'Hello' if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Check the function signature required by teardown_request hooks.
✗ Incorrect
Teardown functions registered with @app.teardown_request must accept one argument (the exception), even if not used. Missing it prevents the function from running.
🧠 Conceptual
expert2:00remaining
What is the main difference between @app.teardown_request and @app.teardown_appcontext?
Choose the statement that best describes the difference between these two Flask teardown decorators.
Attempts:
2 left
💡 Hint
Think about the lifecycle of a request versus the application context.
✗ Incorrect
@app.teardown_request runs after each request finishes. @app.teardown_appcontext runs when the application context is popped, which can happen less often than requests.