Skip to content

Unclosed client session with FastAPI #2349

Closed as not planned
Closed as not planned
@blenzi

Description

@blenzi

Elasticsearch version (bin/elasticsearch --version): 7.17.3

elasticsearch-py version (elasticsearch.__versionstr__): (7, 17, 3)

Please make sure the major version matches the Elasticsearch server you are running.

Description of the problem including expected versus actual behavior:

Hi,

I have an API with routes across multiple python files. The module that uses and loads elasticsearch has this function:

@lru_cache
def get_elastic_search_client(hosts=elasticsearch_url):
    if not hosts:
        raise TypeError("ELASTICSEARCH_URL not defined")
    return AsyncElasticsearch(hosts=hosts)

That is used by many other modules. In main.py:

from es_utils import get_elastic_search_client
[...]
try:
    es_client = get_elastic_search_client()
except Exception:
    es_client = None

app = FastAPI(...)

@app.on_event("shutdown")
async def shutdown():
    if es_client is not None:
        await es_client.close()

following the recipe in https://elasticsearch-py.readthedocs.io/en/7.x/async.html#receiving-unclosed-client-session-connector-warning but I still get Unclosed client session. Any suggestions?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions