Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subprocess.Popen can hang in threaded applications in Python 2 #64517

Closed
AndrewLutomirski mannequin opened this issue Jan 20, 2014 · 7 comments
Closed

subprocess.Popen can hang in threaded applications in Python 2 #64517

AndrewLutomirski mannequin opened this issue Jan 20, 2014 · 7 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@AndrewLutomirski
Copy link
Mannequin

AndrewLutomirski mannequin commented Jan 20, 2014

BPO 20318
Nosy @gpshead, @vstinner, @bitdancer, @vadmium
Files
  • python_thread_bug.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 = 'https://github.com/gpshead'
    closed_at = <Date 2014-04-15.15:10:31.187>
    created_at = <Date 2014-01-20.20:30:18.989>
    labels = ['type-bug', 'library']
    title = 'subprocess.Popen can hang in threaded applications in Python 2'
    updated_at = <Date 2014-04-15.15:10:31.186>
    user = 'https://bugs.python.org/AndrewLutomirski'

    bugs.python.org fields:

    activity = <Date 2014-04-15.15:10:31.186>
    actor = 'gregory.p.smith'
    assignee = 'gregory.p.smith'
    closed = True
    closed_date = <Date 2014-04-15.15:10:31.187>
    closer = 'gregory.p.smith'
    components = ['Library (Lib)']
    creation = <Date 2014-01-20.20:30:18.989>
    creator = 'Andrew.Lutomirski'
    dependencies = []
    files = ['33571']
    hgrepos = []
    issue_num = 20318
    keywords = []
    message_count = 7.0
    messages = ['208577', '208578', '208582', '208584', '208585', '208606', '216305']
    nosy_count = 6.0
    nosy_names = ['gregory.p.smith', 'vstinner', 'gps', 'r.david.murray', 'martin.panter', 'Andrew.Lutomirski']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue20318'
    versions = ['Python 2.7']

    @AndrewLutomirski
    Copy link
    Mannequin Author

    AndrewLutomirski mannequin commented Jan 20, 2014

    Running python python_thread_bug.py -j4 often results in one of the threads failing to start until another thread finishes.

    The bug appears to be that subprocess's pipe_cloexec function is racy: if another thread forks between os.pipe() and _set_cloexec_flag, then the resulting process could hang on to the write end of the pipe. That will cause the Popen call that got rudely interrupted to wait until the whole resulting process tree dies.

    @AndrewLutomirski AndrewLutomirski mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 20, 2014
    @bitdancer
    Copy link
    Member

    Hmm. I thought someone had already reported this, but I can't find an issue. I suspect it is fixed in 3.4 and it may not be practical to fix it in earlier versions.

    @gpshead
    Copy link
    Member

    gpshead commented Jan 20, 2014

    2.7 subprocess is fundamentally flawed and cannot be fixed. Install subprocess32 from PyPI. It's fixed in 3.2 and later.

    @gpshead gpshead closed this as completed Jan 20, 2014
    @AndrewLutomirski
    Copy link
    Mannequin Author

    AndrewLutomirski mannequin commented Jan 20, 2014

    Would it be worth adding something to the Python 2.7 subprocess docs indicating that subprocess is known to be broken?

    @AndrewLutomirski AndrewLutomirski mannequin reopened this Jan 20, 2014
    @AndrewLutomirski
    Copy link
    Mannequin Author

    AndrewLutomirski mannequin commented Jan 20, 2014

    FWIW, sticking a mutex in Popen.__init__ (wrapping the whole thing) seems to work around this issue (for programs that aren't using multiprocessing or fork, for example). This might be a good-enough fix and be safe enough to stick in the standard library.

    @AndrewLutomirski AndrewLutomirski mannequin closed this as completed Jan 20, 2014
    @gpshead
    Copy link
    Member

    gpshead commented Jan 21, 2014

    A pointer in the 2.7 subprocess docs to subprocess32 does seem like a good idea, its what i tell everyone to do anyways. :)

    If you've got a patch for this in 2.7 feel free to add it here and I can take a look.

    leaving this open as a reminder to me to update the docs at the very least.

    @gpshead gpshead reopened this Jan 21, 2014
    @gpshead gpshead self-assigned this Jan 21, 2014
    @vstinner vstinner changed the title subprocess.Popen can hang in threaded applications subprocess.Popen can hang in threaded applications in Python 2 Feb 3, 2014
    @gpshead
    Copy link
    Member

    gpshead commented Apr 15, 2014

    I added a pointer to subprocess32 in the 2.7 subprocess docs in dd52365c8721.

    @gpshead gpshead closed this as completed Apr 15, 2014
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants