Skip to content

Commit 7f08340

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 7f08340

2 files changed

Lines changed: 322 additions & 39 deletions

File tree

‎cassandra/_management.py‎

Lines changed: 87 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def _state(self):
149149
# Bulk waits read state after acquiring ALL future conditions.
150150
# Reject before waiter installation, where their context unwinds
151151
# every lock. Own nonblocking methods use scoped access above.
152-
assert_can_wait()
152+
self._assert_can_wait()
153153
return state
154154

155155
@_state.setter
@@ -179,16 +179,25 @@ def cancelled(self):
179179
def result(self, timeout=None):
180180
self._record.runtime._check_pid()
181181
if not self.done():
182-
assert_can_wait()
182+
self._assert_can_wait()
183183
return super().result(timeout)
184184

185185
@_future_access
186186
def exception(self, timeout=None):
187187
self._record.runtime._check_pid()
188188
if not self.done():
189-
assert_can_wait()
189+
self._assert_can_wait()
190190
return super().exception(timeout)
191191

192+
def _assert_can_wait(self):
193+
assert_can_wait()
194+
current = getattr(_context, 'record', None)
195+
if (execution_context() == 'callback' and current is not None and
196+
current.runtime is self._record.runtime and
197+
self._record.envelope.kind == 'notification'):
198+
raise ManagementContextError(
199+
'Notification wait would block its own callback lane')
200+
192201
@_future_access
193202
def cancel(self):
194203
self._record.runtime._check_pid()
@@ -235,6 +244,10 @@ def add_done_callback(self, fn):
235244
with self._condition:
236245
if record is not self._delivery_record:
237246
continue
247+
if owner and record.released and target is record:
248+
# Retirement can happen after target selection but
249+
# before acquiring the runtime condition.
250+
continue
238251
if self._delivery_error is not None:
239252
raise self._delivery_error
240253
if record.released and owner:
@@ -580,7 +593,13 @@ def supersede(self, future):
580593

581594
def work(self, record, executor, function, *, accept=lambda value: value,
582595
dispose=lambda value: None):
583-
"""Reserve result delivery before physical work, on C/P only."""
596+
"""Reserve result delivery before physical work, on C/P only.
597+
598+
The executor runs work outside C/P. Its completion retains the worker
599+
until the owner accepts or rejects the value, then disposes a rejected
600+
value there. ``accept`` is a short native state transition, serialized
601+
with shutdown and supersession; it must not block or run user code.
602+
"""
584603
self.assert_owner(record.pool)
585604
with self._condition:
586605
if (getattr(_context, 'record', None) is not record or
@@ -590,17 +609,9 @@ def work(self, record, executor, function, *, accept=lambda value: value,
590609
'result reservation')
591610
record.physical = 1
592611
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
612+
returned_to_owner = Event()
613+
adopted = False
614+
received = False
604615

605616
def completed(future):
606617
with _entered('worker', record):
@@ -611,14 +622,29 @@ def complete(future):
611622
value, error = future.result(), None
612623
except BaseException as exc:
613624
value, error = None, exc
625+
receive(value, error)
626+
627+
def receive(value, error):
628+
nonlocal received
614629
with self._condition:
630+
if received:
631+
return
632+
received = True
615633
if not self._closing:
616634
self._enqueue_locked(
617635
record, lambda: returned(value, error), reserved=True)
618-
return
619-
# Close retains this obligation without owner readmission.
636+
queued = True
637+
else:
638+
queued = False
639+
if queued and error is None:
640+
# Only the short native result transition is awaited. Neither
641+
# application callback delivery nor notification progress is
642+
# part of this handoff.
643+
returned_to_owner.wait()
644+
# Keep physical ownership until adoption or disposal completes,
645+
# including results rejected by a closed owner mailbox.
620646
try:
621-
if error is None:
647+
if error is None and not adopted:
622648
dispose(value)
623649
except BaseException:
624650
log.exception('Error disposing late management result')
@@ -628,28 +654,53 @@ def complete(future):
628654
self._release_locked(record)
629655

630656
def returned(value, error):
657+
nonlocal adopted
631658
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:
659+
with self._condition:
660+
if self._closing or record.handler_done:
661+
return
662+
if error is not None:
663+
self._finish(record, error=error)
664+
else:
642665
try:
643-
dispose(value)
644-
finally:
666+
accepted = accept(value)
667+
except BaseException as exc:
645668
self._finish(record, error=exc)
646-
else:
647-
self._finish(record, accepted)
669+
else:
670+
adopted = True
671+
self._finish(record, accepted)
648672
finally:
649-
with self._condition:
650-
record.physical -= 1
651-
self._release_locked(record)
652-
physical.add_done_callback(completed)
673+
returned_to_owner.set()
674+
675+
def physical_work():
676+
with _entered('worker', record):
677+
try:
678+
value = function()
679+
except BaseException as exc:
680+
receive(None, exc)
681+
raise
682+
else:
683+
# Hand off before completing the executor Future. A late
684+
# callback registration can run inline on C/P, but by then
685+
# this worker already owns adoption/disposal responsibility.
686+
receive(value, None)
687+
return value
688+
689+
try:
690+
physical = executor.submit(physical_work)
691+
except BaseException as exc:
692+
self._finish(record, error=exc)
693+
receive(None, exc)
694+
return _PENDING
695+
try:
696+
physical.add_done_callback(completed)
697+
except BaseException as exc:
698+
self._finish(record, error=exc)
699+
if physical.cancel():
700+
# No worker will discharge a cancellation before execution.
701+
# A callback installed before an interrupted registration may
702+
# also deliver it; receive claims the obligation exactly once.
703+
receive(None, exc)
653704
return _PENDING
654705

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

0 commit comments

Comments
 (0)