From dc526273f01054b225c861aefb476f02b0a30cfa Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sat, 3 Jan 2026 16:25:55 +0800 Subject: [PATCH] fix --- astrbot/core/utils/shared_preferences.py | 43 +++++++++++++++++------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/astrbot/core/utils/shared_preferences.py b/astrbot/core/utils/shared_preferences.py index ccd394ee4..54edb3ba7 100644 --- a/astrbot/core/utils/shared_preferences.py +++ b/astrbot/core/utils/shared_preferences.py @@ -25,6 +25,8 @@ class SharedPreferences: t = threading.Thread(target=self._sync_loop.run_forever, daemon=True) t.start() + self._write_lock = threading.Lock() + async def get_async( self, scope: str, @@ -167,8 +169,11 @@ class SharedPreferences: raise ValueError( "scope_id and key cannot be None when getting a specific preference.", ) + scope = scope or "unknown" + scope_id = scope_id or "unknown" + result = asyncio.run_coroutine_threadsafe( - self.get_async(scope or "unknown", scope_id or "unknown", key, default), + self.get_async(scope, scope_id, key, default), self._sync_loop, ).result() @@ -190,21 +195,33 @@ class SharedPreferences: def put(self, key, value, scope: str | None = None, scope_id: str | None = None): """设置偏好设置(已弃用)""" - asyncio.run_coroutine_threadsafe( - self.put_async(scope or "unknown", scope_id or "unknown", key, value), - self._sync_loop, - ).result() + scope = scope or "unknown" + scope_id = scope_id or "unknown" + + with self._write_lock: + asyncio.run_coroutine_threadsafe( + self.put_async(scope, scope_id, key, value), + self._sync_loop, + ).result() def remove(self, key, scope: str | None = None, scope_id: str | None = None): """删除偏好设置(已弃用)""" - asyncio.run_coroutine_threadsafe( - self.remove_async(scope or "unknown", scope_id or "unknown", key), - self._sync_loop, - ).result() + scope = scope or "unknown" + scope_id = scope_id or "unknown" + + with self._write_lock: + asyncio.run_coroutine_threadsafe( + self.remove_async(scope, scope_id, key), + self._sync_loop, + ).result() def clear(self, scope: str | None = None, scope_id: str | None = None): """清空偏好设置(已弃用)""" - asyncio.run_coroutine_threadsafe( - self.clear_async(scope or "unknown", scope_id or "unknown"), - self._sync_loop, - ).result() + scope = scope or "unknown" + scope_id = scope_id or "unknown" + + with self._write_lock: + asyncio.run_coroutine_threadsafe( + self.clear_async(scope, scope_id), + self._sync_loop, + ).result()