Replies: 4 comments 8 replies
-
The |
Beta Was this translation helpful? Give feedback.
-
This issue appears only if self.cache_key = (self.call, tuple(sorted(set(self.security_scopes or [])))) I believe the intended behavior is to isolate |
Beta Was this translation helpful? Give feedback.
-
See #2945 about why the isolation of cache key with scopes was introduced. |
Beta Was this translation helpful? Give feedback.
-
I would like to resume the discussion on dependency caching. I apologize in advance for the long text.. The documentation says that
But this is not true. In the current implementation, FastAPI caches dependencies according to the dependency name and scopes. Thus, one dependency that is used twice with different scopes will be called twice (and cached twice). This is not entirely intuitive behavior and it is undesirable in some cases. In the example above, There are a few discussions on this topic:
And @DurandA has created a PR to fix this problem #9790. Even though this PR doesn't brake any other tests, I think it brakes the logic of scopes propagation. Let's assume that Before summarizing, let`s take a look at one more example: Let`s assume that:
From my point of view it would be logical to call I think it's important to take a deeper look and figure it out before making changes to avoid new mistakes. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code (full working example)
Description
I am using a synchronous (SQLite) session in a FastAPI Dependency as shown in the SQLModel Tutorial.
When a route "depends" on a
Dependency
and aSecurity
that useget_session
, the session is created twice resulting in errors such as:How can this be prevented? I would expect a Security to reuse Dependencies as long as
use_cache=True
.Operating System
Linux
Operating System Details
Ubuntu 20.04
FastAPI Version
0.89.1
Python Version
Python 3.8.10
Additional Context
The question was originally asked in SQLModel discussion. However, I think this is unrelated to SQLModel and maybe bug with FastAPI
Security
object. I also created a StackOverflow question with a bounty if you have a nice solution on how to handle this.Beta Was this translation helpful? Give feedback.
All reactions