diff --git a/src/tribler/core/database/layers/knowledge.py b/src/tribler/core/database/layers/knowledge.py index 7522b04209..c1644ab70f 100644 --- a/src/tribler/core/database/layers/knowledge.py +++ b/src/tribler/core/database/layers/knowledge.py @@ -8,7 +8,7 @@ from pony import orm from pony.orm import raw_sql -from pony.orm.core import Database, Entity, Query, select +from pony.orm.core import Database, Entity, Query, UnrepeatableReadError, select from pony.utils import between from tribler.core.database.layers.layer import EntityImpl, Layer @@ -113,7 +113,7 @@ class StatementOp(EntityImpl): updated_at: datetime.datetime auto_generated: bool - def __init__(self, statement: Statement, peer: Peer, operation: int, clock: int, # noqa: D107, PLR0913 + def __init__(self, statement: Statement, peer: Peer, operation: int, clock: int, # noqa: D107 signature: bytes, auto_generated: bool) -> None: ... @staticmethod @@ -278,7 +278,7 @@ def _get_resources(self, resource_type: ResourceType | None, name: str | None, c results = results.filter(lambda r: r.type == resource_type.value) return results - def get_statements(self, source_type: ResourceType | None, source_name: str | None, # noqa: PLR0913 + def get_statements(self, source_type: ResourceType | None, source_name: str | None, statements_getter: Callable[[Entity], Entity], target_condition: Callable[[Statement], bool], condition: Callable[[Statement], bool], case_sensitive: bool, ) -> Iterator[Statement]: @@ -443,9 +443,14 @@ def get_simple_statements(self, subject_type: ResourceType | None = None, subjec case_sensitive=case_sensitive, ) - return [SimpleStatement(subject_type=s.subject.type, subject=s.subject.name, predicate=s.object.type, - object=s.object.name) - for s in statements] + results = [] + for s in statements: + try: + results.append(SimpleStatement(subject_type=s.subject.type, subject=s.subject.name, + predicate=s.object.type, object=s.object.name)) + except UnrepeatableReadError as e: + self.logger.exception(e) + return results def get_suggestions(self, subject_type: ResourceType | None = None, subject: str | None = "", predicate: ResourceType | None = None, case_sensitive: bool = True) -> List[str]: diff --git a/src/tribler/core/knowledge/community.py b/src/tribler/core/knowledge/community.py index 9a636a9eb7..d769308447 100644 --- a/src/tribler/core/knowledge/community.py +++ b/src/tribler/core/knowledge/community.py @@ -6,7 +6,7 @@ from ipv8.keyvault.private.libnaclkey import LibNaCLSK from ipv8.lazy_community import lazy_wrapper from ipv8.types import Key, Peer -from pony.orm import db_session +from pony.orm import UnrepeatableReadError, db_session from tribler.core.database.layers.knowledge import Operation, ResourceType from tribler.core.database.tribler_database import TriblerDatabase @@ -128,6 +128,8 @@ def on_request(self, peer: Peer, operation: RequestStatementOperationMessage) -> sent_operations.append(operation) except ValueError as e: # validation error self.logger.warning(e) + except UnrepeatableReadError as e: # database error + self.logger.exception(e) if sent_operations: sent_tags_info = ", ".join(f"({t})" for t in sent_operations) self.logger.debug("-> sent operations (%s) to peer: %s", sent_tags_info, peer.mid.hex())