Skip to content

Remove dead try/except StopIteration in asyncio.Lock._wake_up_first #150116

@deadlovelll

Description

@deadlovelll

Bug report

Bug description:

asyncio.Lock._wake_up_first contains an unreachable try/except StopIteration block:

  def _wake_up_first(self):
      """Ensure that the first waiter will wake up."""
      if not self._waiters:
          return
      try:
          fut = next(iter(self._waiters))
      except StopIteration:
          return
      ...

Early return at if not self._waiters already gurantees that self._waiters is not an empty collections.deque. next(iter(deque)) on cannot raise StopIteration, so there is unreachable dead code.

Proposed change:

def _wake_up_first(self):
      """Ensure that the first waiter will wake up."""
      if not self._waiters:
          return
      fut = next(iter(self._waiters))
      # .done() means that the waiter is already set to wake up.
      if not fut.done():
          fut.set_result(True)

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotype-refactorCode refactoring (with no changes in behavior)
    No fields configured for issues without a type.

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions