Skip to content

Commit 1ab063f

Browse files
committed
libev: serialize management release admission with cleanup
Hold the existing startup lock across the release shutdown check and callback admission. Cleanup can then either drain an accepted release on the runner or let the caller detach after shutdown has begun. Exercise both orders with the native loop and actual cleanup, observing the admission lock rather than relying on a sleep to establish the race.
1 parent 4021091 commit 1ab063f

2 files changed

Lines changed: 91 additions & 6 deletions

File tree

‎cassandra/io/libevreactor.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,15 @@ def detach():
285285
completed()
286286
if current_thread() is self._thread:
287287
detach()
288-
elif self._shutdown or self._thread is None or not self._thread.is_alive():
289-
# Shutdown closes admission before the runner finishes draining.
290-
# Release remains idempotent while that runner unwinds.
291-
with self._lock:
292-
detach()
293288
else:
294-
self.call_soon(detach)
289+
# Cleanup closes admission under this same lock. Either queue
290+
# the release before shutdown or detach after it has begun.
291+
with self._lock_thread:
292+
if self._shutdown or self._thread is None or not self._thread.is_alive():
293+
with self._lock:
294+
detach()
295+
else:
296+
self.call_soon(detach)
295297

296298
if current_thread() is self._thread:
297299
acquire()

‎tests/unit/test_management_reactors.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,89 @@ def borrow():
318318
loop._cleanup()
319319

320320

321+
@pytest.mark.parametrize('cleanup_order', ['during-release', 'before-release'])
322+
def test_libev_borrow_release_completes_during_cleanup(monkeypatch, cleanup_order):
323+
module = reactor_module('libev')
324+
loop = module.LibevLoop()
325+
release_borrow = loop.borrow_management()
326+
entered, finish, admission_attempted, stopped, completed = [Event() for _ in range(5)]
327+
errors, observations, blocked = [], [], []
328+
admission_lock, keeper = loop._lock_thread, loop._management_keeper
329+
dispatch = loop.call_soon
330+
def hold():
331+
entered.set()
332+
assert finish.wait(5)
333+
def cleanup():
334+
try:
335+
loop._cleanup()
336+
except BaseException as exc:
337+
errors.append(exc)
338+
stopper = Thread(target=cleanup)
339+
class ObservedLock:
340+
def __enter__(self):
341+
if current_thread() is stopper:
342+
acquired = admission_lock.acquire(False)
343+
blocked.append(not acquired)
344+
admission_attempted.set()
345+
if acquired:
346+
return self
347+
admission_lock.acquire()
348+
return self
349+
def __exit__(self, *args):
350+
admission_lock.release()
351+
class ObservedKeeper:
352+
def stop(self):
353+
keeper.stop()
354+
stopped.set()
355+
def __getattr__(self, name):
356+
return getattr(keeper, name)
357+
def racing_dispatch(callback):
358+
stopper.start()
359+
assert admission_attempted.wait(5)
360+
if not blocked[0]:
361+
# The unprotected release allows cleanup to commit shutdown here.
362+
assert stopped.wait(5)
363+
dispatch(callback)
364+
def detached():
365+
observations.append(current_thread())
366+
completed.set()
367+
try:
368+
dispatch(hold)
369+
assert entered.wait(5)
370+
runner = loop._thread
371+
monkeypatch.setattr(loop, '_lock_thread', ObservedLock())
372+
monkeypatch.setattr(loop, '_management_keeper', ObservedKeeper())
373+
if cleanup_order == 'during-release':
374+
monkeypatch.setattr(loop, 'call_soon', racing_dispatch)
375+
else:
376+
stopper.start()
377+
assert stopped.wait(5)
378+
assert loop._shutdown
379+
release_borrow(detached)
380+
assert stopped.wait(5)
381+
finish.set()
382+
assert completed.wait(5)
383+
stopper.join(5)
384+
runner.join(5)
385+
assert not stopper.is_alive()
386+
assert not runner.is_alive()
387+
assert errors == []
388+
assert not loop._management_users
389+
assert not loop._management_callbacks
390+
assert len(observations) == 1
391+
if cleanup_order == 'during-release':
392+
assert blocked[0], 'cleanup passed the release admission boundary'
393+
assert observations == [runner]
394+
else:
395+
assert observations == [current_thread()]
396+
finally:
397+
finish.set()
398+
if stopper.ident is not None:
399+
stopper.join(5)
400+
loop._cleanup()
401+
release_borrow(lambda: None)
402+
403+
321404
def test_chaining_application_loop_closes_when_management_join_raises(monkeypatch, connection_class):
322405
# Exercise the actual chaining test's failure cleanup, then supply our own
323406
# fallback cleanup so a negative control cannot leak a non-daemon thread.

0 commit comments

Comments
 (0)