fix: fence final worker admission against UI shutdown

This commit is contained in:
阿白叔 committed 2026-11-17 00:00:50 +08:00
1 parent dbc685b9a5
commit eabf61c73b
13 files changed
+356 -46

No files matched your search

+24 -6
View File
@@ -53,9 +53,10 @@ def removal_output_ok(code, output, name, partition):
+ r"' from partition '" + re.escape(partition) + r"'\.", output) is not None)
def catalog_dialog_action(title, text):
def catalog_dialog_action(title, text, *, read_only=False):
value = (title + '\n' + text).casefold()
if ('this central library must be updated' in value or 'select flow type' in value):
if ('this central library must be updated' in value or 'select flow type' in value
or read_only and ('save' in value or 'upgrade' in value)):
return 'cancel'
return 'observe'
@@ -222,6 +223,7 @@ class NativeManagementAdapter:
from .session_record import process_identity
job = {**job, 'cancel_signal': str(evidence/'cancel-management.signal'),
'start_gate': str(evidence/'start-management.signal'),
'start_gate_protocol': 'event-v1',
'session_record_path': str(evidence/'session.json'), 'operation_id': uuid4().hex,
'supervisor_pid': os.getpid(), 'supervisor_identity': process_identity(os.getpid()),
'block': job.get('name')}
@@ -242,7 +244,8 @@ class NativeManagementAdapter:
worker.bind_cancellation(job['cancel_signal'])
if hasattr(worker, 'bind_session'):
worker.bind_session(job)
Path(job['start_gate']).touch()
from .operation_queue import commit_worker_start
commit_worker_start(worker, job)
while worker.poll() is None and time.monotonic() < deadline:
if self.stop is not None and self.stop.is_set():
Path(job['cancel_signal']).touch()
@@ -275,7 +278,8 @@ class NativeManagementAdapter:
worker.close()
def catalog(self, library, evidence):
result = self._worker({'operation': 'catalog', 'library': str(absolute_preserving_drive(library))}, evidence)
result = self._worker({'operation': 'catalog', 'worker_role': 'catalog_readonly',
'library': str(absolute_preserving_drive(library))}, evidence)
if not result.get('success') or not result.get('safe_to_continue'):
raise NativeOperationError(result.get('detail', '原生目录复读未确认,禁止文件夹回退。'),
safe_to_continue=result.get('safe_to_continue') is True)
@@ -1069,6 +1073,8 @@ def _catalog_native(job, root):
def _catalog_com(job, root):
if job.get('worker_role') == 'catalog_readonly' and job.get('operation') != 'catalog':
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
import pythoncom, win32com.client, win32con, win32gui, win32process
from .mentor_adapter import _process_details
try:
@@ -1102,7 +1108,7 @@ def _catalog_com(job, root):
win32gui.EnumChildWindows(hwnd, lambda child, _: children.append(child), None)
title = win32gui.GetWindowText(hwnd)
text = '\n'.join(win32gui.GetWindowText(child) for child in children)
if catalog_dialog_action(title, text) == 'cancel':
if catalog_dialog_action(title, text, read_only=job.get('worker_role') == 'catalog_readonly') == 'cancel':
if hwnd not in observed:
observed.append(hwnd)
refused.append({'title': title, 'text': text})
@@ -1236,11 +1242,23 @@ def _catalog_com(job, root):
return result
def management_worker_main(request_path):
def catalog_worker_main(request_path):
"""A catalog-only entry: never allow this read role to select a write branch."""
job = json.loads(Path(request_path).read_text(encoding='utf-8'))
if job.get('operation') != 'catalog' or job.get('worker_role') != 'catalog_readonly':
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
return management_worker_main(request_path, catalog_readonly=True)
def management_worker_main(request_path, *, catalog_readonly=False):
"""Internal worker. Desktop identity and capability gates are rechecked here."""
request_path = Path(request_path)
root = request_path.parent
job = json.loads(request_path.read_text(encoding='utf-8'))
if job.get('worker_role') == 'catalog_readonly' and not catalog_readonly:
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
if catalog_readonly and (job.get('operation') != 'catalog' or job.get('worker_role') != 'catalog_readonly'):
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
result = {'success': False, 'safe_to_continue': True}
try:
_, desktop = _current_desktop()