Removed useless lock.

This commit is contained in:
2026-06-13 00:55:05 +00:00
parent ebfed10f24
commit 43f2cdbd53
+13 -23
View File
@@ -5,7 +5,6 @@ from __future__ import annotations
import asyncio import asyncio
import copy import copy
import logging import logging
import threading
from collections import deque from collections import deque
from collections.abc import Callable from collections.abc import Callable
from datetime import UTC, datetime from datetime import UTC, datetime
@@ -31,7 +30,6 @@ class PersistenceMixin:
flush_failed: bool flush_failed: bool
statedict: dict[str, Any] statedict: dict[str, Any]
pending_changes: deque[ChangeRecord] pending_changes: deque[ChangeRecord]
pending_lock: threading.Lock
snapshot: SnapshotState snapshot: SnapshotState
serializer: Serializer serializer: Serializer
framer: Framer framer: Framer
@@ -55,7 +53,6 @@ class PersistenceMixin:
self.flush_failed = False self.flush_failed = False
self.statedict = {} self.statedict = {}
self.pending_changes = deque() self.pending_changes = deque()
self.pending_lock = threading.Lock()
self.serializer = serializer or JsonSerializer() self.serializer = serializer or JsonSerializer()
self.framer = self.serializer.framer_cls() self.framer = self.serializer.framer_cls()
self.snapshot = SnapshotState(serializer=self.serializer, framer=self.framer) self.snapshot = SnapshotState(serializer=self.serializer, framer=self.framer)
@@ -139,8 +136,7 @@ class PersistenceMixin:
m=m, m=m,
diff=diff, diff=diff,
) )
with self.pending_lock: self.pending_changes.append(record)
self.pending_changes.append(record)
self.statedict = copy.deepcopy(current) self.statedict = copy.deepcopy(current)
if m is not None: if m is not None:
self.mtime = m self.mtime = m
@@ -158,10 +154,9 @@ class PersistenceMixin:
if self.flush_failed: if self.flush_failed:
return return
with self.pending_lock: if not self.pending_changes:
if not self.pending_changes: return
return changes_to_write = list(self.pending_changes)
changes_to_write = list(self.pending_changes)
if not self.file.is_open: if not self.file.is_open:
self.file.open(self.filename, create=True) self.file.open(self.filename, create=True)
@@ -178,15 +173,13 @@ class PersistenceMixin:
records.append(framed) records.append(framed)
running_size += len(framed) running_size += len(framed)
if not records: if not records:
with self.pending_lock: self.pending_changes.clear()
self.pending_changes.clear()
return return
self.file.write(b"".join(records)) self.file.write(b"".join(records))
self.snapshot.record_changes(len(records)) self.snapshot.record_changes(len(records))
with self.pending_lock: for _ in changes_to_write:
for _ in changes_to_write: self.pending_changes.popleft()
self.pending_changes.popleft()
except OSError as e: except OSError as e:
_logger.error("Failed to flush database: %s", e) _logger.error("Failed to flush database: %s", e)
self.flush_failed = True self.flush_failed = True
@@ -208,10 +201,9 @@ class PersistenceMixin:
if self.flush_failed: if self.flush_failed:
return return
with self.pending_lock: if not self.pending_changes:
if not self.pending_changes: return
return changes_to_write = list(self.pending_changes)
changes_to_write = list(self.pending_changes)
if not self.file.is_open: if not self.file.is_open:
await asyncio.to_thread(self.file.open, self.filename, create=True) await asyncio.to_thread(self.file.open, self.filename, create=True)
@@ -228,15 +220,13 @@ class PersistenceMixin:
records.append(framed) records.append(framed)
running_size += len(framed) running_size += len(framed)
if not records: if not records:
with self.pending_lock: self.pending_changes.clear()
self.pending_changes.clear()
return return
await asyncio.to_thread(self.file.write, b"".join(records)) await asyncio.to_thread(self.file.write, b"".join(records))
self.snapshot.record_changes(len(records)) self.snapshot.record_changes(len(records))
with self.pending_lock: for _ in changes_to_write:
for _ in changes_to_write: self.pending_changes.popleft()
self.pending_changes.popleft()
except OSError as e: except OSError as e:
_logger.error("Failed to flush database: %s", e) _logger.error("Failed to flush database: %s", e)
self.flush_failed = True self.flush_failed = True