python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Async resource is sometimes None

Open tymokvo opened this issue 3 years ago • 1 comments
trafficstars

Hello, I am trying to provide a long-lived aiohttp.ClientSession for integrating with an external HTTP API.

I have an async generator for the resource defined as:

async def http_session_factory(*args, **kwargs):
    session = ClientSession(*args, **kwargs)

    yield session

    if not session.closed:
        await session.close()

which is then added to the DeclarativeContainer with providers.Resource(http_session_factory).

But, my application is intermittently throwing:

AttributeError: 'NoneType' object has no attribute 'request'"

when trying to use the provided ClientSession instance.

Am I initializing this incorrectly?

tymokvo avatar Dec 15 '21 22:12 tymokvo

I am seeing similar behavior in flask but for a blocking/non-async resource. It seems to happen when there are several concurrent requests being handled by separate threads. Did you ever resolve the problem?

kyle-mccarthy avatar Feb 13 '22 03:02 kyle-mccarthy