How does FastAPI execute setup and teardown in nested yield dependencies?
It tests FastAPI's dependency injection lifecycle and stack-like teardown for nested yield dependencies. Setup runs top-down; teardown runs bottom-up after the response. A red flag is claiming teardown order is arbitrary or follows garbage collection.
WHAT THIS TESTS: This question probes whether you understand FastAPI's dependency injection machinery beyond simple function calls. Specifically it checks if you know that yield-based dependencies create managed context scopes, that sub-dependencies form a directed graph resolved by FastAPIs Depends() mechanism, and that teardown is deterministic rather than left to the Python garbage collector. Interviewers want to see that you recognize the framework maintains an internal stack to ensure resources are cleaned up in the correct order.
A GOOD ANSWER COVERS: First state that FastAPI resolves dependencies recursively so if dependency B depends on A then A is fully set up before B begins its setup. Second explain that when yield is used the code before the yield is setup and the code after is teardown. Third describe execution order as top-down for setup and bottom-up for teardown meaning the outermost dependency yields first then the inner ones yield in sequence and after the response the innermost teardown runs first followed by each outer teardown in reverse. Fourth mention that FastAPI handles this with an internal context manager-like stack not by relying on CPython reference counting. Fifth note that exceptions in teardown are propagated and can affect the response unless handled inside the dependency.
COMMON WRONG ANSWERS: A major red flag is saying teardown order is undefined or depends on Python's garbage collector. Another is claiming all setup and teardown happen sequentially in the same order rather than reversed for cleanup. Some candidates incorrectly state that yield dependencies run in parallel or that background tasks block dependency teardown. Also saying that an exception raised after yield is automatically swallowed by FastAPI is wrong; it propagates and can convert a successful response into a 500 error.
LIKELY FOLLOW-UPS: An interviewer might ask what happens if a sub-dependency raises an exception during setup which prevents downstream dependencies from running and triggers teardown for anything already initialized. They might also ask how this interacts with background tasks where teardown of dependencies waits for background tasks to finish. Another follow-up is comparing yield dependencies to lifespan events or asking how you would test a chain of yield dependencies.
ONE CONCRETE EXAMPLE: Imagine a path operation that depends on get_db which itself depends on get_connection_pool. Setup order is get_connection_pool opens a TCP pool and yields then get_db acquires a session and yields then the endpoint runs. Teardown order is get_db closes the session and commits or rolls back then get_connection_pool returns the connection to the pool. If get_db used yield and get_connection_pool also used yield FastAPI guarantees the session closes before the pool is released preventing leaked connections.
Read the original → fastapi.tiangolo.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.