Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-112202: Ensure that condition.notify() succeeds even when racing with Task.cancel() #112201

Merged
merged 8 commits into from
Feb 3, 2024
42 changes: 21 additions & 21 deletions Lib/asyncio/locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ class Lock(_ContextManagerMixin, mixins._LoopBoundMixin):
"""Primitive lock objects.

A primitive lock is a synchronization primitive that is not owned
by a particular coroutine when locked. A primitive lock is in one
by a particular task when locked. A primitive lock is in one
of two states, 'locked' or 'unlocked'.

It is created in the unlocked state. It has two basic methods,
acquire() and release(). When the state is unlocked, acquire()
changes the state to locked and returns immediately. When the
state is locked, acquire() blocks until a call to release() in
another coroutine changes it to unlocked, then the acquire() call
another task changes it to unlocked, then the acquire() call
resets it to locked and returns. The release() method should only
be called in the locked state; it changes the state to unlocked
and returns immediately. If an attempt is made to release an
unlocked lock, a RuntimeError will be raised.

When more than one coroutine is blocked in acquire() waiting for
the state to turn to unlocked, only one coroutine proceeds when a
release() call resets the state to unlocked; first coroutine which
When more than one task is blocked in acquire() waiting for
the state to turn to unlocked, only one task proceeds when a
release() call resets the state to unlocked; first task which
is blocked in acquire() is being processed.
kristjanvalur marked this conversation as resolved.
Show resolved Hide resolved

acquire() is a coroutine and should be called with 'await'.
kristjanvalur marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -130,7 +130,7 @@ def release(self):
"""Release a lock.

When the lock is locked, reset it to unlocked, and return.
If any other coroutines are blocked waiting for the lock to become
If any other tasks are blocked waiting for the lock to become
unlocked, allow exactly one of them to proceed.

When invoked on an unlocked lock, a RuntimeError is raised.
Expand Down Expand Up @@ -182,8 +182,8 @@ def is_set(self):
return self._value

def set(self):
"""Set the internal flag to true. All coroutines waiting for it to
become true are awakened. Coroutine that call wait() once the flag is
"""Set the internal flag to true. All tasks waiting for it to
become true are awakened. Tasks that call wait() once the flag is
true will not block at all.
"""
if not self._value:
Expand All @@ -194,7 +194,7 @@ def set(self):
fut.set_result(True)

def clear(self):
"""Reset the internal flag to false. Subsequently, coroutines calling
"""Reset the internal flag to false. Subsequently, tasks calling
wait() will block until set() is called to set the internal flag
to true again."""
self._value = False
Expand All @@ -203,7 +203,7 @@ async def wait(self):
"""Block until the internal flag is true.

If the internal flag is true on entry, return True
immediately. Otherwise, block until another coroutine calls
immediately. Otherwise, block until another task calls
set() to set the flag to true, then return True.
"""
if self._value:
Expand All @@ -222,8 +222,8 @@ class Condition(_ContextManagerMixin, mixins._LoopBoundMixin):
"""Asynchronous equivalent to threading.Condition.

This class implements condition variable objects. A condition variable
allows one or more coroutines to wait until they are notified by another
coroutine.
allows one or more tasks to wait until they are notified by another
task.

A new Lock object is created and used as the underlying lock.
"""
Expand All @@ -250,12 +250,12 @@ def __repr__(self):
async def wait(self):
"""Wait until notified.

If the calling coroutine has not acquired the lock when this
If the calling task has not acquired the lock when this
method is called, a RuntimeError is raised.

This method releases the underlying lock, and then blocks
until it is awakened by a notify() or notify_all() call for
the same condition variable in another coroutine. Once
the same condition variable in another task. Once
awakened, it re-acquires the lock and returns True.

This method may return spuriously,
Expand Down Expand Up @@ -317,14 +317,14 @@ async def wait_for(self, predicate):
return result

def notify(self, n=1):
"""By default, wake up one coroutine waiting on this condition, if any.
If the calling coroutine has not acquired the lock when this method
"""By default, wake up one task waiting on this condition, if any.
If the calling task has not acquired the lock when this method
is called, a RuntimeError is raised.

This method wakes up n of the coroutines waiting for the condition
This method wakes up n of the tasks waiting for the condition
variable; if fewer than n are waiting, they are all awoken.

Note: an awakened coroutine does not actually return from its
Note: an awakened task does not actually return from its
wait() call until it can reacquire the lock. Since notify() does
not release the lock, its caller should.
"""
Expand Down Expand Up @@ -390,7 +390,7 @@ async def acquire(self):

If the internal counter is larger than zero on entry,
decrement it by one and return True immediately. If it is
zero on entry, block, waiting until some other coroutine has
zero on entry, block, waiting until some other task has
called release() to make it larger than 0, and then return
True.
"""
Expand Down Expand Up @@ -430,8 +430,8 @@ async def acquire(self):
def release(self):
"""Release a semaphore, incrementing the internal counter by one.

When it was zero on entry and another coroutine is waiting for it to
become larger than zero again, wake up that coroutine.
When it was zero on entry and another task is waiting for it to
become larger than zero again, wake up that task.
"""
self._value += 1
self._wake_up_next()
Expand Down