Closed
Description
Some time ago - never mind how long precisely - having a little coordination between tasks and nothing particular to interest me in semaphores, I saw our RuntimeError("can't release a Lock you don't own")
.
I approve pretty strongly of keeping things local by default; it prevents all sorts of bugs and trio.Semaphore
is right there if you need something without this limitation. But two further observations prompted this issue:
trio.Semaphore()
is considerably more powerful and flexible in general. By the principle of least power, it might be nice to havetrio.Lock(*, allow_outside_release: bool = False)
or something along those lines.- If the owning task finishes (returns or raises) without releasing the lock, then you're stuck forever. The toy program below demonstrates. Perhaps we should raise an error or at least emit a warning when a task which owns a lock finishes?
import trio
@trio.run
async def main():
lock = trio.Lock()
async with trio.open_nursery() as nursery:
nursery.start_soon(lock.acquire)
# The lock is now held, and can only be released by the acquiring task.
# However, that task is now finished, so we can never release the lock!
lock.release()
Metadata
Metadata
Assignees
Labels
No labels