X Tutup
The Wayback Machine - https://web.archive.org/web/20250612200902/https://github.com/python/cpython/issues/92824
Skip to content

Document whether asyncio.wait_for(q.get(), timeout) is safe of race-conditions. #92824

Closed as not planned
@Yaakov-Belch

Description

@Yaakov-Belch

Documentation

Please add the following text (or some equivalent) to the documentation of asyncio.Queue:

The construct asyncio.wait_for(queue.get(), timeout) is guaranteed to be free of race conditions. It will not lose queue items.

Explanation: According to the public documentation, this construct is allowed to lose queue items: The documentation states that wait_for uses cancellation which is implemented by raising a CancelledError exception. Depending on the implementation, this may happen in the call queue.get() after an item has been removed from the queue. This would cause the extracted queue item to be lost.

According to the current documentation, this race condition is allowed: Such a race condition would be surprising for most programmers --- but it would not violate the letter of the documentation. Hence, judging only the documented features, a responsible programmer will hesitate to use this construct in production settings where losing queue items is not acceptable.

This issue has been reported and addressed at least twice:

Based on the comments in these two bug reports it seems to me that the current implementation avoids this race condition. However, this is not clearly spelled out as a commitment for future versions.

Conclusion
At the current state of affairs, a responsible programmer will find no assurance in the documentation of whether the construct asyncio.wait_for(queue.get(), timeout) can safely be used in production environments. He will need to spend much time (I spend more than two hours) to search for bug reports such as listed above and still be left with doubts about whether he can safely use the simple construct or still needs to implement a safe queue-with-timeout-in-get.

If the python implementation promises to avoid this race condition --- please state it clearly in the documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup