fix: enforce native repair ownership and cancellation boundaries
This commit is contained in:
1 parent
2490067b2d
commit
9cd4968a97
8 files changed
+248
-10
No files matched your search
@@ -850,7 +850,9 @@ def execute_job(job, emit):
|
||||
elif not completed and identity_verified:
|
||||
try:
|
||||
from .failed_session import close_failed_document
|
||||
close_failed_document(job, emit=emit)
|
||||
failed_close = close_failed_document(job, emit=emit)
|
||||
if failed_close.get('status') == 'CLOSED' and lm_session is not None:
|
||||
lm_session.close()
|
||||
except Exception as error:
|
||||
emit(stage='failure_close_unconfirmed', detail=str(error))
|
||||
cleanup_verify_processes(
|
||||
|
||||
@@ -98,6 +98,9 @@ def backup_project(target, destination):
|
||||
if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns):
|
||||
raise OSError(f'REPAIR_BACKUP_CHANGED: 备份期间源文件变化:{path}')
|
||||
files[str(relative)] = {'size': before.st_size, 'sha256': digest}
|
||||
if {path.relative_to(source) for path in source.rglob('*')} != {
|
||||
path.relative_to(source) for path in paths}:
|
||||
raise OSError('REPAIR_BACKUP_CHANGED: 备份期间工程文件集合变化')
|
||||
record = {'complete': True, 'source': str(source), 'files': files}
|
||||
(destination/'backup-manifest.json').write_text(json.dumps(record, ensure_ascii=False, indent=2),
|
||||
encoding='utf-8')
|
||||
@@ -147,12 +150,13 @@ def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progr
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
job_path = root/'repair-request.json'
|
||||
cancel = root/'cancel-repair.signal'
|
||||
desktop = 'CbbRepair_'+uuid4().hex[:12]
|
||||
job_path.write_text(json.dumps({'project': str(target.project), 'database': str(target.database),
|
||||
'root': str(root), 'mentor_bin': str(mentor_bin or ''), 'timeout': timeout_seconds,
|
||||
'cancel_signal': str(cancel), 'supervisor_pid': os.getpid()}, ensure_ascii=False), encoding='utf-8')
|
||||
'cancel_signal': str(cancel), 'supervisor_pid': os.getpid(), 'desktop': desktop},
|
||||
ensure_ascii=False), encoding='utf-8')
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False) else
|
||||
[sys.executable, '-m', 'cbb_refresh_verify']) + ['--repair-worker', str(job_path)]
|
||||
desktop = 'CbbRepair_'+uuid4().hex[:12]
|
||||
worker = DesktopWorker(command, desktop)
|
||||
worker.bind_cancellation(cancel)
|
||||
deadline = time.monotonic()+max(1, float(timeout_seconds))+5
|
||||
|
||||
@@ -30,6 +30,22 @@ def repair_completed(diagnostic_text, database_text, *, native_backup):
|
||||
and 'iCDBProjectBackupRepair' in database_text and 'IC: Closing project' in database_text)
|
||||
|
||||
|
||||
def _current_desktop_name():
|
||||
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
user32 = ctypes.WinDLL('user32', use_last_error=True)
|
||||
kernel32.GetCurrentThreadId.restype = wintypes.DWORD
|
||||
user32.GetThreadDesktop.argtypes = [wintypes.DWORD]
|
||||
user32.GetThreadDesktop.restype = wintypes.HANDLE
|
||||
user32.GetUserObjectInformationW.argtypes = [wintypes.HANDLE, ctypes.c_int,
|
||||
wintypes.LPVOID, wintypes.DWORD, ctypes.POINTER(wintypes.DWORD)]
|
||||
desktop = user32.GetThreadDesktop(kernel32.GetCurrentThreadId())
|
||||
name = ctypes.create_unicode_buffer(512)
|
||||
required = wintypes.DWORD()
|
||||
if not user32.GetUserObjectInformationW(desktop, 2, name, ctypes.sizeof(name), ctypes.byref(required)):
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
return name.value
|
||||
|
||||
|
||||
def _repair_command(executable):
|
||||
import win32api, win32gui, win32con
|
||||
user32 = ctypes.WinDLL('user32')
|
||||
@@ -124,6 +140,13 @@ def repair_worker_main(job_path):
|
||||
with (root/'repair-events.jsonl').open('a', encoding='utf-8') as stream:
|
||||
stream.write(json.dumps(event, ensure_ascii=False)+'\n')
|
||||
try:
|
||||
if (not str(job.get('desktop', '')).startswith('CbbRepair_')
|
||||
or _current_desktop_name() != job['desktop']):
|
||||
raise ValueError('REPAIR_PRIVATE_DESKTOP_REQUIRED')
|
||||
if not job.get('supervisor_pid'):
|
||||
raise ValueError('REPAIR_SUPERVISOR_REQUIRED')
|
||||
from .desktop_worker import start_supervisor_watchdog
|
||||
start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job['cancel_signal'])
|
||||
if project.suffix.casefold() != '.prj' or database.resolve() != (project.parent/'database').resolve():
|
||||
raise ValueError('REPAIR_TARGET_UNCONFIRMED')
|
||||
home = discover_sdd_home(job.get('mentor_bin'))
|
||||
@@ -140,9 +163,6 @@ def repair_worker_main(job_path):
|
||||
raise ValueError('REPAIR_CANCELLED_AFTER_BACKUP')
|
||||
baseline = snapshot_log(project.parent/'Log Files/ProjectBackupLog.txt')
|
||||
started_ns = time.time_ns()
|
||||
if job.get('supervisor_pid'):
|
||||
from .desktop_worker import start_supervisor_watchdog
|
||||
start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job['cancel_signal'])
|
||||
process = subprocess.Popen([str(executable), '-p', str(project)], cwd=str(project.parent))
|
||||
result['native_pid'] = process.pid
|
||||
emit(stage='native_started', pid=process.pid, project=str(project))
|
||||
@@ -157,7 +177,7 @@ def repair_worker_main(job_path):
|
||||
if not invoked and main and not dialogs and main['enabled']:
|
||||
if Path(job['cancel_signal']).exists():
|
||||
raise ValueError('REPAIR_CANCELLED_BEFORE_START')
|
||||
win32gui.PostMessage(main['hwnd'], 0x0111, command_id, 0)
|
||||
win32gui.PostMessage(main['hwnd'], win32con.WM_COMMAND, command_id, 0)
|
||||
invoked = True
|
||||
emit(stage='native_repair_requested', command=command_id)
|
||||
if invoked and not confirmed and main:
|
||||
|
||||
Reference in new issue
Block a user