Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
gh-112202: Ensure that condition.notify() succeeds even when racing with Task.cancel()#112201
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
fa8d5e4df113e513827d1c8e6b13db3f83a0dad88012bb75892566a1File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -24,25 +24,23 @@ class Lock(_ContextManagerMixin, mixins._LoopBoundMixin): | ||
| """Primitive lock objects. | ||
| A primitive lock is a synchronization primitive that is not owned | ||
| by a particulartask 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 | ||
| anothertask 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 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; successive release() | ||
| calls will unblock tasks in FIFO order. | ||
| Locks also support the asynchronous context management protocol. | ||
| 'async with lock' statement should be used. | ||
| @@ -130,7 +128,7 @@ def release(self): | ||
| """Release a lock. | ||
| When the lock is locked, reset it to unlocked, and return. | ||
| If any othertasks 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. | ||
| @@ -182,8 +180,8 @@ def is_set(self): | ||
| return self._value | ||
| def set(self): | ||
| """Set the internal flag to true. Alltasks 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: | ||
| @@ -194,7 +192,7 @@ def set(self): | ||
| fut.set_result(True) | ||
| def clear(self): | ||
| """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 | ||
| @@ -203,7 +201,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 anothertask calls | ||
| set() to set the flag to true, then return True. | ||
| """ | ||
| if self._value: | ||
| @@ -222,8 +220,8 @@ class Condition(_ContextManagerMixin, mixins._LoopBoundMixin): | ||
| """Asynchronous equivalent to threading.Condition. | ||
| This class implements condition variable objects. A condition variable | ||
| allows one or moretasks to wait until they are notified by another | ||
| task. | ||
| A new Lock object is created and used as the underlying lock. | ||
| """ | ||
| @@ -250,50 +248,64 @@ def __repr__(self): | ||
| async def wait(self): | ||
| """Wait until notified. | ||
| If the callingtask 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 anothertask. Once | ||
| awakened, it re-acquires the lock and returns True. | ||
| This method may return spuriously, | ||
| which is why the caller should always | ||
| re-check the state and be prepared to wait() again. | ||
| """ | ||
| if not self.locked(): | ||
| raise RuntimeError('cannot wait on un-acquired lock') | ||
| fut = self._get_loop().create_future() | ||
| self.release() | ||
| try: | ||
| try: | ||
| self._waiters.append(fut) | ||
| try: | ||
| await fut | ||
| return True | ||
| finally: | ||
| self._waiters.remove(fut) | ||
| finally: | ||
| # Must re-acquire lock even if wait is cancelled. | ||
| # We only catch CancelledError here, since we don't want any | ||
| # other (fatal) errors with the future to cause us to spin. | ||
| err = None | ||
| while True: | ||
| try: | ||
| await self.acquire() | ||
| break | ||
| except exceptions.CancelledError as e: | ||
| err = e | ||
| if err is not None: | ||
| try: | ||
| raise err # Re-raise most recent exception instance. | ||
| finally: | ||
| err = None # Break reference cycles. | ||
| except BaseException: | ||
| # Any error raised out of here _may_ have occurred after this Task | ||
| # believed to have been successfully notified. | ||
| # Make sure to notify another Task instead. This may result | ||
| # in a "spurious wakeup", which is allowed as part of the | ||
| # Condition Variable protocol. | ||
| self._notify(1) | ||
| raise | ||
| async def wait_for(self, predicate): | ||
| """Wait until a predicate becomes true. | ||
| The predicate should be a callable whose result will be | ||
| interpreted as a boolean value. The method will repeatedly | ||
| wait() until it evaluates to true. The final predicate value is | ||
| the return value. | ||
| """ | ||
| result = predicate() | ||
| @@ -303,20 +315,22 @@ async def wait_for(self, predicate): | ||
| return result | ||
| def notify(self, n=1): | ||
| """By default, wake up onetask waiting on this condition, if any. | ||
| If the callingtask has not acquired the lock when this method | ||
| is called, a RuntimeError is raised. | ||
| This method wakes up n of thetasks waiting for the condition | ||
| variable;if fewer than n are waiting, they areall awoken. | ||
| Note: an awakenedtask does not actually return from its | ||
| wait() call until it can reacquire the lock. Since notify() does | ||
| not release the lock, its caller should. | ||
| """ | ||
| if not self.locked(): | ||
| raise RuntimeError('cannot notify on un-acquired lock') | ||
| self._notify(n) | ||
| def _notify(self, n): | ||
| idx = 0 | ||
| for fut in self._waiters: | ||
| if idx >= n: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Note that the docstring for ContributorAuthor
| ||
| @@ -374,7 +388,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 othertask has | ||
| called release() to make it larger than 0, and then return | ||
| True. | ||
| """ | ||
| @@ -414,8 +428,8 @@ async def acquire(self): | ||
| def release(self): | ||
| """Release a semaphore, incrementing the internal counter by one. | ||
| When it was zero on entry and anothertask is waiting for it to | ||
| become larger than zero again, wake up thattask. | ||
| """ | ||
| self._value += 1 | ||
| self._wake_up_next() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Ensure that a :func:`asyncio.Condition.notify` call does not get lost if the awakened ``Task`` is simultaneously cancelled or encounters any other error. |