-
First Check
Commit to Help
Example Code## Server code
@app.post("/heroes/")
def create_hero(hero: Hero, session: SessionDep) -> Hero:
session.add(hero)
session.flush()
return hero
@app.get("/heroes/{hero_id}")
def read_hero(hero_id: int, session: SessionDep) -> Hero:
hero = session.get(Hero, hero_id)
if not hero:
raise HTTPException(status_code=404, detail="Hero not found")
return hero
## Client code
hero_id = requests.post("/heroes", json={...})
requests.get(f"/heroes/{hero_id}") # sometimes returns 404DescriptionMy Use CaseBefore This pattern is very common in tutorials and examples. It ensures that when the client receives a Problem With Current BehaviorAfter
In effect, this changes the semantics from "commit before responding" to "commit sometime after responding". Why Early Closing Would HelpIn my case, I need to guarantee transactional consistency at response time.
ProposalIt would be very helpful to have an opt-in mechanism for early closing, e.g.:
This way, users can choose the behavior that fits their use case:
Operating SystemWindows Operating System DetailsNo response FastAPI Version0.118.0 Pydantic Version2.11.9 Python Version3.13.5 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 5 replies
-
IMO, the main benefit of "dependency-based session handling" is the ability to override this dependency in tests |
Beta Was this translation helpful? Give feedback.
-
|
Just to add - we independently had the same case in Airflow - I described it in #14155 and missed this one, but the code we had and use case is practically the same. |
Beta Was this translation helpful? Give feedback.
-
|
+1 for the an extra param to the dependency to opt-in for early closing that would be super helpful for us. On Apache Airflow I have the same feeling as @alexjrk, we can't have a centralised session teardown in the dependency anymore, making it quite painful to duplicate this logic at the end of each endpoint. We explored a possible alternative which is to move away from the dependency handling the session lifecycle, and instead use a custom route class that will be able to do that logic right after creating the response and before sending it back. (Still provide a dependency for convenience) but it felt super hacky. |
Beta Was this translation helpful? Give feedback.
-
|
Same here. Also, in case of connection error to external db, there's a chance for the post endpoint in the example to return 200 although no changes are left in the db |
Beta Was this translation helpful? Give feedback.
-
|
Chiming in this is a real problem, since, from the SQLAlchemy docs:
I agree that putting commits in every endpoint manually is an anti-pattern and prone to errors. The only workaround against data loss I'm aware of at the moment is using a hacky middleware which is mentioned in this discussion. |
Beta Was this translation helpful? Give feedback.
-
|
Any thoughts frrom maintainers for now @tiangolo @YuriiMotov ? |
Beta Was this translation helpful? Give feedback.
-
|
I'm working on adding support for early closing here: #14262 This will enable If you have opinions, let me know there, I hope to merge and release it soon. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks. much appreciated @tiangolo . I will ping my fellow Airflow maintainers who can provide much more feedback than me |
Beta Was this translation helpful? Give feedback.
I'm working on adding support for early closing here: #14262
This will enable
Depends(func, scope="function"), more details and alternative names in the PR.If you have opinions, let me know there, I hope to merge and release it soon.