fix: bound task handoff and preserve native writer ownership
This commit is contained in:
1 parent
0440b2efbf
commit
ddff6bd857
18 files changed
+1397
-118
No files matched your search
@@ -1,36 +1,320 @@
|
||||
"""Cooperative FIFO admission for this application's native operations."""
|
||||
from threading import Condition
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass, field
|
||||
import ntpath
|
||||
from threading import Condition, Event, RLock, Thread, local
|
||||
import time
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
class QueueCancelled(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class TaskHandoff(QueueCancelled):
|
||||
"""The call is still observable; this is never a release receipt."""
|
||||
def __init__(self, context, *, cancelled=False, result=None):
|
||||
self.operation_id = context.operation_id
|
||||
self.library = context.library
|
||||
self.cancelled = cancelled
|
||||
self.scope = context.scope
|
||||
self.result = result
|
||||
super().__init__('已交给后台观察;当前资源尚未确认释放。')
|
||||
|
||||
|
||||
class _StopSignal:
|
||||
def __init__(self):
|
||||
self._event = Event()
|
||||
|
||||
def set(self):
|
||||
self._event.set()
|
||||
|
||||
def is_set(self):
|
||||
return self._event.is_set()
|
||||
|
||||
def wait(self, timeout=None):
|
||||
return self._event.wait(timeout)
|
||||
|
||||
|
||||
_local = local()
|
||||
_tasks_guard = RLock()
|
||||
_calls = {}
|
||||
|
||||
|
||||
def _path_key(path):
|
||||
# Identity was bound by the caller; queue predicates must never resolve a share.
|
||||
return ntpath.normcase(ntpath.normpath(str(path)))
|
||||
|
||||
|
||||
def request_library_keys(request, context=None):
|
||||
library = str(getattr(request, 'lmc_file', getattr(request, 'library', '__injected_verifier_scope__')))
|
||||
keys = {_path_key(library)}
|
||||
drive, remote = getattr(request, 'mapped_drive', ''), getattr(request, 'mapped_remote_root', '')
|
||||
if drive and remote and ntpath.splitdrive(library)[0].casefold() == drive.rstrip('\\/').casefold():
|
||||
keys.add(_path_key(ntpath.join(remote, ntpath.splitdrive(library)[1].lstrip('\\/'))))
|
||||
if context is not None:
|
||||
aliases = {_path_key(v) for v in (context.library, *context.aliases)}
|
||||
if keys.intersection(aliases):
|
||||
keys.update(aliases)
|
||||
return keys
|
||||
|
||||
|
||||
@dataclass(eq=False)
|
||||
class _Call:
|
||||
context: object
|
||||
external_stop: object = None
|
||||
done: Event = field(default_factory=Event)
|
||||
abandoned: bool = False
|
||||
native_depth: int = 0
|
||||
native_handoff: str = ''
|
||||
phase_started: float = field(default_factory=time.monotonic)
|
||||
result: object = None
|
||||
last_result: object = None
|
||||
error: BaseException | None = None
|
||||
|
||||
def is_set(self):
|
||||
return (self.abandoned or self.context.stop.is_set()
|
||||
or self.external_stop is not None and self.external_stop.is_set())
|
||||
|
||||
|
||||
def current_context():
|
||||
call = getattr(_local, 'call', None)
|
||||
return call.context if call is not None else None
|
||||
|
||||
|
||||
def task_stop(fallback=None):
|
||||
return getattr(_local, 'call', None) or fallback
|
||||
|
||||
|
||||
def check_active():
|
||||
call = getattr(_local, 'call', None)
|
||||
if call is not None and call.is_set():
|
||||
raise QueueCancelled('任务已取消,未提交新的原生操作。')
|
||||
|
||||
|
||||
def remember_result(result):
|
||||
call = getattr(_local, 'call', None)
|
||||
if call is not None:
|
||||
with _tasks_guard:
|
||||
call.last_result = result
|
||||
return result
|
||||
|
||||
|
||||
def hold_native_handoff(process, session_record_path):
|
||||
"""Keep this thread's mutex while a recorded native client is unreturned.
|
||||
|
||||
The caller registers the observer first. Only UI/scheduling is handed off;
|
||||
this thread and its native channel remain alive until that exact handle exits.
|
||||
"""
|
||||
call = getattr(_local, 'call', None)
|
||||
if call is not None:
|
||||
with _tasks_guard:
|
||||
if not call.native_depth:
|
||||
raise RuntimeError('Native handoff requires an owned native phase')
|
||||
call.native_handoff = str(session_record_path)
|
||||
while process.poll() is None:
|
||||
Event().wait(.1)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def native_phase():
|
||||
"""Never abandon a transaction between native writes (including Remove/Add)."""
|
||||
call = getattr(_local, 'call', None)
|
||||
with _tasks_guard:
|
||||
check_active()
|
||||
if call is not None:
|
||||
call.native_depth += 1
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
if call is not None:
|
||||
with _tasks_guard:
|
||||
call.native_depth -= 1
|
||||
call.phase_started = time.monotonic()
|
||||
|
||||
|
||||
@contextmanager
|
||||
def native_channel(*, inspection_only=False):
|
||||
"""Shared by Verify, Repair and backup native clients; reentrant per thread."""
|
||||
import win32con, win32event
|
||||
with native_phase():
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
try:
|
||||
check_active()
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (
|
||||
win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired:
|
||||
raise RuntimeError('NATIVE_CHANNEL_BUSY: 另一个原生操作仍在运行。')
|
||||
from .desktop_worker import assert_native_writer_idle
|
||||
if inspection_only is not True:
|
||||
assert_native_writer_idle()
|
||||
yield
|
||||
finally:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class BatchTaskContext:
|
||||
library: str
|
||||
aliases: tuple[str, ...] = ()
|
||||
targets: tuple[str, ...] = ()
|
||||
resources: tuple[str, ...] = ()
|
||||
operation_id: str = field(default_factory=lambda: uuid4().hex)
|
||||
stop: _StopSignal = field(default_factory=_StopSignal, repr=False, compare=False)
|
||||
_closed: Event = field(default_factory=Event, repr=False, compare=False)
|
||||
|
||||
def __post_init__(self):
|
||||
object.__setattr__(self, 'library', str(self.library))
|
||||
for name in ('aliases', 'targets', 'resources'):
|
||||
object.__setattr__(self, name, tuple(str(v) for v in getattr(self, name)))
|
||||
|
||||
@property
|
||||
def scope(self):
|
||||
return dict(scope_state='DECLARED', library=self.library, aliases=list(self.aliases),
|
||||
block_names=list(self.targets), resources=list(self.resources),
|
||||
operation_ids=[self.operation_id])
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
return self.snapshot()['state']
|
||||
|
||||
def snapshot(self):
|
||||
with _tasks_guard:
|
||||
calls = [call for call in _calls.values() if call.context is self]
|
||||
pending = sum(not call.done.is_set() for call in calls)
|
||||
return dict(operation_id=self.operation_id, resource_scope=self.scope,
|
||||
state='CLOSED' if self._closed.is_set() else
|
||||
'CANCELLED' if self.stop.is_set() else
|
||||
'HANDED_OFF' if any(c.abandoned for c in calls) else
|
||||
'RUNNING' if pending else 'IDLE', pending=pending,
|
||||
pending_native=any(c.native_depth for c in calls))
|
||||
|
||||
def cancel(self):
|
||||
self.stop.set()
|
||||
|
||||
def check_active(self):
|
||||
if self.stop.is_set() or self._closed.is_set():
|
||||
raise QueueCancelled('任务已取消,未提交新的原生操作。')
|
||||
check_active()
|
||||
|
||||
def guard_callback(self, callback, *, current=None):
|
||||
call = getattr(_local, 'call', None)
|
||||
def guarded(*args, **kwargs):
|
||||
# Cancellation still gets its own terminal result; closing or an
|
||||
# abandoned invocation cannot update a newer task/library.
|
||||
if (not self._closed.is_set() and not (call and call.abandoned)
|
||||
and (current is None or current() is self)):
|
||||
return callback(*args, **kwargs)
|
||||
return guarded
|
||||
|
||||
def close(self, *, timeout_seconds=30):
|
||||
self._closed.set()
|
||||
self.cancel()
|
||||
deadline = time.monotonic() + min(30, max(0, timeout_seconds))
|
||||
with _tasks_guard:
|
||||
calls = [c for c in _calls.values() if c.context is self]
|
||||
for call in calls:
|
||||
call.done.wait(max(0, deadline-time.monotonic()))
|
||||
with _tasks_guard:
|
||||
for call in calls:
|
||||
if not call.done.is_set() and not call.native_depth:
|
||||
call.abandoned = True
|
||||
return self.snapshot()
|
||||
|
||||
def run(self, action, *, stop=None, timeout_seconds=10):
|
||||
self.check_active()
|
||||
if current_context() is self:
|
||||
return action() # Preserve nested lease and mutex thread identity.
|
||||
aliases = {_path_key(v) for v in (self.library, *self.aliases)}
|
||||
call = _Call(self, stop)
|
||||
with _tasks_guard:
|
||||
if any(c.abandoned and aliases.intersection(
|
||||
_path_key(v) for v in (c.context.library, *c.context.aliases))
|
||||
for c in _calls.values()):
|
||||
raise TaskHandoff(self)
|
||||
_calls[id(call)] = call
|
||||
def execute():
|
||||
_local.call = call
|
||||
try:
|
||||
check_active()
|
||||
call.result = action()
|
||||
except BaseException as error:
|
||||
call.error = error
|
||||
finally:
|
||||
with _tasks_guard:
|
||||
call.done.set()
|
||||
_calls.pop(id(call), None)
|
||||
del _local.call
|
||||
Thread(target=execute, name='cbb-task-'+self.operation_id[:12], daemon=True).start()
|
||||
while not call.done.wait(.02):
|
||||
with _tasks_guard:
|
||||
if call.native_handoff:
|
||||
cancelled = call.is_set()
|
||||
call.abandoned = True
|
||||
raise TaskHandoff(self, cancelled=cancelled, result=call.last_result)
|
||||
elapsed = time.monotonic()-call.phase_started
|
||||
if not call.native_depth and (call.abandoned or elapsed >= timeout_seconds):
|
||||
cancelled = call.is_set()
|
||||
call.abandoned = True
|
||||
raise TaskHandoff(self, cancelled=cancelled, result=call.last_result)
|
||||
if call.error is not None:
|
||||
raise call.error
|
||||
return call.result
|
||||
|
||||
|
||||
class NativeQueue:
|
||||
def __init__(self):
|
||||
self.condition = Condition()
|
||||
self.pending = []
|
||||
self.active = False
|
||||
self.closed = False
|
||||
self.contexts = {}
|
||||
|
||||
def wake(self):
|
||||
with self.condition: self.condition.notify_all()
|
||||
|
||||
def run(self, action, *, stop=None, waiting=None, ready=lambda: True, wait_state=None):
|
||||
ticket = object()
|
||||
def close(self, *, timeout_seconds=30):
|
||||
deadline = time.monotonic()+min(30, max(0, timeout_seconds))
|
||||
with self.condition:
|
||||
self.closed = True
|
||||
contexts = tuple(self.contexts.values())
|
||||
for context in contexts:
|
||||
context.cancel()
|
||||
self.condition.notify_all()
|
||||
return tuple(context.close(timeout_seconds=max(0, deadline-time.monotonic()))
|
||||
for context in contexts)
|
||||
|
||||
def run(self, action, *, stop=None, waiting=None, ready=lambda: True, wait_state=None,
|
||||
context=None, timeout_seconds=10):
|
||||
ticket = [object(), False]
|
||||
acquired, notified = False, False
|
||||
previous_state = None
|
||||
with self.condition: self.pending.append((ticket, ready))
|
||||
with self.condition:
|
||||
if self.closed:
|
||||
raise QueueCancelled('队列已关闭,未启动新的操作。')
|
||||
if context is not None:
|
||||
self.contexts[id(context)] = context
|
||||
self.pending.append(ticket)
|
||||
try:
|
||||
while not acquired:
|
||||
available = ready() # Call only this ticket's predicate, outside the queue lock.
|
||||
with self.condition:
|
||||
if stop is not None and stop.is_set():
|
||||
if (self.closed or stop is not None and stop.is_set()
|
||||
or context is not None and context.stop.is_set()):
|
||||
raise QueueCancelled('已取消排队,未启动原生操作。')
|
||||
first = next((token for token, available in self.pending if available()), None)
|
||||
ticket[1] = bool(available)
|
||||
first = next((entry for entry in self.pending if entry[1]), None)
|
||||
if not self.active and first is ticket:
|
||||
self.pending = [entry for entry in self.pending if entry[0] is not ticket]
|
||||
self.active = acquired = True
|
||||
else:
|
||||
self.condition.wait(.1)
|
||||
if acquired and not ready():
|
||||
with self.condition:
|
||||
ticket[1] = self.active = acquired = False
|
||||
self.condition.notify_all()
|
||||
if not acquired and waiting is not None:
|
||||
try:
|
||||
current_state = wait_state() if wait_state is not None else None
|
||||
@@ -41,9 +325,9 @@ class NativeQueue:
|
||||
pass
|
||||
if stop is not None and stop.is_set():
|
||||
raise QueueCancelled('已取消排队,未启动原生操作。')
|
||||
return action()
|
||||
return context.run(action, stop=stop, timeout_seconds=timeout_seconds) if context else action()
|
||||
finally:
|
||||
with self.condition:
|
||||
self.pending = [entry for entry in self.pending if entry[0] is not ticket]
|
||||
self.pending = [entry for entry in self.pending if entry is not ticket]
|
||||
if acquired: self.active = False
|
||||
self.condition.notify_all()
|
||||
Reference in new issue
Block a user