Navigation Menu

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

SharedMemory crash when size is 0 #85516

Closed
vinay0410 mannequin opened this issue Jul 20, 2020 · 9 comments
Closed

SharedMemory crash when size is 0 #85516

vinay0410 mannequin opened this issue Jul 20, 2020 · 9 comments
Labels
3.8 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@vinay0410
Copy link
Mannequin

vinay0410 mannequin commented Jul 20, 2020

BPO 41344
Nosy @terryjreedy, @pitrou, @applio, @pablogsal, @miss-islington, @vinay0410
PRs
  • bpo-41344: raise ValueError when creating shared memory of size 0 #21556
  • [3.9] bpo-41344: Raise ValueError when creating shared memory of size 0 (GH-21556) #22018
  • [3.8] bpo-41344: Raise ValueError when creating shared memory of size 0 (GH-21556) #22019
  • 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 2020-08-30.19:42:50.614>
    created_at = <Date 2020-07-20.07:35:36.713>
    labels = ['3.8', 'type-bug', 'library', '3.10']
    title = 'SharedMemory crash when size is 0'
    updated_at = <Date 2020-08-30.19:42:50.613>
    user = 'https://github.com/vinay0410'

    bugs.python.org fields:

    activity = <Date 2020-08-30.19:42:50.613>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-08-30.19:42:50.614>
    closer = 'pablogsal'
    components = ['Library (Lib)']
    creation = <Date 2020-07-20.07:35:36.713>
    creator = 'vinay0410'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41344
    keywords = ['patch']
    message_count = 9.0
    messages = ['373990', '374232', '374244', '374246', '374247', '376122', '376125', '376126', '376127']
    nosy_count = 6.0
    nosy_names = ['terry.reedy', 'pitrou', 'davin', 'pablogsal', 'miss-islington', 'vinay0410']
    pr_nums = ['21556', '22018', '22019']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41344'
    versions = ['Python 3.8', 'Python 3.10']

    @vinay0410
    Copy link
    Mannequin Author

    vinay0410 mannequin commented Jul 20, 2020

    On running this: shm = SharedMemory(create=True, size=0)
    I get the following error:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.8/multiprocessing/shared_memory.py", line 111, in __init__
        self._mmap = mmap.mmap(self._fd, size)
    ValueError: cannot mmap an empty file
    Error in sys.excepthook:
    Traceback (most recent call last):
      File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
        from apport.fileutils import likely_packaged, get_recent_crashes
      File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
        from apport.report import Report
      File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
        import apport.fileutils
      File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
        from apport.packaging_impl import impl as packaging
      File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
        import apt
      File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
        import apt_pkg
    ModuleNotFoundError: No module named 'apt_pkg'
    Original exception was:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.8/multiprocessing/shared_memory.py", line 111, in __init__
        self._mmap = mmap.mmap(self._fd, size)
    ValueError: cannot mmap an empty file
    >>> shm = SharedMemory(create=True, size=0)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.8/multiprocessing/shared_memory.py", line 111, in __init__
        self._mmap = mmap.mmap(self._fd, size)
    ValueError: cannot mmap an empty file
    Error in sys.excepthook:
    Traceback (most recent call last):
      File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
        from apport.fileutils import likely_packaged, get_recent_crashes
      File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
        from apport.report import Report
      File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
        import apport.fileutils
      File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
        from apport.packaging_impl import impl as packaging
      File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
        import apt
      File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
        import apt_pkg
    ModuleNotFoundError: No module named 'apt_pkg'
    
    Original exception was:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.8/multiprocessing/shared_memory.py", line 111, in __init__
        self._mmap = mmap.mmap(self._fd, size)
    ValueError: cannot mmap an empty file

    This can be simply resolved by adding a check when size passed is 0, so that a shared memory segment is never created.
    Currently, the following is coded:

    if not size >= 0:
        raise ValueError("'size' must be a positive integer")

    I believe this should be changed to:
    if not size > 0:
    raise ValueError("'size' must be a positive integer")

    As zero is not a positive and integer and is causing problems.

    @vinay0410 vinay0410 mannequin added type-crash A hard crash of the interpreter, possibly with a core dump 3.8 only security fixes stdlib Python modules in the Lib dir labels Jul 20, 2020
    @SilentGhost SilentGhost mannequin added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Jul 20, 2020
    @terryjreedy
    Copy link
    Member

    Python 3.10.0a0 Jul 23 2020, win32 (without patch)

    >>> from multiprocessing import shared_memory
    >>> shm_a = shared_memory.SharedMemory(create=True, size=0)
    Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
        shm_a = shared_memory.SharedMemory(create=True, size=0)
      File "f:\dev\3x\lib\multiprocessing\shared_memory.py", line 129, in __init__
        h_map = _winapi.CreateFileMapping(
    OSError: [WinError 87] The parameter is incorrect: 'wnsm_4ab39616'

    After the patch, I get the value error.

    @terryjreedy terryjreedy added 3.10 only security fixes labels Jul 25, 2020
    @vinay0410
    Copy link
    Mannequin Author

    vinay0410 mannequin commented Jul 25, 2020

    Hi, The patch aims to raise a value error, because before the patch also, ValueError was being raised in case size was negative. That's why I thought it would be correct behaviour if I raise ValueError in case size is 0.

    Do you mean to say, that OSError should be raise in the case size=0.
    Also, I think handling of Windows was fine even before the patch, it was MacOS and Linux which were causing problems.

    @vinay0410
    Copy link
    Mannequin Author

    vinay0410 mannequin commented Jul 25, 2020

    Also, Linux created the shared memory with the passed name if size = 0, but threw error when it was being mapped to the process's virtual memory. Therefore, this scenario should be prevented before actually creating the shared memory segment in cases of Linux.

    @terryjreedy
    Copy link
    Member

    No, I think ValueError on all systems is better. I have no idea what 'wnsm_4ab39616' is about other than something internal to Windows. It does not say what to do to correct the call.

    @pablogsal
    Copy link
    Member

    New changeset 475a5fb by Vinay Sharma in branch 'master':
    bpo-41344: Raise ValueError when creating shared memory of size 0 (GH-21556)
    475a5fb

    @pablogsal
    Copy link
    Member

    New changeset ca55ecb by Miss Islington (bot) in branch '3.9':
    bpo-41344: Raise ValueError when creating shared memory of size 0 (GH-21556) (GH-22018)
    ca55ecb

    @pablogsal
    Copy link
    Member

    New changeset 38e3287 by Miss Islington (bot) in branch '3.8':
    bpo-41344: Raise ValueError when creating shared memory of size 0 (GH-21556) (GH-22019)
    38e3287

    @pablogsal
    Copy link
    Member

    Thanks for the report and the PR, vinay0410!

    @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
    3.8 only security fixes 3.10 only security fixes 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