Skip to content

Potential deadlock in concurrent futures when garbage collection occurs during Queue.get #65208

Closed

Description

@simonjagoe
BPO 21009
Nosy @gpshead, @brianquinlan, @mdickinson, @pitrou, @applio
Superseder
  • bpo-32576: concurrent.futures.thread deadlock due to Queue in weakref callback
  • Files
  • executor-hang.py: Script exercising hang
  • executor-hang-faulthandler.txt
  • 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 = <Date 2018-01-17.22:03:30.250>
    created_at = <Date 2014-03-21.15:47:26.814>
    labels = ['type-bug', 'library']
    title = 'Potential deadlock in concurrent futures when garbage collection occurs during Queue.get'
    updated_at = <Date 2018-04-10.05:47:26.678>
    user = 'https://bugs.python.org/simonjagoe'

    bugs.python.org fields:

    activity = <Date 2018-04-10.05:47:26.678>
    actor = 'devurandom'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-01-17.22:03:30.250>
    closer = 'gregory.p.smith'
    components = ['Library (Lib)']
    creation = <Date 2014-03-21.15:47:26.814>
    creator = 'simon.jagoe'
    dependencies = []
    files = ['34551', '47132']
    hgrepos = []
    issue_num = 21009
    keywords = []
    message_count = 10.0
    messages = ['214380', '215972', '274591', '275413', '275414', '275424', '301168', '301811', '310207', '310212']
    nosy_count = 9.0
    nosy_names = ['gregory.p.smith', 'bquinlan', 'mark.dickinson', 'pitrou', 'devurandom', 'simon.jagoe', 'Chris.Farrow', 'davin', 'Patrik Dufresne']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'patch review'
    status = 'closed'
    superseder = '32576'
    type = 'behavior'
    url = 'https://bugs.python.org/issue21009'
    versions = ['Python 3.3', 'Python 3.4']

    Activity

    1. simonjagoe commented on Mar 21, 2014

      simonjagoemannequin
      MannequinAuthor

      At Enthought we have been tracking a deadlock in some code that turned out to be due to the following scenario:

      1. There is some cyclic garbage that requires collection; an object in the garbage is referred to by a weakref with a callback
      2. You have a lock that is acquired
      3. While the lock is held, the garbage collector runs (on the same thread)
      4. The weakref callback in (0) is called via the garbage collecter and the callback tries to acquire the same lock that was acquired in (1)

      Attached is a simple script that exercises the issue in Python 3.3. The script monkey-patches Queue.get to force garbage collection after acquiring the Queue.not_empty lock.

    2. added
      stdlib
      type-bug
      on Mar 21, 2014
    3. mdickinson commented on Apr 12, 2014

      @mdickinson
      Member

      Adding Brian Quinlan to the nosy list.

      I don't see an easy way around this other than documenting it and recommending that people do explicit executor shutdown where necessary.

      Weakref callbacks are dangerous things. Weakref callbacks that acquire a lock at any point doubly so.

    4. PatrikDufresne commented on Sep 6, 2016

      PatrikDufresnemannequin
      Mannequin

      I've encounter this issue. To easily avoid this issue, I've change queue.put(None) to queue.put(None, block=False) to work around this.

    5. applio commented on Sep 9, 2016

      @applio
      Member

      Sounds related to bpo-14976.

    6. applio commented on Sep 9, 2016

      @applio
      Member

      Per the decision of bpo-14976.

    7. mdickinson commented on Sep 9, 2016

      @mdickinson
      Member

      Davin: I don't see the link with bpo-14976. There are no external signals involved in this issue. There's just a piece of simple pure Python code, which can deadlock if gc happens to kick in at the wrong moment.

      I don't think this issue should be closed.

    8. reopened this on Sep 9, 2016
    9. pitrou commented on Sep 2, 2017

      @pitrou
      Member

      I don't really understand this issue. How is concurrent.futures supposed to be the culprit here? Can you elaborate which weakref callback you are talking about?

    10. simonjagoe commented on Sep 10, 2017

      simonjagoemannequin
      MannequinAuthor

      In the script attached to the original issue, the weakref callback that causes the hang is the callback defined in ThreadPoolExecutor._adjust_thread_count

      Attached is a faulthandler stack captured from Python 3.6.1. The script submitted here uses a patched queue.get() method to force garbage collection to reliably trigger the issue, but this deadlock was observed in a real application using threadpool executors.

    11. gpshead commented on Jan 17, 2018

      @gpshead
      Member

      there's a PR on the other issue i opened for this without noticing this one so i'm marking this as the dup.

    12. pitrou commented on Jan 17, 2018

      @pitrou
      Member

      There's a proposed fix in #5216
      It would be nice if someone able to reproduce this issue could test it (the reproducer script here wouldn't work with the new SimpleQueue implementation).

    13. transferred this issue fromon Apr 10, 2022
    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

      stdlibtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions