Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(tray): 改字重载先释放托盘动作锁再写终态

longmeidao93dc69013514
diff --git a/src/peach/dev_copy_runtime.py b/src/peach/dev_copy_runtime.py
index 8d5b45bb4..87618cf03 100644
--- a/src/peach/dev_copy_runtime.py
+++ b/src/peach/dev_copy_runtime.py
@@ -162,6 +162,8 @@ def poll(tray, root: Path) -> None:
return
def reload_services():
+ # 先释放托盘动作锁再写终态,读到终态的一方据此可以立即发起下一次托盘动作。
+ outcome = {'state': 'ready', 'message': '服务已重载,正在刷新…'}
try:
from .config import DATABASE_PATH
from .runtime_prepare import task_gate
@@ -173,11 +175,11 @@ def poll(tray, root: Path) -> None:
readiness = tray._readiness()
if readiness.problem:
raise ValueError(readiness.message)
- _write(root, {**job, 'state': 'ready', 'message': '服务已重载,正在刷新…'})
except Exception as exc:
- _write(root, {**job, 'state': 'error', 'message': str(exc)})
+ outcome = {'state': 'error', 'message': str(exc)}
finally:
tray._action_lock.release()
+ _write(root, {**job, **outcome})
try:
threading.Thread(target=reload_services, daemon=True, name='PeachCopyReload').start()
except RuntimeError as exc:
diff --git a/tests/test_dev_copy.py b/tests/test_dev_copy.py
index 566304ad5..21bbb0a69 100644
--- a/tests/test_dev_copy.py
+++ b/tests/test_dev_copy.py
@@ -98,12 +98,20 @@ class DevCopyTests(unittest.TestCase):
tray = SimpleNamespace(_action_lock=threading.Lock(), manager=Mock(),
_readiness=lambda: Step('readiness', 'current', '服务已就绪'))
gate = TaskGate(blocking=(ActiveRun(1, 'test', 'manual', 'running', os.getpid(), None),)) if blocked else TaskGate()
- with patch('peach.runtime_prepare.task_gate', return_value=gate):
+ locked_at_terminal = []
+ def recording_write(target, data, write=runtime._write, lock=tray._action_lock, seen=locked_at_terminal):
+ if data.get('state') in ('ready', 'error'):
+ seen.append(lock.locked())
+ return write(target, data)
+ with patch('peach.runtime_prepare.task_gate', return_value=gate), \
+ patch.object(runtime, '_write', side_effect=recording_write):
runtime.poll(tray, root)
self._wait_for_state(runtime, root, 'error' if blocked else 'ready')
self.assertEqual(tray.manager.restart.call_count, 0 if blocked else 1)
if not blocked:
tray.manager.restart.assert_called_once_with(reason='源码改字构建完成,重载服务')
+ # 终态写盘时托盘动作锁已经释放,看到终态的调用方不会抢在释放之前。
+ self.assertEqual(locked_at_terminal, [False])
self.assertFalse(tray._action_lock.locked())
@staticmethod