Skip to content

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

Merged
kumaraditya303 merged 2 commits into
python:mainfrom
deadlovelll:uncs-if
Jul 8, 2026
Merged

gh-149740: Remove redundant self.empty() check in asyncio.Queue.get()#149741
kumaraditya303 merged 2 commits into
python:mainfrom
deadlovelll:uncs-if

Conversation

@deadlovelll

@deadlovelll deadlovelll commented May 12, 2026

Copy link
Copy Markdown
Contributor

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

bedevere-app Bot commented May 12, 2026

Copy link
Copy Markdown

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

picnixz commented May 12, 2026

Copy link
Copy Markdown
Member

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

@picnixz

picnixz commented May 13, 2026

Copy link
Copy Markdown
Member

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.

@deadlovelll

Copy link
Copy Markdown
Contributor Author

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.

Hello! Sorry for the slow follow-up, just lost the conversation.

Its not a bug because get() only raises QueueShutDown if its empty and shutdown, OR queue was shutdown immediatly (link). shutdown(immedeate=True) is not separate path, it sets the self._is_shutdown to True, then clears the queue. get() sees an empty queue and shutdown flag and raises QueueShutDown (link)

You also mentioned in issue body that there can be race, but all asyncio primitives are not thread-safe (link)

So there's no behavioral bug here, just drops the duplicate check

We'll be happy to close it if you prefer to leave it as is

@kumaraditya303 kumaraditya303 enabled auto-merge (squash) July 8, 2026 09:39
@kumaraditya303 kumaraditya303 merged commit 754b9f2 into python:main Jul 8, 2026
47 of 48 checks passed
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.

4 participants