File tree Expand file tree Collapse file tree 1 file changed +12
-0
lines changed
Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Original file line number Diff line number Diff line change 11# ruff: noqa: PLR6301
2+ import atexit
3+ import contextlib
24import re
35from abc import ABC , abstractmethod
46from collections .abc import Awaitable
1921from sqlspec .exceptions import NotFoundError
2022from sqlspec .statement import SQLStatement
2123from sqlspec .typing import ModelDTOT , StatementParameterType
24+ from sqlspec .utils .sync_tools import maybe_async_
2225
2326if TYPE_CHECKING :
2427 from contextlib import AbstractAsyncContextManager , AbstractContextManager
@@ -202,6 +205,15 @@ class SQLSpec:
202205
203206 def __init__ (self ) -> None :
204207 self ._configs : dict [Any , DatabaseConfigProtocol [Any , Any , Any ]] = {}
208+ # Register the cleanup handler to run at program exit
209+ atexit .register (self ._cleanup_pools )
210+
211+ def _cleanup_pools (self ) -> None :
212+ """Clean up all open database pools at program exit."""
213+ for config in self ._configs .values ():
214+ if config .support_connection_pooling and config .pool_instance is not None :
215+ with contextlib .suppress (Exception ):
216+ maybe_async_ (config .close_pool )()
205217
206218 @overload
207219 def add_config (self , config : "SyncConfigT" ) -> "type[SyncConfigT]" : ...
You can’t perform that action at this time.
0 commit comments