fix: scope runtime release proof and bound native file probes
This commit is contained in:
1 parent
a1b38c242f
commit
b070837acb
10 files changed
+816
-77
No files matched your search
@@ -77,6 +77,7 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||
parser.add_argument('--verify-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--repair-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--owner-probe', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--file-probe', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--native-backup-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--management-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--dialog-agent', type=Path, help=argparse.SUPPRESS)
|
||||
@@ -89,6 +90,9 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||
parser.add_argument('--mentor-bin', default='', help=argparse.SUPPRESS)
|
||||
parser.add_argument("--smoke-test", action="store_true", help=argparse.SUPPRESS); parser.add_argument("--data-root", type=Path, help=argparse.SUPPRESS)
|
||||
args = parser.parse_args(list(argv) if argv is not None else None)
|
||||
if args.file_probe:
|
||||
from .file_probe import file_probe_main
|
||||
return file_probe_main(args.file_probe)
|
||||
if args.owner_probe:
|
||||
from .owner_probe import owner_probe_main
|
||||
return owner_probe_main(args.owner_probe)
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
"""Bound read-only filesystem probes; keep proof handles alive through commit."""
|
||||
from contextlib import contextmanager, ExitStack
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
@contextmanager
|
||||
def hold_release_files(library, roots=None, *, timeout_seconds=10, stop=None):
|
||||
"""Only this pure probe may be terminated. stdin EOF releases its handles."""
|
||||
with tempfile.TemporaryDirectory(prefix='cbb-file-probe-') as folder:
|
||||
job = Path(folder)/'request.json'
|
||||
token = uuid4().hex
|
||||
job.write_text(json.dumps({'token': token, 'library': str(library),
|
||||
'roots': [str(p) for p in roots] if roots is not None else None}), 'utf-8')
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False) else
|
||||
[sys.executable, '-m', 'cbb_refresh_verify']) + ['--file-probe', str(job)]
|
||||
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL, creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
||||
try:
|
||||
deadline = time.monotonic()+max(.1, min(10, timeout_seconds))
|
||||
result_path = job.with_name('result.json')
|
||||
while not result_path.exists():
|
||||
if stop is not None and stop.is_set():
|
||||
raise InterruptedError('FILE_PROBE_CANCELLED')
|
||||
if time.monotonic() >= deadline:
|
||||
raise TimeoutError('FILE_PROBE_TIMEOUT: 文件占用检查超时,未证明释放')
|
||||
if process.poll() is not None:
|
||||
raise OSError('FILE_PROBE_UNCONFIRMED: 文件探测未返回证据')
|
||||
time.sleep(.02)
|
||||
if stop is not None and stop.is_set():
|
||||
raise InterruptedError('FILE_PROBE_CANCELLED')
|
||||
record = json.loads(result_path.read_text('utf-8'))
|
||||
if record.get('token') != token or record.get('status') != 'HELD' or process.poll() is not None:
|
||||
error = OSError('FILE_PROBE_UNCONFIRMED: '+str(record.get('error', '探测身份或句柄失效')))
|
||||
error.winerror = record.get('winerror')
|
||||
raise error
|
||||
# Duplicate the already-open handles before acknowledging the probe.
|
||||
# Its exit can never create a gap in the parent's commit protection.
|
||||
import win32api, win32con
|
||||
with ExitStack() as protection:
|
||||
child = win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE, False, process.pid)
|
||||
protection.callback(child.Close)
|
||||
if len(record['handles']) != len(record['exclusive_files'])+len(record['binding_files']):
|
||||
raise OSError('FILE_PROBE_UNCONFIRMED: 证明句柄数量不符')
|
||||
for handle in record['handles']:
|
||||
owned = win32api.DuplicateHandle(child, handle, win32api.GetCurrentProcess(),
|
||||
0, False, win32con.DUPLICATE_SAME_ACCESS)
|
||||
protection.callback(owned.Close)
|
||||
yield {k: v for k, v in record.items() if k != 'handles'}
|
||||
finally:
|
||||
process.stdin.close()
|
||||
try:
|
||||
process.wait(.5)
|
||||
except subprocess.TimeoutExpired:
|
||||
process.terminate() # This executable only scans and opens read handles.
|
||||
process.wait(3)
|
||||
|
||||
|
||||
def _snapshot(library, roots):
|
||||
from .scoped_manifest import scan_tree
|
||||
entries = {library: library.lstat()}
|
||||
for root in roots:
|
||||
if root.exists():
|
||||
entries.update(scan_tree(root))
|
||||
native = sorted(path for path, info in entries.items() if stat.S_ISREG(info.st_mode)
|
||||
and (path == library or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt')))
|
||||
if library not in native:
|
||||
raise ValueError('目标库身份缺失')
|
||||
addresses = sorted(path for path in entries if path.name.casefold() == 'saddress.adr')
|
||||
targets, signatures = [], {}
|
||||
for address in addresses:
|
||||
data = address.read_bytes()
|
||||
signatures[str(address)] = hashlib.sha256(data).hexdigest()
|
||||
database = address.parent.parent
|
||||
matches = []
|
||||
for project in database.parent.glob('*.prj'):
|
||||
if project not in entries or project.stat().st_size > 1024*1024:
|
||||
continue
|
||||
from .backup_recovery import _project_text
|
||||
text = _project_text(project)
|
||||
references = re.findall(r'^KEY iCDBDir "([^"\r\n]+)"', text, re.M)
|
||||
if len(references) == 1 and (project.parent/references[0]).resolve() == database:
|
||||
matches.append(project)
|
||||
if len(matches) != 1 or database/'icdb.dat' not in native:
|
||||
raise ValueError(f'地址未绑定唯一工程数据库,需人工核对:{address}')
|
||||
targets.append({'project': str(matches[0]), 'database': str(database)})
|
||||
signatures[str(matches[0])] = hashlib.sha256(matches[0].read_bytes()).hexdigest()
|
||||
return {'exclusive_files': [str(p) for p in native], 'addresses': [str(p) for p in addresses],
|
||||
'inspection_targets': targets, 'signatures': signatures, 'binding_files': sorted(signatures),
|
||||
'resource_scope': {'library': str(library), 'roots': [str(p) for p in roots],
|
||||
'projects': sorted(str(p) for p in entries if p.suffix.casefold() == '.prj'),
|
||||
'shared_dependencies': sorted(str(p.parent) for p in native
|
||||
if p.is_relative_to(library.parent/'SymbolLibs') and p.name.casefold() == 'icdb.dat'),
|
||||
'databases': sorted({str(p.parent) for p in native if p.name.casefold() == 'icdb.dat'})}}
|
||||
|
||||
|
||||
def file_probe_main(job):
|
||||
"""No native launch, writes, repair, address deletion or process termination."""
|
||||
import win32con, win32file
|
||||
job = Path(job)
|
||||
request = json.loads(job.read_text('utf-8'))
|
||||
result = {'token': request['token'], 'status': 'UNCONFIRMED'}
|
||||
try:
|
||||
def checked_path(value):
|
||||
path = Path(value).absolute()
|
||||
for part in (path, *path.parents):
|
||||
try:
|
||||
info = part.lstat()
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
if info.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
||||
raise ValueError(f'资源范围含重解析点:{part}')
|
||||
return path.resolve()
|
||||
library = checked_path(request['library'])
|
||||
roots = [checked_path(p) for p in request['roots']] if request['roots'] is not None else [library.parent]
|
||||
if library.suffix.casefold() != '.lmc' or any(not p.is_relative_to(library.parent) for p in roots):
|
||||
raise ValueError('文件探测范围不属于所选库')
|
||||
snapshot = _snapshot(library, roots)
|
||||
with ExitStack() as stack:
|
||||
handles = []
|
||||
for path in snapshot['exclusive_files']:
|
||||
handle = win32file.CreateFile(path, win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
stack.callback(handle.Close)
|
||||
handles.append(int(handle))
|
||||
for path in snapshot['binding_files']:
|
||||
handle = win32file.CreateFile(path, win32con.GENERIC_READ, win32con.FILE_SHARE_READ,
|
||||
None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
stack.callback(handle.Close)
|
||||
handles.append(int(handle))
|
||||
if _snapshot(library, roots) != snapshot:
|
||||
raise ValueError('占用检查期间资源集合变化')
|
||||
pending = job.with_name('result.pending')
|
||||
pending.write_text(json.dumps({**result, **snapshot, 'status': 'HELD', 'handles': handles}), 'utf-8')
|
||||
pending.replace(job.with_name('result.json'))
|
||||
sys.stdin.buffer.read(1) # Parent keeps stdin open until its commit finishes.
|
||||
return 0
|
||||
except Exception as error:
|
||||
pending = job.with_name('result.pending')
|
||||
pending.write_text(json.dumps({**result, 'error': str(error),
|
||||
'winerror': getattr(error, 'winerror', None)}), 'utf-8')
|
||||
pending.replace(job.with_name('result.json'))
|
||||
return 1
|
||||
@@ -2,6 +2,19 @@
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def session_resource_scope(job):
|
||||
"""Persist declared targets; only fresh proof promotes them to observed scope."""
|
||||
library = Path(job['library'])
|
||||
project, pcb = job.get('project'), job.get('pcb')
|
||||
projects = [str(Path(project))] if project else []
|
||||
databases = [str(Path(p).parent/'database') for p in (project, pcb) if p]
|
||||
shared = library.parent/'SymbolLibs/__ReusableBlocksSymb'
|
||||
return {'scope_state': 'DECLARED' if project else 'RECHECK_REQUIRED',
|
||||
'library': str(library), 'operation_id': job.get('operation_id'),
|
||||
'block_name': job.get('block'), 'projects': projects,
|
||||
'databases': list(dict.fromkeys(databases)), 'shared_dependencies': [str(shared)]}
|
||||
|
||||
|
||||
def database_directories(lmc_file, project, pcb):
|
||||
return tuple(dict.fromkeys((
|
||||
Path(project).parent/'database',
|
||||
@@ -14,3 +27,20 @@ def unreleased_addresses(lmc_file, project, pcb):
|
||||
return [str(path/'cdbsvr/sAddress.adr')
|
||||
for path in database_directories(lmc_file, project, pcb)
|
||||
if (path/'cdbsvr/sAddress.adr').exists()]
|
||||
|
||||
|
||||
def inspect_release_addresses(snapshot, *, stop=None):
|
||||
"""An address is a reason for native inspection, never an occupancy verdict."""
|
||||
import tempfile
|
||||
from .native_repair import RepairTarget, run_native_repair
|
||||
from .native_occupancy import confirmed_inspection
|
||||
from .library_lock import LibraryBusy
|
||||
results = []
|
||||
for item in snapshot['inspection_targets']:
|
||||
target = RepairTarget(Path(item['project']), Path(item['database']))
|
||||
root = Path(tempfile.mkdtemp(prefix='cbb-release-inspection-'))
|
||||
result = run_native_repair(target, root, inspection_only=True, stop=stop)
|
||||
if not confirmed_inspection(target, result) or result['status'] != 'NOT_IN_USE':
|
||||
raise LibraryBusy(f'目标数据库未确认释放:{target.database};检查证据:{root}')
|
||||
results.append(result)
|
||||
return results
|
||||
@@ -11,6 +11,13 @@ 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())
|
||||
|
||||
|
||||
class LibraryBusy(OSError):
|
||||
@@ -82,10 +89,10 @@ def has_retained_library(library):
|
||||
return bool(owner and owner['retained'])
|
||||
|
||||
|
||||
def recheck_previous_release(library, evidence_dir, *, block_name=None, required=False, progress=None):
|
||||
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 _guard:
|
||||
with _library_guard(LibraryLock(library).key):
|
||||
owner = _owners.get(LibraryLock(library).key)
|
||||
marker = blocker_path(library)
|
||||
record = checkpoint_record(marker)
|
||||
@@ -108,7 +115,8 @@ def recheck_previous_release(library, evidence_dir, *, block_name=None, required
|
||||
logging.getLogger(__name__).warning('Release progress observer failed', exc_info=True)
|
||||
notify('previous_release_check', detail='正在复核历史记录对应的实际占用')
|
||||
try:
|
||||
proof = release_retained_library(library, evidence_dir, block_name=block_name)
|
||||
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
|
||||
@@ -128,9 +136,14 @@ def _persist_blocker(library, status='OWNER_RELEASE_UNCONFIRMED', *, lease_id, b
|
||||
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)
|
||||
@@ -159,7 +172,7 @@ class LibraryLock:
|
||||
return lease
|
||||
|
||||
def __enter__(self):
|
||||
with _guard:
|
||||
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:
|
||||
@@ -180,7 +193,7 @@ class LibraryLock:
|
||||
if self.maintenance_guard is not None:
|
||||
return _enter_maintenance(self)
|
||||
import win32con, win32file
|
||||
with _guard:
|
||||
with _library_guard(self.key):
|
||||
owner = _owners.get(self.key)
|
||||
if owner:
|
||||
if owner['thread'] != threading.get_ident() or owner['retained']:
|
||||
@@ -246,7 +259,7 @@ class LibraryLock:
|
||||
self.maintenance_mutex = None
|
||||
|
||||
def _exit_owned(self):
|
||||
with _guard:
|
||||
with _library_guard(self.key):
|
||||
if not self.entered:
|
||||
return
|
||||
owner = _owners[self.key]
|
||||
@@ -402,7 +415,7 @@ def _enter_maintenance(lease):
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired: raise LibraryBusy('已有 CBB 操作运行,维护不能接续')
|
||||
with _guard:
|
||||
with _library_guard(lease.key):
|
||||
marker = blocker_path(lease.library)
|
||||
if marker.with_suffix('.pending').exists():
|
||||
raise LibraryBusy('未完成检查点写入,不能自动接续')
|
||||
@@ -455,7 +468,7 @@ def _enter_maintenance(lease):
|
||||
def owned_snapshot(path):
|
||||
"""Read only a lease held by this thread, never reopen an exclusive file."""
|
||||
import win32con, win32file
|
||||
with _guard:
|
||||
with _library_guard(_key(path)):
|
||||
owner = _owners.get(_key(path))
|
||||
if not owner or owner['thread'] != threading.get_ident() or owner['retained']:
|
||||
return None
|
||||
@@ -470,19 +483,16 @@ def owned_snapshot(path):
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _prove_library_released(library, block_name=None, *, allow_unknown_checkpoint=False):
|
||||
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."""
|
||||
import stat
|
||||
import win32con, win32file
|
||||
from .owner_probe import query_owners
|
||||
from .scoped_manifest import scan_tree
|
||||
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 (allow_unknown_checkpoint
|
||||
or not getattr(worker, 'cbb_library', None) or _key(worker.cbb_library) == _key(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
|
||||
@@ -492,12 +502,17 @@ def _prove_library_released(library, block_name=None, *, allow_unknown_checkpoin
|
||||
'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe',
|
||||
'cscript.exe', 'wscript.exe', 'python.exe', 'pythonw.exe',
|
||||
'CbbRefreshVerify.exe', *sorted(_OWNER_NAMES))
|
||||
scope_root = (Path(library).resolve().parent/'ReusableBlocksLP'/block_name/'Work').resolve() if block_name else None
|
||||
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_root is not None:
|
||||
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
|
||||
@@ -506,12 +521,11 @@ def _prove_library_released(library, block_name=None, *, allow_unknown_checkpoin
|
||||
for item in query_owners(names):
|
||||
if item['pid'] == os.getpid():
|
||||
continue
|
||||
if not allow_unknown_checkpoint:
|
||||
roots = (scope_root, shared_root) if scope_root is not None else (Path(library).parent,)
|
||||
if not _owner_is_blocking(item, library, roots):
|
||||
if _owner_matches_scope(item, library, roots) is None:
|
||||
observed.append(item)
|
||||
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 (
|
||||
@@ -521,44 +535,48 @@ def _prove_library_released(library, block_name=None, *, allow_unknown_checkpoin
|
||||
raise LibraryBusy(f'原生进程仍存在,不能解除协调阻止:{owners}')
|
||||
return owners
|
||||
check_owners()
|
||||
if scope_root is None:
|
||||
entries = scan_tree(Path(library).parent)
|
||||
else:
|
||||
entries = {Path(library): Path(library).lstat()}
|
||||
for root in (scope_root, shared_root):
|
||||
if root.exists():
|
||||
entries.update(scan_tree(root))
|
||||
addresses = [str(path) for path in entries if path.name.casefold() == 'saddress.adr'
|
||||
and (scope_root is None or path.is_relative_to(scope_root)
|
||||
or path.is_relative_to(shared_root))]
|
||||
if addresses:
|
||||
raise LibraryBusy(f'库中仍有数据库地址记录,不能自动解除:{addresses}')
|
||||
# scan_tree already rejects links; only native candidates need file opens.
|
||||
native = [path for path, metadata in entries.items() if stat.S_ISREG(metadata.st_mode) and (
|
||||
path == library
|
||||
or (path.name.casefold() in ('icdb.dat', 'layoutdb.lyt')
|
||||
and (scope_root is None or path.is_relative_to(scope_root)
|
||||
or path.is_relative_to(shared_root))))]
|
||||
if library not in native:
|
||||
raise LibraryBusy('目标库身份缺失,不能解除协调阻止。')
|
||||
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
|
||||
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
|
||||
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
|
||||
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)
|
||||
for path in native:
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
stack.callback(handle.Close)
|
||||
owners = check_owners()
|
||||
yield {'native_owners': owners, 'observed_owners': list(observed),
|
||||
'addresses': addresses, 'export_copies': export_copies,
|
||||
'unknown_checkpoint_recovery': allow_unknown_checkpoint,
|
||||
'exclusive_files': [str(path) for path in native]}
|
||||
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):
|
||||
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,
|
||||
@@ -569,6 +587,9 @@ def release_retained_library(library, evidence_dir, *, block_name=None):
|
||||
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('释放检查证据必须位于目标库之外。')
|
||||
@@ -577,10 +598,12 @@ def release_retained_library(library, evidence_dir, *, block_name=None):
|
||||
acquired = False
|
||||
external = None
|
||||
try:
|
||||
if stop is not None and stop.is_set():
|
||||
raise InterruptedError('已停止释放检查')
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired:
|
||||
raise LibraryBusy('另一个 CBB 操作仍在运行。')
|
||||
with _guard:
|
||||
with _library_guard(lease.key):
|
||||
owner = _owners.get(lease.key)
|
||||
if owner and (not owner['retained'] or owner['depth']):
|
||||
raise LibraryBusy('本应用操作仍在使用库锁。')
|
||||
@@ -598,8 +621,10 @@ def release_retained_library(library, evidence_dir, *, block_name=None):
|
||||
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 = (block_name if block_name and all(record and record.get('block_name') == block_name
|
||||
for record in records) and (owner is None or owner.get('block_name') == block_name) else 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
|
||||
@@ -610,27 +635,46 @@ def release_retained_library(library, evidence_dir, *, block_name=None):
|
||||
'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})
|
||||
proof_context = (_prove_library_released(library, allow_unknown_checkpoint=True) if unknown else
|
||||
_prove_library_released(library, block_name=scoped)
|
||||
if scoped else _prove_library_released(library))
|
||||
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)
|
||||
reconciled_sessions = reconciled_retained_sessions(retained, block_name=scoped)
|
||||
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, 'proof_scope': 'BLOCK' if scoped else 'LIBRARY',
|
||||
'proof_block_name': scoped, 'reconciled_checkpoints': reconciled,
|
||||
'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]}
|
||||
(root/'release.json').write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
for index, checkpoint in enumerate(checkpoints):
|
||||
if checkpoint.exists():
|
||||
@@ -643,7 +687,7 @@ def release_retained_library(library, evidence_dir, *, block_name=None):
|
||||
if owner:
|
||||
owner['handle'].Close()
|
||||
del _owners[lease.key]
|
||||
acknowledge_library_release(record, retained, block_name=scoped)
|
||||
acknowledge_library_release(record, matching_retained, block_name=scoped)
|
||||
return record
|
||||
finally:
|
||||
if external is not None:
|
||||
|
||||
@@ -25,9 +25,9 @@ def fixture_authority(root, library, token):
|
||||
|
||||
|
||||
def current_lease(library):
|
||||
from .library_lock import LibraryLock, _guard, _owners, blocker_path, checkpoint_record
|
||||
from .library_lock import LibraryLock, _library_guard, _owners, blocker_path, checkpoint_record
|
||||
lease = LibraryLock(library)
|
||||
with _guard:
|
||||
with _library_guard(lease.key):
|
||||
owner = _owners.get(lease.key)
|
||||
guard = checkpoint_record(blocker_path(library))
|
||||
if (owner is None or owner['retained'] or owner['thread'] != threading.get_ident()
|
||||
|
||||
@@ -79,7 +79,7 @@ def assert_retained_exports_idle(library=None):
|
||||
|
||||
|
||||
def begin_export_analysis(library, root, project):
|
||||
from .library_lock import _guard, blocker_path, checkpoint_record, LibraryBusy
|
||||
from .library_lock import LibraryLock, _library_guard, blocker_path, checkpoint_record, LibraryBusy
|
||||
from .management_transaction import current_lease
|
||||
library, root, project = Path(library).resolve(), Path(root).resolve(), Path(project).resolve()
|
||||
owner = current_lease(library)
|
||||
@@ -89,7 +89,7 @@ def begin_export_analysis(library, root, project):
|
||||
record = {'protocol': 'cbb-native-export-analysis', 'version': 1, 'token': uuid4().hex,
|
||||
'library': str(library), 'lease_id': owner['lease_id'], 'supervisor_pid': owner['supervisor_pid'],
|
||||
'analysis_root': str(root), 'project': str(project)}
|
||||
with _guard:
|
||||
with _library_guard(LibraryLock(library).key):
|
||||
marker = blocker_path(library)
|
||||
checkpoint = checkpoint_record(marker)
|
||||
if (checkpoint is None or checkpoint['lease_id'] != owner['lease_id']
|
||||
@@ -116,8 +116,10 @@ def prove_export_copies_released(library, *, allow_unknown_checkpoint=False, uns
|
||||
raise LibraryBusy('Invalid library checkpoint cannot release export ownership.')
|
||||
# Only the explicit release path supplies full-library/native proof and
|
||||
# archives the unchanged metadata while its coordination handle is held.
|
||||
assert_retained_exports_idle()
|
||||
if any(p['name'].casefold() in _OWNER_NAMES for p in _process_snapshot()):
|
||||
from .library_lock import _owner_matches_scope
|
||||
if any(p['name'].casefold() in _OWNER_NAMES and
|
||||
_owner_matches_scope(p, library, (library.parent,)) is not False
|
||||
for p in _process_snapshot()):
|
||||
raise LibraryBusy('Unknown export checkpoint still has a possible native identity owner.')
|
||||
yield []
|
||||
return
|
||||
@@ -208,8 +210,8 @@ def prove_export_copies_released(library, *, allow_unknown_checkpoint=False, uns
|
||||
|
||||
|
||||
def finish_export_analysis(library, token, *, native_not_started=False):
|
||||
from .library_lock import _guard, blocker_path, checkpoint_record, LibraryBusy
|
||||
with _guard, prove_export_copies_released(library,
|
||||
from .library_lock import LibraryLock, _library_guard, blocker_path, checkpoint_record, LibraryBusy
|
||||
with _library_guard(LibraryLock(library).key), prove_export_copies_released(library,
|
||||
unstarted_token=token if native_not_started else ''):
|
||||
marker = blocker_path(library)
|
||||
checkpoint = checkpoint_record(marker)
|
||||
|
||||
@@ -560,9 +560,11 @@ def _force_close(row, *, stop=None):
|
||||
'detail': '已强制结束客户端,工程待检查,未执行修复' if exited else '已请求结束客户端,等待退出'}
|
||||
|
||||
|
||||
def _finish_library_release(library, evidence):
|
||||
def _finish_library_release(library, evidence, block_names=(), stop=None):
|
||||
from .library_lock import release_retained_library
|
||||
return release_retained_library(library, evidence)
|
||||
return release_retained_library(library, evidence,
|
||||
block_name=block_names[0] if len(block_names) == 1 else None,
|
||||
block_names=block_names, stop=stop)
|
||||
|
||||
|
||||
def _request_native_cancel(row, *, stop=None):
|
||||
@@ -713,7 +715,8 @@ def _release_residual_sessions(plan, evidence_dir, *, stop=None, force=False, ca
|
||||
if row.get('force_user_confirmed') is True else None),
|
||||
native_activity=native_activity(row['name'], windows, incomplete=error))
|
||||
if release_targets_complete(result['sessions']) and not (stop and stop.is_set()):
|
||||
result['proof'] = _finish_library_release(library, root/'library-proof')
|
||||
result['proof'] = _finish_library_release(library, root/'library-proof',
|
||||
blocks, stop)
|
||||
proof = result['proof']
|
||||
if (proof.get('status') != 'RELEASE_CONFIRMED'
|
||||
or Path(proof.get('library', '')).resolve() != library):
|
||||
|
||||
@@ -108,9 +108,12 @@ def library_wait_reason(library):
|
||||
def register_worker_session(worker, job):
|
||||
"""The launcher must persist identity before opening the worker's gate."""
|
||||
path = Path(job['session_record_path'])
|
||||
from .icdb_ownership import session_resource_scope
|
||||
write_session_record(path, {k: job.get(k) for k in (
|
||||
'operation_id', 'operation', 'library', 'block', 'desktop', 'cancel_signal')}
|
||||
| {'processes': [{'role': 'worker', **process_identity(worker.pid)}]})
|
||||
| {'processes': [{'role': 'worker', **process_identity(worker.pid)}],
|
||||
'lease_state': 'ACTIVE', 'resource_scope': session_resource_scope(job),
|
||||
'blocked_resources': [], 'data_recovery_required': None})
|
||||
worker.session_record_path = path
|
||||
worker.cbb_library = job['library']
|
||||
|
||||
@@ -230,7 +233,13 @@ def _result(status, detail='', *, session=None, state=None, proof=None):
|
||||
return {'status': status, 'detail': detail, 'library': (session or {}).get('library'),
|
||||
'block': (session or {}).get('block'),
|
||||
'operation_id': (session or {}).get('operation_id'),
|
||||
'phase': (state or {}).get('phase', 'UNCONFIRMED'), 'release_proof': proof}
|
||||
'phase': (state or {}).get('phase', 'UNCONFIRMED'), 'release_proof': proof,
|
||||
'lease_state': (proof or {}).get('lease_state', 'ACTIVE' if status == 'ACTIVE' else 'UNCONFIRMED'),
|
||||
'resource_scope': (proof or {}).get('resource_scope') or (session or {}).get('resource_scope')
|
||||
or {'scope_state': 'RECHECK_REQUIRED'},
|
||||
'blocked_resources': (proof or {}).get('blocked_resources', (session or {}).get('blocked_resources', [])),
|
||||
'data_recovery_required': (proof or {}).get('data_recovery_required',
|
||||
(session or {}).get('data_recovery_required'))}
|
||||
|
||||
|
||||
def _reconcile(path, root, handles, stack, *, release=True):
|
||||
@@ -388,8 +397,11 @@ def handoff_session(path, *, evidence_root, process=None, progress=None, stop=No
|
||||
return result
|
||||
finally:
|
||||
with _observer_guard:
|
||||
_observers.pop(key, None)
|
||||
if result and result['status'] == 'RELEASED':
|
||||
if _observers.get(key, (None,))[0] is threading.current_thread():
|
||||
_observers.pop(key, None)
|
||||
if (result and result['status'] == 'RELEASED'
|
||||
and _retained_libraries.get(key) is aliases
|
||||
and _retained_results.get(key, {}).get('operation_id') == session.get('operation_id')):
|
||||
_retained_libraries.pop(key, None)
|
||||
_retained_results.pop(key, None)
|
||||
thread = threading.Thread(target=observe, name='retained-native-session', daemon=True)
|
||||
@@ -420,9 +432,16 @@ def discover_sessions(evidence_root, *, library=None):
|
||||
if not (name.startswith('observation-') and name.endswith('.json')):
|
||||
continue
|
||||
observation = read_session_record(Path(folder)/name, evidence_root=root)
|
||||
proof = observation.get('release_proof') or {}
|
||||
proof = proof if isinstance(proof, dict) else {}
|
||||
proof_scope = proof.get('resource_scope')
|
||||
session_scope = value.get('resource_scope')
|
||||
if observation['operation_id'] == value['operation_id'] and (
|
||||
observation.get('status') == 'RELEASED' and observation.get('release_proof')
|
||||
observation.get('status') == 'RELEASED'
|
||||
and proof.get('lease_state') == 'RELEASED'
|
||||
and isinstance(proof_scope, dict) and proof_scope.get('scope_state') == 'OBSERVED'
|
||||
or observation.get('status') == 'FINISHED'
|
||||
and isinstance(session_scope, dict) and session_scope.get('scope_state') == 'DECLARED'
|
||||
and observation.get('native_release_confirmed') is True
|
||||
and observation.get('worker_exited') is True):
|
||||
completed = True
|
||||
|
||||
Reference in new issue
Block a user