From 39431572f41d149fa9a7d6eebd5f8013015c5224 Mon Sep 17 00:00:00 2001 From: Sebastian Allard Date: Fri, 27 Oct 2023 10:14:00 +0100 Subject: [PATCH] Ensure connection is closed after CLI commands (#2635)(patch) --- cg/cli/base.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cg/cli/base.py b/cg/cli/base.py index 5905a4e586..0475ca9a6b 100644 --- a/cg/cli/base.py +++ b/cg/cli/base.py @@ -6,6 +6,7 @@ import click import coloredlogs +from sqlalchemy.orm import scoped_session import cg from cg.cli.add import add as add_cmd @@ -26,11 +27,19 @@ from cg.io.controller import ReadFile from cg.models.cg_config import CGConfig from cg.store.database import create_all_tables, drop_all_tables, get_tables +from cg.store.database import get_scoped_session_registry LOG = logging.getLogger(__name__) LEVELS = ["DEBUG", "INFO", "WARNING", "ERROR"] +def teardown_session(): + """Ensure that the session is closed and all resources are released to the connection pool.""" + registry: scoped_session | None = get_scoped_session_registry() + if registry: + registry.remove() + + @click.group() @click.option("-c", "--config", type=click.Path(exists=True), help="path to config file") @click.option("-d", "--database", help="path/URI of the SQL database") @@ -60,6 +69,7 @@ def base( else {"database": database} ) context.obj = CGConfig(**raw_config) + context.call_on_close(teardown_session) @base.command()