Skip to content

Commit f9edfd6

Browse files
committed
More attention to closing Persistence
Avoid ResourceWarning from unclosed SQLite connection.
1 parent 5bc4acb commit f9edfd6

File tree

4 files changed

+381
-357
lines changed

4 files changed

+381
-357
lines changed

ohmqtt/persistence/base.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from abc import ABCMeta, abstractmethod
2+
import sys
23
from typing import ClassVar, NamedTuple, Sequence
34

45
from ..handles import PublishHandle
@@ -13,6 +14,11 @@
1314
from ..property import MQTTPublishProps
1415
from ..topic_alias import AliasPolicy
1516

17+
if sys.version_info >= (3, 11):
18+
from typing import Self
19+
else:
20+
from typing_extensions import Self
21+
1622

1723
class LostMessageError(Exception):
1824
"""Raised when a message is lost from the persistence store and can not be acknowledged."""
@@ -28,6 +34,12 @@ class Persistence(metaclass=ABCMeta):
2834
"""Abstract base class for message persistence."""
2935
__slots__: ClassVar[Sequence[str]] = tuple()
3036

37+
def __enter__(self) -> Self:
38+
return self
39+
40+
def __exit__(self, exc_type: type | None, exc_value: BaseException | None, traceback: object | None) -> None:
41+
self.close()
42+
3143
@abstractmethod
3244
def __len__(self) -> int:
3345
"""Return the number of outgoing messages in the persistence store."""
@@ -90,3 +102,9 @@ def open(self, client_id: str, clear: bool = False) -> None:
90102
91103
This may clear the persistence store if the client_id is different from the persisted,
92104
or if clear is True."""
105+
106+
@abstractmethod
107+
def close(self) -> None:
108+
"""Finalize and close the persistence store.
109+
110+
The store must not be used after this call."""

ohmqtt/persistence/in_memory.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,6 @@ def open(self, client_id: str, clear: bool = False) -> None:
177177
self._client_id = client_id
178178
else:
179179
self._reset_inflight()
180+
181+
def close(self) -> None:
182+
self.clear()

ohmqtt/persistence/sqlite.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ def __init__(self, db_path: str, *, db_fast: bool = False) -> None:
5454
self._create_tables()
5555

5656
def __del__(self) -> None:
57-
# Ignore "already closed" errors while closing resources.
58-
with suppress(sqlite3.ProgrammingError):
59-
self._cursor.close()
60-
with suppress(sqlite3.ProgrammingError):
61-
self._conn.close()
57+
self.close()
6258

6359
def __len__(self) -> int:
6460
with self._cond:
@@ -333,3 +329,10 @@ def open(self, client_id: str, clear: bool = False) -> None:
333329
self.clear()
334330
else:
335331
self._reset_inflight()
332+
333+
def close(self) -> None:
334+
# Ignore "already closed" errors while closing resources.
335+
with suppress(sqlite3.ProgrammingError):
336+
self._cursor.close()
337+
with suppress(sqlite3.ProgrammingError):
338+
self._conn.close()

0 commit comments

Comments
 (0)