Skip to content

Commit cadd629

Browse files
fix: add custom loop for connection __del__ (#70)
* add custom loop for connection __del__ * close connection loop in the end * don't close connection twice
1 parent e659b54 commit cadd629

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/firebolt/db/connection.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from asyncio import new_event_loop
34
from functools import wraps
45
from inspect import cleandoc
56
from types import TracebackType
@@ -37,7 +38,7 @@ class Connection(AsyncBaseConnection):
3738
"""
3839
)
3940

40-
__slots__ = AsyncBaseConnection.__slots__ + ("_closing_lock",)
41+
__slots__ = AsyncBaseConnection.__slots__ + ("_closing_lock", "_loop")
4142

4243
cursor_class = Cursor
4344

@@ -46,6 +47,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
4647
# Holding this lock for write means that connection is closing itself.
4748
# cursor() should hold this lock for read to read/write state
4849
self._closing_lock = RWLockWrite()
50+
self._loop = new_event_loop()
4951

5052
@wraps(AsyncBaseConnection.cursor)
5153
def cursor(self) -> Cursor:
@@ -57,7 +59,9 @@ def cursor(self) -> Cursor:
5759
@wraps(AsyncBaseConnection._aclose)
5860
def close(self) -> None:
5961
with self._closing_lock.gen_wlock():
60-
return async_to_sync(super()._aclose)()
62+
if not self.closed:
63+
self._loop.run_until_complete(self._aclose())
64+
self._loop.close()
6165

6266
# Context manager support
6367
def __enter__(self) -> Connection:

0 commit comments

Comments
 (0)