Skip to content

asyncio.Queue: putting items out of order when it is full #83055

In progress

Description

@rhdxmr
mannequin
BPO 38874
Nosy @asvetlov, @1st1, @MojoVampire, @rhdxmr
Files
  • poc-oooput.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2019-11-21.03:25:54.744>
    labels = ['3.7', 'expert-asyncio']
    title = 'asyncio.Queue: putting items out of order when it is full'
    updated_at = <Date 2019-11-27.03:57:51.152>
    user = 'https://github.com/rhdxmr'

    bugs.python.org fields:

    activity = <Date 2019-11-27.03:57:51.152>
    actor = 'josh.r'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['asyncio']
    creation = <Date 2019-11-21.03:25:54.744>
    creator = 'esrse'
    dependencies = []
    files = ['48740']
    hgrepos = []
    issue_num = 38874
    keywords = []
    message_count = 6.0
    messages = ['357129', '357130', '357132', '357166', '357247', '357553']
    nosy_count = 4.0
    nosy_names = ['asvetlov', 'yselivanov', 'josh.r', 'esrse']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue38874'
    versions = ['Python 3.7']

    Linked PRs

    Activity

    1. YvesDup commented on Nov 20, 2023

      @YvesDup
      Contributor

      But any change that "fixed" that would make all code slower (forcing unnecessary coroutine switches), and violate existing documentation guarantees

      I don't really agree with the first statement above because the main problem is how to handle waiting and moving tasks with the Queue._putters deque.

      Here is an a very simple example to point out this use case:

      async def simplier_put_case():
          q = asyncio.Queue(1)
          for n in range(3):
              asyncio.create_task(q.put(n:=n+1))
              print(f"PUT: {n}")
          await asyncio.sleep(0.0)
      
          # queue full: 1 item in queue, 2 items waiting into the _putters
          print(f'{str(q) = }/{q.full() = }')
          print(f'GET: {await q.get()}')
          # queue not full: 1 item out of queue, 1 item waiting in the _putters, the last 1 out of _putters moving into the queue
          print(f'{str(q) = }/{q.full() = }')
      
          # steal the free solt
          print(f"PUT_NOWAIT: {q.put_nowait(n:=n+1) or n}")
          print(f'{str(q) = }/{q.full() = }')
      
          # get all items
          while (n:=n-1):
              print(f'GET: {await q.get()}')
          print(f'END {(str(q)) = }/{q.full() = }')
      
      asyncio.run(simplier_put_case())

      Ouput is:

      PUT: 1
      PUT: 2
      PUT: 3
      str(q) = '<Queue maxsize=1 _queue=[1] _putters[2] tasks=1>'/q.full() = True
      GET: 1
      str(q) = '<Queue maxsize=1 _putters[1] tasks=1>'/q.full() = False
      PUT_NOWAIT: 4
      str(q) = '<Queue maxsize=1 _queue=[4] _putters[1] tasks=2>'/q.full() = True
      GET: 4
      GET: 2
      GET: 3
      END (str(q)) = '<Queue maxsize=1 tasks=4>'/q.full() = False

      To avoid stealing a free slot, when entering in Queue.put or Queue.put_nowait, moreover to check that queue is not full, we have also to check that Queue._putters is not empty. This could indicate that there are pending task(s) or at least one item moving to a free slot.
      To make that possible, we have to modify the Queue._wakeup_next method. I suggest to leave the first 'not done' future in the Queue._putters deque, and remove it only in the Queue.put method, just after the await putter.

      Having said that, the fix seems simple.

      • preserve a 'Queue._putters' containing all pending or moving items (via their attached future). We have to move the removing of a future from _wakeup_next to Queue.put method.
      • prevent stealing a empty slot
        • in the Queue.put_nowait method, raising a new exception as QFullWithPendingOrMovingItems when Queue._putters is not empty,
        • in the Queue.put method, adding a new item in the queue only when the queue is not full and Queue._putters is empty. otherwise append it into Queue._putters deque.

      Finally, I am wondering if it should simplier to handle pending items with a Semaphore than a deque.

    2. gvanrossum commented on Nov 20, 2023

      @gvanrossum
      Member

      @YvesDup Can you phrase that suggestion in the form of a PR? I really don't want to try and interpret your four bullet points into working code plus thorough tests. You seem to have thought about this a fair deal so I expect you have everything readily available in your mind to write that PR.

    3. YvesDup commented on Dec 20, 2023

      @YvesDup
      Contributor

      @gvanrossum PR is ready.
      Please could you take a look ?
      Thank for your comments.

    4. gvanrossum commented on Dec 24, 2023

      @gvanrossum
      Member

      I am probably going to slow roll the PR, sorry. It's not that I am really against the idea of making Queue fair, it's that the change is subtle and fairly involved (witness the number of tests added!), and there is not consensus here that there is even a problem. I realize I asked for a PR, but that was in part to get you to quantify your claim "the fix seems simple". It doesn't seem simple to me -- in fact, in my experience, nothing in the area of synchronization primitives is ever simple (count how many times we had to redo Lock and Semaphore), and the size of your PR proves it.

      Maybe some other core dev cares more about this issue, but I need to limit the things I work on to a reasonable amount otherwise I wouldn't be able to sleep (I've got chronic insomnia already). Sorry!

    5. YvesDup commented on Dec 24, 2023

      @YvesDup
      Contributor

      I understand your point of view, which seems very reasonable to me. This is not easy at all, you are right. And above all I wouldn't want these issue and PR to contribute to our insomnia.
      For my part, I will push a updated version which will be my last contribution on this issue.
      Happy end of year !!

    6. gvanrossum commented on Jan 24, 2024

      @gvanrossum
      Member

      After see it the complexity of the solution I think we should not try to make the Queue class “fair” in this sense.

    7. moved this from Todo to Done in asyncioon Jan 24, 2024
    8. YvesDup commented on Sep 7, 2026

      @YvesDup
      Contributor

      I’d like to make this asyncio.Queues.queue “fair” by submitting a simple new PR that prevents the mixing of “wait” and “nowait” operations when there are pending tasks, or, failing that, add a note to the documentation describing the consequences of such mixed operations.

      Could you please reopen this ticket, or would you prefer that I create a new one referencing this one?
      Thank you

    9. moved this from Done to In Progress in asyncioon Sep 7, 2026
    10. gvanrossum commented on Sep 7, 2026

      @gvanrossum
      Member

      Reopened.

      Please understand though that I am now retired, and don't have the focus to help you by reviewing any PRs or otherwise.

    11. YvesDup commented on Sep 8, 2026

      @YvesDup
      Contributor

      Oh I am really sorry to disturb you, I am not aware about that.
      I wish you plenty of good things

    12. added a commit that references this issue on Nov 22, 2026
    13. added 2 commits that reference this issue on Dec 3, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      topic-asynciotype-bug

      Projects

      • Status
        In Progress

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions