fix: admit explicit ICDB maintenance with preserved address evidence
This commit is contained in:
1 parent
4f741cd176
commit
e39dc63ded
10 files changed
+691
-25
No files matched your search
@@ -82,8 +82,19 @@ class LibraryLock:
|
||||
self.key = _key(self.path)
|
||||
self.entered = False
|
||||
self.safe_to_release = safe_to_release
|
||||
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):
|
||||
if self.maintenance_guard is not None:
|
||||
return _enter_maintenance(self)
|
||||
import win32con, win32file
|
||||
with _guard:
|
||||
owner = _owners.get(self.key)
|
||||
@@ -134,6 +145,16 @@ class LibraryLock:
|
||||
_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 _guard:
|
||||
if not self.entered:
|
||||
return
|
||||
@@ -172,6 +193,74 @@ class LibraryLock:
|
||||
_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 _enter_maintenance(lease):
|
||||
"""Transfer only a valid known lease, after project admission under lock."""
|
||||
import win32con, win32event, win32file
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
handle = None
|
||||
existing = None
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired: raise LibraryBusy('已有 CBB 操作运行,维护不能接续')
|
||||
with _guard:
|
||||
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 _supervisor_alive(record['pid']):
|
||||
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.
|
||||
with 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('原检查点归档校验失败')
|
||||
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}
|
||||
lease.maintenance_proof = proof
|
||||
lease.entered = True
|
||||
lease.maintenance_mutex = mutex
|
||||
return lease
|
||||
except Exception:
|
||||
if handle is not None and existing is None: handle.Close()
|
||||
if acquired: win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
raise
|
||||
|
||||
|
||||
def owned_snapshot(path):
|
||||
"""Read only a lease held by this thread, never reopen an exclusive file."""
|
||||
import win32con, win32file
|
||||
|
||||
Reference in new issue
Block a user