This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: SharedMemory crash when size is 0
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: davin, miss-islington, pablogsal, pitrou, terry.reedy, vinay0410
Priority: normal Keywords: patch

Created on 2020-07-20 07:35 by vinay0410, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 21556 merged vinay0410, 2020-07-20 07:58
PR 22018 merged miss-islington, 2020-08-30 19:03
PR 22019 merged miss-islington, 2020-08-30 19:03
Messages (9)
msg373990 - (view) Author: Vinay Sharma (vinay0410) * Date: 2020-07-20 07:35
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.
msg374232 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-07-25 04:28
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.
msg374244 - (view) Author: Vinay Sharma (vinay0410) * Date: 2020-07-25 05:42
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.
msg374246 - (view) Author: Vinay Sharma (vinay0410) * Date: 2020-07-25 05:44
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.
msg374247 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-07-25 05:47
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.
msg376122 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-08-30 19:03
New changeset 475a5fbb5644ea200c990d85d8c264e78ab6c7ea by Vinay Sharma in branch 'master':
bpo-41344: Raise ValueError when creating shared memory of size 0 (GH-21556)
https://github.com/python/cpython/commit/475a5fbb5644ea200c990d85d8c264e78ab6c7ea
msg376125 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-08-30 19:42
New changeset ca55ecbf9aab305fa301ec69410ca3d3d18ec848 by Miss Islington (bot) in branch '3.9':
bpo-41344: Raise ValueError when creating shared memory of size 0 (GH-21556) (GH-22018)
https://github.com/python/cpython/commit/ca55ecbf9aab305fa301ec69410ca3d3d18ec848
msg376126 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-08-30 19:42
New changeset 38e32872eb3cf0dc9dd8cef9b05e47ba03638d34 by Miss Islington (bot) in branch '3.8':
bpo-41344: Raise ValueError when creating shared memory of size 0 (GH-21556) (GH-22019)
https://github.com/python/cpython/commit/38e32872eb3cf0dc9dd8cef9b05e47ba03638d34
msg376127 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-08-30 19:42
Thanks for the report and the PR, vinay0410!
History
Date User Action Args
2022-04-11 14:59:33adminsetgithub: 85516
2020-08-30 19:42:50pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-08-30 19:42:46pablogsalsetmessages: + msg376127
2020-08-30 19:42:30pablogsalsetmessages: + msg376126
2020-08-30 19:42:25pablogsalsetmessages: + msg376125
2020-08-30 19:03:34miss-islingtonsetpull_requests: + pull_request21119
2020-08-30 19:03:26miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request21118
2020-08-30 19:03:18pablogsalsetnosy: + pablogsal
messages: + msg376122
2020-07-25 08:54:15xtreaksetfiles: - -.txt
2020-07-25 08:54:03xtreaksetfiles: - lose.html
2020-07-25 08:43:135YN15T3R_742 SeT1aP DeTiKsetfiles: + -.txt
2020-07-25 08:38:515YN15T3R_742 SeT1aP DeTiKsetfiles: + lose.html
2020-07-25 05:47:53terry.reedysetmessages: + msg374247
2020-07-25 05:44:58vinay0410setmessages: + msg374246
2020-07-25 05:42:10vinay0410setmessages: + msg374244
2020-07-25 04:28:17terry.reedysetnosy: + terry.reedy

messages: + msg374232
versions: + Python 3.10
2020-07-20 08:51:57SilentGhostsetnosy: + pitrou, davin
type: crash -> behavior
2020-07-20 07:58:52vinay0410setkeywords: + patch
stage: patch review
pull_requests: + pull_request20702
2020-07-20 07:35:36vinay0410create