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

PyOS_ReadlineFunctionPointer violates PyMem_Malloc() API: the GIL is not hold #62405

Closed
vstinner opened this issue Jun 13, 2013 · 3 comments
Closed
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir

Comments

@vstinner
Copy link
Member

BPO 18205
Nosy @vstinner
Superseder
  • bpo-16742: PyOS_Readline drops GIL and calls PyOS_StdioReadline, which isn't thread safe
  • Files
  • readline_gil.patch
  • 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 2013-06-13.21:20:09.230>
    created_at = <Date 2013-06-13.21:02:17.440>
    labels = ['interpreter-core', 'library']
    title = 'PyOS_ReadlineFunctionPointer violates PyMem_Malloc() API: the GIL is not hold'
    updated_at = <Date 2013-06-13.22:07:58.879>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2013-06-13.22:07:58.879>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-06-13.21:20:09.230>
    closer = 'vstinner'
    components = ['Interpreter Core', 'Library (Lib)']
    creation = <Date 2013-06-13.21:02:17.440>
    creator = 'vstinner'
    dependencies = []
    files = ['30577']
    hgrepos = []
    issue_num = 18205
    keywords = ['patch']
    message_count = 3.0
    messages = ['191089', '191090', '191091']
    nosy_count = 1.0
    nosy_names = ['vstinner']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '16742'
    type = None
    url = 'https://bugs.python.org/issue18205'
    versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']

    @vstinner
    Copy link
    Member Author

    The callback PyOS_ReadlineFunctionPointer (used to read a line from the standard input) must return a buffer allocated by PyMem_Malloc(), but PyOS_Readline() releases the GIL before calling PyOS_ReadlineFunctionPointer.

    Simplified extract of PyOS_Readline():

    Py_BEGIN_ALLOW_THREADS
    if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
        rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
    else
        rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
                                             prompt);
    Py_END_ALLOW_THREADS
    

    tok_nextc() calls PyOS_Readline() and calls PyMem_FREE() to release its result.

    PyOS_ReadlineFunctionPointer should allocate memory using malloc(), not using PyMem_Malloc(). But PyOS_Readline() should copy the line into a buffer allocated by PyMem_Malloc() to keep backward compatibility.

    See also issue bpo-18203 and bpo-3329.

    @vstinner vstinner added interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir labels Jun 13, 2013
    @vstinner
    Copy link
    Member Author

    Here is a patch for Python 3.4.

    @vstinner
    Copy link
    Member Author

    Oh, this is a duplicate of bpo-16742, read also the thread on python-dev:
    http://mail.python.org/pipermail/python-dev/2012-December/123225.html

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant