"""Uploader-compatible exclusive sidecar lease. Metadata never proves ownership.""" from datetime import datetime, timezone from contextlib import contextmanager, ExitStack import json import re import os from pathlib import Path import socket import threading from uuid import uuid4 _guard = threading.RLock() _owners = {} _library_guards = {} def _library_guard(key): """Serialize one lease without making slow resource probes global.""" with _guard: return _library_guards.setdefault(key, threading.RLock()) def duplicate_worker_lease(library, process_handle): """Keep the exact admitted exclusive file object alive across parent exit.""" import win32api, win32con lease = LibraryLock(library) with _library_guard(lease.key): owner = _owners.get(lease.key) if owner is None: return None if owner['thread'] != threading.get_ident() or owner['retained'] or not owner['depth']: raise LibraryBusy('不能将其他任务的库锁交给工作进程。') remote = win32api.DuplicateHandle(win32api.GetCurrentProcess(), owner['handle'], process_handle, 0, False, win32con.DUPLICATE_SAME_ACCESS) return remote.Detach() # This handle belongs to the target process only. class LibraryBusy(OSError): pass def inspect_residual_sessions(library, *, block_names=()): from .residual_sessions import inspect_residual_sessions as inspect return inspect(library, block_names=block_names) def release_residual_sessions(plan, evidence_dir, *, stop=None, force=False): from .residual_sessions import release_residual_sessions as release return release(plan, evidence_dir, stop=stop, force=force) def _key(path): return str(Path(path).resolve()).casefold() def blocker_path(library): library = Path(library).resolve() return library.with_name('.' + library.stem + '.cbb-upload.blocked.json') def checkpoint_record(path): """Recognize only our reserved filename and complete versioned protocol.""" path = Path(path) if not (path.name.endswith('.cbb-upload.blocked.json') or path.name.endswith('.cbb-upload.blocked.pending')): return None try: import stat if path.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT: return None record = json.loads(path.read_text(encoding='utf-8')) library = Path(record['library']) expected = '.' + library.stem + '.cbb-upload.blocked' if (record.get('protocol') != 'cbb-refresh-verify-library-lease' or record.get('version') != 1 or not library.is_absolute() or library.suffix.casefold() != '.lmc' or path.name not in (expected+'.json', expected+'.pending') or not re.fullmatch('[0-9a-f]{32}', str(record.get('lease_id', ''))) or not isinstance(record.get('pid'), int) or record['pid'] <= 0 or record.get('status') not in ('OPERATION_IN_PROGRESS', 'OWNER_RELEASE_UNCONFIRMED') or not isinstance(record.get('time'), str)): return None return record except (OSError, ValueError, TypeError, KeyError): return None def has_blocker(library): path = blocker_path(library) # Complete snapshots retain every file, including their source lease # checkpoint. Only a valid relocated snapshot record can be ignored. for marker in (path, path.with_suffix('.pending')): if marker.exists(): record = checkpoint_record(marker) if record is None or _key(record['library']) == _key(library): return True return False def has_retained_library(library): """Include leases retained without a JSON checkpoint, such as PCB checks.""" path = Path(library).resolve() key = _key(path.with_name('.' + path.stem + '.cbb-upload.lock')) with _guard: owner = _owners.get(key) return bool(owner and owner['retained']) def recheck_previous_release(library, evidence_dir, *, block_name=None, required=False, progress=None, stop=None): """Historical records trigger fresh proof, never process termination or repair.""" if not required: with _library_guard(LibraryLock(library).key): owner = _owners.get(LibraryLock(library).key) marker = blocker_path(library) record = checkpoint_record(marker) if (owner and owner['thread'] == threading.get_ident() and owner['depth'] > 0 and not owner['retained'] and not marker.with_suffix('.pending').exists() and record and record['lease_id'] == owner['lease_id'] and record['pid'] == os.getpid() and record['status'] == 'OPERATION_IN_PROGRESS' and record.get('block_name') == owner.get('block_name') and owner.get('block_name') in (None, block_name) and 'export_analysis' not in record): return None # Nested Verify belongs to this live transaction, not a past crash. if not (required or has_blocker(library) or has_retained_library(library)): return None def notify(stage, **details): if progress is not None: try: progress({'stage': stage, 'block': block_name or '', **details}) except Exception: import logging logging.getLogger(__name__).warning('Release progress observer failed', exc_info=True) notify('previous_release_check', detail='正在复核历史记录对应的实际占用') try: options = {'stop': stop} if stop is not None else {} proof = release_retained_library(library, evidence_dir, block_name=block_name, **options) except Exception as error: notify('previous_release_unconfirmed', detail=str(error)) raise from .operation_queue import note_metadata_progress note_metadata_progress() notify('previous_release_confirmed', release_proof=proof, detail='已确认会话释放并归档历史记录;这不表示历史数据已恢复') return proof def _persist_blocker(library, status='OWNER_RELEASE_UNCONFIRMED', *, lease_id, block_name=None): import win32api, win32process path = blocker_path(library) pending = path.with_suffix('.pending') previous = checkpoint_record(path) export_record = ({'export_analysis': previous['export_analysis']} if previous and previous['lease_id'] == lease_id and 'export_analysis' in previous else {}) if block_name is not None: export_record['block_name'] = block_name elif previous and previous['lease_id'] == lease_id and 'block_name' in previous: export_record['block_name'] = previous['block_name'] from .icdb_ownership import session_resource_scope scope = (previous.get('resource_scope') if previous and previous['lease_id'] == lease_id else None) scope = scope or session_resource_scope({'library': library, 'block': export_record.get('block_name')}) with pending.open('w', encoding='utf-8') as stream: json.dump({**export_record, 'protocol': 'cbb-refresh-verify-library-lease', 'version': 1, 'lease_id': lease_id, 'library': str(library), 'status': status, 'lease_state': 'ACTIVE' if status == 'OPERATION_IN_PROGRESS' else 'RETAINED', 'resource_scope': scope, 'blocked_resources': [], 'data_recovery_required': None, 'machine': socket.gethostname(), 'supervisor_created': win32process.GetProcessTimes(win32api.GetCurrentProcess())['CreationTime'].isoformat(), 'pid': os.getpid(), 'time': datetime.now(timezone.utc).isoformat()}, stream) stream.flush(); os.fsync(stream.fileno()) pending.replace(path) class LibraryLock: def __init__(self, library, *, safe_to_release=None, checkpoint=True, block_name=None): library = Path(library).resolve() self.library = library self.path = library.with_name('.' + library.stem + '.cbb-upload.lock') self.key = _key(self.path) self.entered = False self.safe_to_release = safe_to_release self.checkpoint = checkpoint self.block_name = block_name self.maintenance_guard = None self.maintenance_mutex = None @classmethod def for_maintenance(cls, library, archive, guard, *, safe_to_release=None): lease = cls(library, safe_to_release=safe_to_release) lease.maintenance_guard = guard lease.maintenance_archive = Path(archive) return lease def __enter__(self): with _library_guard(self.key): owner = _owners.get(self.key) if owner and owner['retained'] and owner['depth'] == 0 and owner.get('release_probe'): try: released = owner['release_probe']() is True except Exception: released = False if released: if owner.get('checkpoint', True): marker = blocker_path(self.library) record = checkpoint_record(marker) if (record is None or record['lease_id'] != owner['lease_id'] or 'export_analysis' in record or marker.with_suffix('.pending').exists()): raise LibraryBusy('协调检查点身份或导出会话尚未确认,保持库锁。') marker.unlink() owner['handle'].Close() del _owners[self.key] owner = None if self.maintenance_guard is not None: return _enter_maintenance(self) import win32con, win32file with _library_guard(self.key): owner = _owners.get(self.key) if owner: if owner['thread'] != threading.get_ident() or owner['retained']: raise LibraryBusy(f'中心库被其他任务占用:{self.path}') owner['depth'] += 1 else: if self.checkpoint and has_blocker(self.library): raise LibraryBusy(f'中心库存在未解除的会话阻止记录,请先执行释放检查:{blocker_path(self.library)}') try: lease_id = uuid4().hex handle = win32file.CreateFile(str(self.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) except Exception as error: if getattr(error, 'winerror', None) not in (32, 33): raise raise LibraryBusy(f'中心库已锁定或无法创建协调锁:{self.path}: {error}') from error try: data = (f'machine={socket.gethostname()}; pid={os.getpid()}; ' f'startedUtc={datetime.now(timezone.utc).isoformat()}; ' f'library={self.library}').encode('utf-8') win32file.SetFilePointer(handle, 0, win32con.FILE_BEGIN) win32file.SetEndOfFile(handle) win32file.WriteFile(handle, data) win32file.FlushFileBuffers(handle) # Closing flushes the last-write timestamp on Windows. Set # it explicitly before complete-copy metadata is captured. import pywintypes win32file.SetFileTime(handle, None, None, pywintypes.Time(datetime.now())) if self.checkpoint: _persist_blocker(self.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id, block_name=self.block_name) except Exception: handle.Close() raise _owners[self.key] = {'thread': threading.get_ident(), 'depth': 1, 'handle': handle, 'retained': False, 'lease_id': lease_id, 'checkpoint': self.checkpoint, 'block_name': self.block_name, 'release_probe': self.safe_to_release} self.entered = True return self def snapshot_bytes(self): data = owned_snapshot(self.path) if data is None: raise LibraryBusy('当前线程不拥有此库协调锁') return data def retain(self): with _guard: if self.entered: _owners[self.key]['retained'] = True def __exit__(self, *_): try: self._exit_owned() finally: if self.maintenance_mutex is not None: import win32event win32event.ReleaseMutex(self.maintenance_mutex) self.maintenance_mutex.Close() self.maintenance_mutex = None def _exit_owned(self): with _library_guard(self.key): if not self.entered: return owner = _owners[self.key] if not owner.get('checkpoint', True): if self.safe_to_release is not None: try: released = self.safe_to_release() is True except Exception: released = False if not released: owner['retained'] = True owner['depth'] -= 1 self.entered = False if owner['depth'] == 0 and not owner['retained']: owner['handle'].Close() del _owners[self.key] return checkpoint = checkpoint_record(blocker_path(self.library)) if checkpoint is not None and 'export_analysis' in checkpoint: owner['retained'] = True if self.safe_to_release is not None: try: safe = self.safe_to_release() is True except Exception: safe = False if not safe: # A supervisor can time out while its native child still # owns the DB. Keep excluding the uploader until app exit. owner['retained'] = True owner['depth'] -= 1 self.entered = False if owner['depth'] == 0 and not owner['retained']: # This JSON is our durable operation checkpoint, not the # uploader's .lock file. Clear only after proven normal release. marker = blocker_path(self.library) record = checkpoint_record(marker) if marker.exists() and (record is None or record['lease_id'] != owner['lease_id']): owner['retained'] = True raise LibraryBusy('协调检查点被替换或损坏,保留冲突文件及库锁。') try: marker.unlink(missing_ok=True) except OSError: owner['retained'] = True raise owner['handle'].Close() del _owners[self.key] elif owner['retained']: marker = blocker_path(self.library) record = checkpoint_record(marker) if marker.exists() and (record is None or record['lease_id'] != owner['lease_id']): raise LibraryBusy('协调检查点被替换或损坏,保留冲突文件及库锁。') _persist_blocker(self.library, lease_id=owner['lease_id']) def _supervisor_alive(pid): import pywintypes, win32api, win32con, win32event try: handle = win32api.OpenProcess(win32con.SYNCHRONIZE, False, int(pid)) except pywintypes.error as error: if error.winerror == 87: return False raise LibraryBusy('无法确认原监督进程退出') from error try: return win32event.WaitForSingleObject(handle, 0) != win32con.WAIT_OBJECT_0 finally: handle.Close() def _recorded_supervisor_alive(record): """None means legacy/unknown identity; the native/file guard still applies.""" if record.get('machine') != socket.gethostname() or not record.get('supervisor_created'): return None import pywintypes, win32api, win32con, win32process try: handle = win32api.OpenProcess(win32con.SYNCHRONIZE | 0x1000, False, record['pid']) except pywintypes.error as error: return False if error.winerror == 87 else None try: return (win32process.GetProcessTimes(handle)['CreationTime'].isoformat() == record['supervisor_created'] and _supervisor_alive(record['pid'])) finally: handle.Close() def _owner_matches_scope(item, library, roots): """False requires an explicit other project; a missing path stays unknown.""" import ntpath from .failed_session import _command_args command = str(item.get('command') or '') if not command: return None try: args = _command_args(command) except (OSError, ValueError): return None candidates = [] worker_flags = {'--verify-worker', '--repair-worker', '--management-worker', '--native-backup-worker'} for index, arg in enumerate(args[1:], 1): if arg.casefold() in worker_flags and index + 1 < len(args): try: request = Path(args[index + 1]) if request.stat().st_size > 1024 * 1024: return None record = json.loads(request.read_text(encoding='utf-8')) for key in ('library', 'lmc_file', 'project', 'database'): if isinstance(record.get(key), str): candidates.append(record[key]) except (OSError, ValueError, TypeError, AttributeError): return None value = arg.split('=', 1)[1] if arg.startswith('-') and '=' in arg else arg if (ntpath.isabs(value) and (ntpath.splitext(value)[1].casefold() in ('.prj', '.pcb', '.lmc') or arg.casefold().startswith(('-project=', '-icdb=')) or args[index - 1].casefold() in ('-icdb', '-project', '-p'))): candidates.append(value) normalized_roots = [ntpath.normcase(ntpath.normpath(str(root))) for root in roots] expected_library = ntpath.normcase(ntpath.normpath(str(library))) for candidate in candidates: path = ntpath.normcase(ntpath.normpath(candidate)) if path == expected_library: return True for root in normalized_roots: try: if ntpath.commonpath((path, root)) == root: return True except ValueError: pass return False if candidates else None def _owner_is_blocking(item, library, roots): """Unknown external clients need resource proof, not a global exit rule.""" scope = _owner_matches_scope(item, library, roots) if scope is not None: return scope command = str(item.get('command') or '').casefold() flags = ('--verify-worker', '--repair-worker', '--management-worker', '--native-backup-worker') if any(flag in command for flag in flags): return True # An unreadable in-flight request is not proof of another library. from .failed_session import _command_args try: return any(Path(arg).name.casefold() in ('close_failed_block.vbs', 'save_verified_block.vbs') for arg in _command_args(command)[1:]) except (OSError, ValueError): return False def _enter_maintenance(lease): """Transfer only a valid known lease, after project admission under lock.""" import win32con, win32file from .operation_queue import check_active handle = None existing = None try: check_active() with _library_guard(lease.key): marker = blocker_path(lease.library) if marker.with_suffix('.pending').exists(): raise LibraryBusy('未完成检查点写入,不能自动接续') old = marker.read_bytes() if marker.exists() else None record = checkpoint_record(marker) if old is not None else None if old is not None and (record is None or _key(record['library']) != _key(lease.library)): raise LibraryBusy('维护拒绝未知、损坏或其他库的检查点') existing = _owners.get(lease.key) if existing: if (not existing['retained'] or existing['depth'] != 0 or record is None or record['pid'] != os.getpid() or record['lease_id'] != existing['lease_id']): raise LibraryBusy('本进程库锁仍在使用或检查点身份不符') handle = existing['handle'] else: if record is not None and record['pid'] != os.getpid() and _recorded_supervisor_alive(record) is True: raise LibraryBusy('原监督进程仍存在,不能接续其库锁') handle = win32file.CreateFile(str(lease.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) # All target/process/address/exclusive-file checks finish BEFORE # any old checkpoint replacement. Keep the exact sidecar open. from .native_export_guard import prove_export_copies_released if record is not None and _recorded_supervisor_alive(record) is None: from .maintenance_entry import assert_native_idle assert_native_idle() with prove_export_copies_released(lease.library), lease.maintenance_guard() as proof: if (marker.read_bytes() if marker.exists() else None) != old: raise LibraryBusy('维护检查期间检查点变化') if old is not None: lease.maintenance_archive.mkdir(parents=True, exist_ok=False) saved = lease.maintenance_archive/'previous-checkpoint.json' with saved.open('xb') as stream: stream.write(old); stream.flush(); os.fsync(stream.fileno()) if saved.read_bytes() != old: raise OSError('原检查点归档校验失败') check_active() lease_id = uuid4().hex _persist_blocker(lease.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id) _owners[lease.key] = {'thread': threading.get_ident(), 'depth': 1, 'handle': handle, 'retained': False, 'lease_id': lease_id, 'checkpoint': True, 'release_probe': lease.safe_to_release, 'block_name': None} lease.maintenance_proof = proof lease.entered = True return lease except Exception: if handle is not None and existing is None: handle.Close() raise def owned_snapshot(path): """Read only a lease held by this thread, never reopen an exclusive file.""" import win32con, win32file with _library_guard(_key(path)): owner = _owners.get(_key(path)) if not owner or owner['thread'] != threading.get_ident() or owner['retained']: return None handle = owner['handle'] position = win32file.SetFilePointer(handle, 0, win32con.FILE_CURRENT) try: win32file.SetFilePointer(handle, 0, win32con.FILE_BEGIN) size = win32file.GetFileSize(handle) return win32file.ReadFile(handle, size)[1] if size else b'' finally: win32file.SetFilePointer(handle, position, win32con.FILE_BEGIN) @contextmanager def _prove_library_released(library, block_name=None, *, block_names=(), allow_unknown_checkpoint=False, stop=None): """Hold all native database read handles during explicit lease release.""" from .operation_queue import note_metadata_progress from .owner_probe import query_owners from .native_management import _retained_workers from .native_export_guard import assert_retained_exports_idle, prove_export_copies_released from .native_engineering_identity import _OWNER_NAMES library = Path(library).resolve() assert_retained_exports_idle(library) if any(worker.poll() is None and (not getattr(worker, 'cbb_library', None) or _key(worker.cbb_library) == _key(library)) for worker in _retained_workers): raise LibraryBusy('原生管理工作进程尚未返回,不能解除协调阻止。') from .com_runtime import initialize_com, uninitialize_com initialized = initialize_com() try: names = ('LibraryManager.exe', 'ExpeditionPCB.exe', 'viewdraw.exe', 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe', 'cscript.exe', 'wscript.exe', 'python.exe', 'pythonw.exe', 'CbbRefreshVerify.exe', *sorted(_OWNER_NAMES)) blocks = tuple(dict.fromkeys(block_names or ((block_name,) if block_name else ()))) if any(not name or name in ('.', '..') or any(c in name for c in '\\/:*?"<>|') for name in blocks): raise ValueError('释放检查的工程名称无效') scope_roots = tuple(library.parent/'ReusableBlocksLP'/name/'Work' for name in blocks) shared_root = Path(library).resolve().parent/'SymbolLibs' if allow_unknown_checkpoint and scope_roots: raise LibraryBusy('未知检查点必须执行完整库释放证明。') observed = [] def check_owners(): if stop is not None and stop.is_set(): raise InterruptedError('已停止释放检查') owners = [] observed.clear() from .session_record import active_write_records if active_write_records(library): raise LibraryBusy('本库原生调用或助手尚未确认返回,请等待当前操作结束。') for item in query_owners(names): if item['pid'] == os.getpid(): continue roots = (*scope_roots, shared_root) if scope_roots else (library.parent,) if not _owner_is_blocking(item, library, roots): if _owner_matches_scope(item, library, roots) is None: observed.append(item) continue automation = item['name'].casefold() in ('python.exe', 'pythonw.exe', 'cbbrefreshverify.exe') command = item['command'].casefold() if not automation or not command or any(flag in command for flag in ( '--verify-worker', '--repair-worker', '--management-worker', '--native-backup-worker')): owners.append(item) if owners: raise LibraryBusy(f'原生进程仍存在,不能解除协调阻止:{owners}') note_metadata_progress() return owners check_owners() from .file_probe import hold_release_files roots = (*scope_roots, shared_root) if scope_roots else None inspections = [] with ExitStack() as stack: try: files = stack.enter_context(hold_release_files(library, roots, stop=stop)) except OSError as error: blocked = LibraryBusy(str(error)) blocked.winerror = getattr(error, 'winerror', None) raise blocked from error note_metadata_progress() if files['addresses']: # Native inspection needs the database; reacquire proof handles # and compare the exact address/project binding before commit. stack.close() from .icdb_ownership import inspect_release_addresses # Checkpoint I/O is per-library; only native inspection owns # the host channel. The sidecar still excludes other writers. if stop is not None and stop.is_set(): raise InterruptedError('已停止释放检查') inspections = inspect_release_addresses(files, stop=stop) try: current = stack.enter_context(hold_release_files(library, roots, stop=stop)) except OSError as error: blocked = LibraryBusy(str(error)) blocked.winerror = getattr(error, 'winerror', None) raise blocked from error if any(current[key] != files[key] for key in ( 'exclusive_files', 'addresses', 'signatures', 'inspection_targets')): raise LibraryBusy('原生检查期间资源范围或地址身份变化。') files = current note_metadata_progress() export_proof = (prove_export_copies_released(library, allow_unknown_checkpoint=True) if allow_unknown_checkpoint else prove_export_copies_released(library)) export_copies = stack.enter_context(export_proof) owners = check_owners() yield {**{k: v for k, v in files.items() if k not in ('status', 'token')}, 'native_owners': owners, 'observed_owners': list(observed), 'export_copies': export_copies, 'native_inspections': inspections, 'data_recovery_required': any(item['repair_required'] for item in inspections) if inspections else None, 'data_recovery_resources': [item['database'] for item in inspections if item['repair_required']], 'unknown_checkpoint_recovery': allow_unknown_checkpoint} finally: uninitialize_com(initialized) def release_retained_library(library, evidence_dir, *, block_name=None, block_names=(), stop=None): """Reconcile historical leases after fresh owner/address/access proof. The upload sidecar is never deleted. This acknowledges process release, not the correctness of a partially changed library or its recovery state. """ import win32con, win32file from .operation_queue import note_metadata_progress library = Path(library).resolve(strict=True) from .session_record import (retained_session_snapshot, acknowledge_library_release, reconciled_retained_sessions) retained = retained_session_snapshot(library) requested_blocks = tuple(dict.fromkeys(block_names or ((block_name,) if block_name else ()))) if any(not name or name in ('.', '..') or any(c in name for c in '\\/:*?"<>|') for name in requested_blocks): raise ValueError('释放检查的工程名称无效') evidence = Path(evidence_dir).resolve() if evidence.is_relative_to(library.parent) or library.parent.is_relative_to(evidence): raise ValueError('释放检查证据必须位于目标库之外。') lease = LibraryLock(library) external = None try: if stop is not None and stop.is_set(): raise InterruptedError('已停止释放检查') with _library_guard(lease.key): owner = _owners.get(lease.key) if owner and (not owner['retained'] or owner['depth']): raise LibraryBusy('本应用操作仍在使用库锁。') if owner is None: try: external = win32file.CreateFile(str(lease.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) except Exception as error: if getattr(error, 'winerror', None) not in (32, 33): raise raise LibraryBusy('其他进程仍持有库锁,不能解除。') from error marker = blocker_path(library) checkpoints = (marker, marker.with_suffix('.pending')) before = {path: path.read_bytes() if path.exists() else None for path in checkpoints} unknown = any(data is not None and checkpoint_record(path) is None for path, data in before.items()) records = [checkpoint_record(path) for path, data in before.items() if data is not None] scoped_blocks = (requested_blocks if requested_blocks and all(record and record.get('block_name') in requested_blocks for record in records) and (owner is None or owner.get('block_name') in requested_blocks) else ()) scoped = scoped_blocks[0] if len(scoped_blocks) == 1 else None reconciled = [{'kind': 'checkpoint', 'path': str(path), 'recognized': metadata is not None, **{key: (metadata or {}).get(key) for key in ('block_name', 'lease_id', 'pid', 'status', 'time')}} for path, data in before.items() if data is not None for metadata in (checkpoint_record(path),)] if owner: reconciled.append({'kind': 'retained_owner', 'path': str(lease.path), 'block_name': owner.get('block_name'), 'lease_id': owner['lease_id'], 'status': 'OWNER_RELEASE_UNCONFIRMED'}) foreign = sorted({item['block_name'] for item in reconciled if block_name and item.get('block_name') and item['block_name'] != block_name}) options = {'stop': stop} if stop is not None else {} if len(scoped_blocks) > 1: options['block_names'] = scoped_blocks proof_context = (_prove_library_released(library, allow_unknown_checkpoint=True, **options) if unknown else _prove_library_released(library, block_name=scoped, **options) if scoped else _prove_library_released(library, **options)) with proof_context as proof: if stop is not None and stop.is_set(): raise InterruptedError('已停止释放检查') with _guard: if _owners.get(lease.key) is not owner: raise LibraryBusy('释放检查期间租约身份变化,保留当前任务。') if unknown and proof.get('unknown_checkpoint_recovery') is not True: raise LibraryBusy('未知检查点缺少完整库释放证明,保留原文件。') if any((path.read_bytes() if path.exists() else None) != data for path, data in before.items()): raise LibraryBusy('释放检查期间协调检查点变化,保留原文件。') root = evidence/('release-' + uuid4().hex) root.mkdir(parents=True, exist_ok=False) matching_retained = ({path: value for path, value in retained.items() if value[1] in scoped_blocks} if scoped_blocks else retained) reconciled_sessions = reconciled_retained_sessions(matching_retained, block_name=scoped) reconciled_blocks = sorted({item['block_name'] for item in (*reconciled, *reconciled_sessions) if item.get('block_name')}) record = {'library': str(library), 'status': 'RELEASE_CONFIRMED', **proof, 'requested_block_name': block_name, 'requested_block_names': list(requested_blocks), 'proof_scope': 'BLOCK' if scoped else 'BLOCKS' if scoped_blocks else 'LIBRARY', 'proof_block_name': scoped, 'proof_block_names': list(scoped_blocks), 'reconciled_checkpoints': reconciled, 'reconciled_foreign_blockers': sorted(set(foreign) | { block for block in reconciled_blocks if block_name and block != block_name}), 'reconciled_blocks': reconciled_blocks, 'reconciled_sessions': reconciled_sessions, 'unknown_checkpoint_archived': unknown, 'lease_state': 'RELEASED', 'blocked_resources': [], 'data_recovery_required': proof.get('data_recovery_required', None), 'data_recovery_confirmed': False, 'evidence_path': str(root/'release.json')} record['resource_scope'] = {**proof.get('resource_scope', {}), 'scope_state': 'OBSERVED', 'library': str(library), 'block_name': scoped, 'block_names': list(scoped_blocks), 'lease_id': owner['lease_id'] if owner else None, 'operation_ids': [item['operation_id'] for item in reconciled_sessions]} pending = {**record, 'status': 'RELEASE_PENDING', 'lease_state': 'PENDING'} (root/'release.json').write_text(json.dumps(pending, ensure_ascii=False, indent=2), encoding='utf-8') note_metadata_progress() for index, checkpoint in enumerate(checkpoints): if checkpoint.exists(): data = checkpoint.read_bytes() archived = root/f'blocker-before-{index}.json' archived.write_bytes(data) if archived.read_bytes() != data: raise OSError('阻止记录归档校验失败,保持库锁。') note_metadata_progress() if stop is not None and stop.is_set(): raise InterruptedError('已停止释放检查') checkpoint.unlink() # Checkpoint JSON only; never .lock. if owner: owner['handle'].Close() del _owners[lease.key] (root/'release.json').write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8') acknowledge_library_release(record, matching_retained, block_name=scoped) return record finally: if external is not None: external.Close()