fix: finish current management item before stopping its batch

This commit is contained in:
阿白叔 committed 2026-11-17 04:53:30 +08:00
1 parent fd50805380
commit e0bb2438e8
5 files changed
+178 -7

No files matched your search

+5 -2
View File
@@ -237,6 +237,8 @@ class BatchTaskContext:
stop: _StopSignal = field(default_factory=_StopSignal, repr=False, compare=False)
_closed: Event = field(default_factory=Event, repr=False, compare=False)
native_policy: str = field(default='legacy', kw_only=True)
# Only queue/batch boundaries consume this; the current item retains hard-stop semantics.
stop_after_current: _StopSignal = field(default_factory=_StopSignal, repr=False, compare=False, kw_only=True)
def __post_init__(self):
if self.native_policy not in ('legacy', 'restricted', 'external'):
@@ -402,7 +404,7 @@ class NativeQueue:
available = ready() # Call only this ticket's predicate, outside the queue lock.
with self.condition:
if (self.closed or stop is not None and stop.is_set()
or context is not None and context.stop.is_set()):
or context is not None and (context.stop.is_set() or context.stop_after_current.is_set())):
raise QueueCancelled('已取消排队,未启动原生操作。')
ticket[1] = bool(available)
first = next((entry for entry in self.pending if entry[1]), None)
@@ -422,7 +424,8 @@ class NativeQueue:
notified, previous_state = True, current_state
except Exception:
pass
if stop is not None and stop.is_set():
if (stop is not None and stop.is_set()
or context is not None and context.stop_after_current.is_set()):
raise QueueCancelled('已取消排队,未启动原生操作。')
return context.run(action, stop=stop, timeout_seconds=timeout_seconds) if context else action()
finally: