Skip to content

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

Closed

Description

@vstinner
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']

    Activity

    1. vstinner commented on Jun 13, 2013

      @vstinner
      MemberAuthor

      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.

    2. added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      stdlibStandard Library Python modules in the Lib/ directory
      on Jun 13, 2013
    3. vstinner commented on Jun 13, 2013

      @vstinner
      MemberAuthor

      Here is a patch for Python 3.4.

    4. vstinner commented on Jun 13, 2013

      @vstinner
      MemberAuthor

      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

    5. 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

      interpreter-core(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directory

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions