Skip to content

gh-149740: Remove redundant self.empty() check in asyncio.Queue.get()#149741

Open
deadlovelll wants to merge 1 commit into
python:mainfrom
deadlovelll:uncs-if
Open

gh-149740: Remove redundant self.empty() check in asyncio.Queue.get()#149741
deadlovelll wants to merge 1 commit into
python:mainfrom
deadlovelll:uncs-if

Conversation

@deadlovelll
Copy link
Copy Markdown

@deadlovelll deadlovelll commented May 12, 2026

Bug description:

In Lib/asyncio/queues.py, Queue.get() contains a redundant call to self.empty() inside its waiting loop:

async def get(self):
    while self.empty():
        if self._is_shutdown and self.empty():   # second self.empty() is redundant
            raise QueueShutDown

self.empty() is guaranteed to be true by the enclosing while self.empty() the loop body cannot execute otherwise. This check is a dead code.

Proposed fix:

Drop the redundant and self.empty():

while self.empty():
    if self._is_shutdown:
        raise QueueShutDown

This PR is cleanup only, no behavior change

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 12, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented May 12, 2026

I don't think it's correct. See the issue.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented May 13, 2026

Looking at the other parts of the code, I think it would make indeed more sense to consistently raise on shutdown whether the queue is empty or not. So it is not dead code but it rathet looks like a bug to me.

Comment thread Lib/asyncio/queues.py
"""
while self.empty():
if self._is_shutdown and self.empty():
if self._is_shutdown:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just change the loop to while self.empty() and not self._is_shutdown:; get_nowait already rechecks all this stuff when it's called, and would raise the necessary exception for us.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer keeping this as is, so to keep the symmetry in the get() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants