Skip to content

Commit

Permalink
fix test. polish comments and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkdl committed Oct 8, 2024
1 parent b826210 commit 92f9799
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion newsfragments/3035.feature.rst
Original file line number Diff line number Diff line change
@@ -1 +1 @@
:class:`trio.Lock` and :class:`trio.StrictFIFOLock` will now raise :exc:`trio.BrokenResourceError` when :meth:`trio.Lock.acquire` would previously stall due to the owner of the lock having exited without releasing the lock.
:class:`trio.Lock` and :class:`trio.StrictFIFOLock` will now raise :exc:`trio.BrokenResourceError` when :meth:`trio.Lock.acquire` would previously stall due to the owner of the lock exiting without releasing the lock.
2 changes: 1 addition & 1 deletion src/trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -1897,7 +1897,7 @@ async def python_wrapper(orig_coro: Awaitable[RetT]) -> RetT:
return task

def task_exited(self, task: Task, outcome: Outcome[Any]) -> None:
# break parking lots associated with the task exiting
# break parking lots associated with the exiting task
if task in GLOBAL_PARKING_LOT_BREAKER:
for lot in GLOBAL_PARKING_LOT_BREAKER[task]:
lot.break_lot(task)
Expand Down
2 changes: 0 additions & 2 deletions src/trio/_core/_tests/test_parking_lot.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,11 @@ async def test_parking_lot_breaker_rebreak() -> None:
lot.break_lot(child_task)
nursery.cancel_scope.cancel()

# and appends the task
assert lot.broken_by == [task, child_task]


async def test_parking_lot_multiple_breakers_exit() -> None:
# register multiple tasks as lot breakers, then have them all exit
# No warning is given on task exit, even if the lot is already broken.
lot = ParkingLot()
async with trio.open_nursery() as nursery:
child_task1 = await nursery.start(dummy_task)
Expand Down
14 changes: 10 additions & 4 deletions src/trio/_tests/test_sync.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import re
import weakref
from typing import TYPE_CHECKING, Callable, Union

Expand Down Expand Up @@ -599,9 +600,10 @@ async def test_lock_acquire_unowned_lock() -> None:
lock = trio.Lock()
async with trio.open_nursery() as nursery:
nursery.start_soon(lock.acquire)
owner_str = re.escape(str(lock._lot.broken_by[0]))
with pytest.raises(
trio.BrokenResourceError,
match="^Owner of this lock exited without releasing",
match=f"^Owner of this lock exited without releasing: {owner_str}$",
):
await lock.acquire()
assert not GLOBAL_PARKING_LOT_BREAKER
Expand All @@ -615,7 +617,7 @@ async def test_lock_multiple_acquire() -> None:
with RaisesGroup(
Matcher(
trio.BrokenResourceError,
match="^Owner of this lock exited without releasing",
match="^Owner of this lock exited without releasing: ",
),
):
async with trio.open_nursery() as nursery:
Expand All @@ -626,9 +628,11 @@ async def test_lock_multiple_acquire() -> None:

async def test_lock_handover() -> None:
assert not GLOBAL_PARKING_LOT_BREAKER
child_task: Task | None = None
lock = trio.Lock()

# this task acquires the lock
lock.acquire_nowait()
child_task: Task | None = None
assert GLOBAL_PARKING_LOT_BREAKER == {
_core.current_task(): [
lock._lot,
Expand All @@ -639,11 +643,13 @@ async def test_lock_handover() -> None:
nursery.start_soon(lock.acquire)
await wait_all_tasks_blocked()

# hand over the lock to the child task
lock.release()

# check values, and get the identifier out of the dict for later check
assert len(GLOBAL_PARKING_LOT_BREAKER) == 1
child_task = next(iter(GLOBAL_PARKING_LOT_BREAKER))
assert GLOBAL_PARKING_LOT_BREAKER[child_task] == [lock._lot]

assert lock._lot.broken_by == child_task
assert lock._lot.broken_by == [child_task]
assert not GLOBAL_PARKING_LOT_BREAKER

0 comments on commit 92f9799

Please sign in to comment.