Open John98Zakaria opened 2 years ago
I suppose we have a regression?
https://github.com/ets-labs/python-dependency-injector/issues/574
I encountered the same issue, shutdown method of resource just don`t called after handler scope ends.
I think the main problem here that Resource is more about global context, I mean that resource initialized and shutdowns with server. But in our case we need something like AsyncFactory, that will build different objects on every request and shutdown them properly after handler scopes ends. From the docs: "Resource provider is similar to Singleton. Resource initialization happens only once. You can make injections and use provided instance the same way like you do with any other provider."
I think the main problem here that Resource is more about global context, I mean that resource initialized and shutdowns with server. But in our case we need something like AsyncFactory, that will build different objects on every request and shutdown them properly after handler scopes ends. From the docs: "Resource provider is similar to Singleton. Resource initialization happens only once. You can make injections and use provided instance the same way like you do with any other provider."
I would argue against that. If you put a breakpoint at https://github.com/tiangolo/fastapi/blob/master/fastapi/dependencies/utils.py#L522
You will see how fastapi handles Depends(...) objects.
Fastapi is unable to recognize that the async generator function (the one creating/closing the db) a generator is. I instead it sees it as a normal function which makes it skip the yield call and nothing close the resource. You can verify that by converting everything to a sync function. Then the resource will be closed and everything would be work as expected.
The only once that is mentioned for being created once /request dependency chain.
I would argue against that. If you put a breakpoint at https://github.com/tiangolo/fastapi/blob/master/fastapi/dependencies/utils.py#L522
I put breakpoint there and in my case fastapi receive Closing marker, not generator. I tried to use sync generator, but it also did not closed. So I think problem is that Closing marker don`t work with Depends, If you call function manually, resource closes how it should.
I would argue against that. If you put a breakpoint at https://github.com/tiangolo/fastapi/blob/master/fastapi/dependencies/utils.py#L522
I use fastapi==0.79.1 and dependency-injector==4.40.0. In your case fastapi also receives Closing marker at that breakpoint.
Hi, I'm facing the same problem here
Closing + Depends does not work
Hi guys! I have the same issue, there is any update on it, or any workaround ?
Example of my init method to provide connection resource :
async def init_connection(db_pool) :
async with db_pool.connection() as connection:
yield connection
_Where dbpool is an instance of Database from databases module.
Also, this Depends(Provide[...])
usage will spin up a new thread per request due to solved = await run_in_threadpool(call, **sub_values)
. This has some major issues:
Hence, I don't recommend following the FastAPI example. https://python-dependency-injector.ets-labs.org/examples/fastapi.html.
I was trying out the async resource provider to verify that it is actually closing the resource. However, I noticed that it is not closing the resource.
After deep investigations I found out that the @inject decorator on the API makes python think that the function is not an async generator.
FastAPI decides whether the dependency is a generator on this line https://github.com/tiangolo/fastapi/blob/master/fastapi/dependencies/utils.py#L522
Which then tries to inspect the code using the builtin inspect function
Which is returning false for async generators decorated with @inject
I have attached the test code.
I'll try investigating the @inject decorator To see whether it could be fixed