Skip to content

Commit 193e9c9

Browse files
committed
management: preserve notification and result ownership
Reject pending same-runtime notification waits from the serial callback lane without blocking independent native reentry. Retry callback batch selection when its original reservation retires during registration. Serialize physical-result adoption with close and supersession. Retain the original executor worker until native acceptance or rejected-result disposal finishes, so a completed Future cannot move cleanup onto an owner. Claim delivery once across execution, cancellation and interrupted callback registration. Keep admission charged until cleanup completes. Cover the races with controlled interleavings, actual disposal thread checks, standard bulk waits and executor failure regressions.
1 parent d76d975 commit 193e9c9

2 files changed

Lines changed: 333 additions & 49 deletions

File tree

‎cassandra/_management.py‎

Lines changed: 89 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def _checked_acquire(self, *args, **kwargs):
9898

9999
def _checked_release(self):
100100
if self._runtime._pid == os.getpid():
101-
return self._release()
101+
self._release()
102+
return None
102103

103104
def __enter__(self):
104105
self._runtime._check_pid()
@@ -149,7 +150,7 @@ def _state(self):
149150
# Bulk waits read state after acquiring ALL future conditions.
150151
# Reject before waiter installation, where their context unwinds
151152
# every lock. Own nonblocking methods use scoped access above.
152-
assert_can_wait()
153+
self._assert_can_wait()
153154
return state
154155

155156
@_state.setter
@@ -179,16 +180,25 @@ def cancelled(self):
179180
def result(self, timeout=None):
180181
self._record.runtime._check_pid()
181182
if not self.done():
182-
assert_can_wait()
183+
self._assert_can_wait()
183184
return super().result(timeout)
184185

185186
@_future_access
186187
def exception(self, timeout=None):
187188
self._record.runtime._check_pid()
188189
if not self.done():
189-
assert_can_wait()
190+
self._assert_can_wait()
190191
return super().exception(timeout)
191192

193+
def _assert_can_wait(self):
194+
assert_can_wait()
195+
current = getattr(_context, 'record', None)
196+
if (execution_context() == 'callback' and current is not None and
197+
current.runtime is self._record.runtime and
198+
self._record.envelope.kind == 'notification'):
199+
raise ManagementContextError(
200+
'Notification wait would block its own callback lane')
201+
192202
@_future_access
193203
def cancel(self):
194204
self._record.runtime._check_pid()
@@ -235,6 +245,10 @@ def add_done_callback(self, fn):
235245
with self._condition:
236246
if record is not self._delivery_record:
237247
continue
248+
if owner and record.released and target is record:
249+
# Retirement can happen after target selection but
250+
# before acquiring the runtime condition.
251+
continue
238252
if self._delivery_error is not None:
239253
raise self._delivery_error
240254
if record.released and owner:
@@ -580,7 +594,13 @@ def supersede(self, future):
580594

581595
def work(self, record, executor, function, *, accept=lambda value: value,
582596
dispose=lambda value: None):
583-
"""Reserve result delivery before physical work, on C/P only."""
597+
"""Reserve result delivery before physical work, on C/P only.
598+
599+
The executor runs work outside C/P. Its completion retains the worker
600+
until the owner accepts or rejects the value, then disposes a rejected
601+
value there. ``accept`` is a short native state transition, serialized
602+
with shutdown and supersession; it must not block or run user code.
603+
"""
584604
self.assert_owner(record.pool)
585605
with self._condition:
586606
if (getattr(_context, 'record', None) is not record or
@@ -590,17 +610,9 @@ def work(self, record, executor, function, *, accept=lambda value: value,
590610
'result reservation')
591611
record.physical = 1
592612
self._condition.notify_all()
593-
def physical_work():
594-
with _entered('worker', record):
595-
return function()
596-
597-
try:
598-
physical = executor.submit(physical_work)
599-
except BaseException as exc:
600-
with self._condition:
601-
record.physical -= 1
602-
self._finish(record, error=exc)
603-
return _PENDING
613+
returned_to_owner = Event()
614+
adopted = False
615+
received = False
604616

605617
def completed(future):
606618
with _entered('worker', record):
@@ -611,14 +623,29 @@ def complete(future):
611623
value, error = future.result(), None
612624
except BaseException as exc:
613625
value, error = None, exc
626+
receive(value, error)
627+
628+
def receive(value, error):
629+
nonlocal received
614630
with self._condition:
631+
if received:
632+
return
633+
received = True
615634
if not self._closing:
616635
self._enqueue_locked(
617636
record, lambda: returned(value, error), reserved=True)
618-
return
619-
# Close retains this obligation without owner readmission.
637+
queued = True
638+
else:
639+
queued = False
640+
if queued and error is None:
641+
# Only the short native result transition is awaited. Neither
642+
# application callback delivery nor notification progress is
643+
# part of this handoff.
644+
returned_to_owner.wait()
645+
# Keep physical ownership until adoption or disposal completes,
646+
# including results rejected by a closed owner mailbox.
620647
try:
621-
if error is None:
648+
if error is None and not adopted:
622649
dispose(value)
623650
except BaseException:
624651
log.exception('Error disposing late management result')
@@ -628,28 +655,53 @@ def complete(future):
628655
self._release_locked(record)
629656

630657
def returned(value, error):
658+
nonlocal adopted
631659
try:
632-
if self._closing or record.handler_done:
633-
if error is None:
634-
dispose(value)
635-
return
636-
if error is not None:
637-
self._finish(record, error=error)
638-
else:
639-
try:
640-
accepted = accept(value)
641-
except BaseException as exc:
660+
with self._condition:
661+
if self._closing or record.handler_done:
662+
return
663+
if error is not None:
664+
self._finish(record, error=error)
665+
else:
642666
try:
643-
dispose(value)
644-
finally:
667+
accepted = accept(value)
668+
except BaseException as exc:
645669
self._finish(record, error=exc)
646-
else:
647-
self._finish(record, accepted)
670+
else:
671+
adopted = True
672+
self._finish(record, accepted)
648673
finally:
649-
with self._condition:
650-
record.physical -= 1
651-
self._release_locked(record)
652-
physical.add_done_callback(completed)
674+
returned_to_owner.set()
675+
676+
def physical_work():
677+
with _entered('worker', record):
678+
try:
679+
value = function()
680+
except BaseException as exc:
681+
receive(None, exc)
682+
raise
683+
else:
684+
# Hand off before completing the executor Future. A late
685+
# callback registration can run inline on C/P, but by then
686+
# this worker already owns adoption/disposal responsibility.
687+
receive(value, None)
688+
return value
689+
690+
try:
691+
physical = executor.submit(physical_work)
692+
except BaseException as exc:
693+
self._finish(record, error=exc)
694+
receive(None, exc)
695+
return _PENDING
696+
try:
697+
physical.add_done_callback(completed)
698+
except BaseException as exc:
699+
self._finish(record, error=exc)
700+
if physical.cancel():
701+
# No worker will discharge a cancellation before execution.
702+
# A callback installed before an interrupted registration may
703+
# also deliver it; receive claims the obligation exactly once.
704+
receive(None, exc)
653705
return _PENDING
654706

655707
def notify(self, callback, *, wait=False, timeout=None, budget='ordinary'):

0 commit comments

Comments
 (0)