Done
Description
| BPO | 20057 |
|---|---|
| Nosy | @vstinner, @bitdancer |
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 = None
created_at = <Date 2013-12-23.21:05:18.990>
labels = ['type-bug', 'library']
title = 'wrong behavior with fork and mmap'
updated_at = <Date 2013-12-23.22:10:48.342>
user = 'https://bugs.python.org/btiplitz'bugs.python.org fields:
activity = <Date 2013-12-23.22:10:48.342>
actor = 'btiplitz'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2013-12-23.21:05:18.990>
creator = 'btiplitz'
dependencies = []
files = []
hgrepos = []
issue_num = 20057
keywords = []
message_count = 5.0
messages = ['206871', '206872', '206876', '206877', '206878']
nosy_count = 3.0
nosy_names = ['vstinner', 'r.david.murray', 'btiplitz']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue20057'
versions = ['Python 2.7']
Activity
btiplitz commentedon Dec 23, 2013
When running the example mmap library (with a slight modification, plus I did not handle all the changes for the 3.3 string handling as the example posted does not work with 3.x)
When looking at the subprocess, the spawned process will have all the mmap'd file descriptors open. The spawned process has the responsibility of closing any FD's that are in use. However, since the shared memory segment get's closed and the program has no knowledge of private FD's, the mmap's private FD becomes a leak in the FD table. It seems python should set the close-on-exec attribute on the dup'd FD that it maintains. Examples of fixing this issue are found on http://stackoverflow.com/questions/1643304/how-to-set-close-on-exec-by-default
import mmap,os
bitdancer commentedon Dec 23, 2013
It seems very likely that this is addressed by PEP-446. Since that is not a behavior change that can be backported, I think this issue should probably be closed as out of date.
btiplitz commentedon Dec 23, 2013
Changing the code to
subprocess.call(["/bin/ls", "-l", "/proc/self/fd"])
and running this on Python 3.3 does show this as being resolved by the broader fix implemented in PEP-446. It does seem bad that the os.system call remains in place with bad behavior as I know it's widely used.
vstinner commentedon Dec 23, 2013
This issue is not specific to mmap. Many other functions and libraries may
use private inheritable file descriptors. Python 3.4 does not fix the issue
for third party libraries.
os.system() must be avoided, use subprocess.call() instead. It avoids an
useless shell process and closes all fds by default.
Is it a documentation issue?
btiplitz commentedon Dec 23, 2013
Man page currently says as follows: (this does not says it's deprecated or that files have to be closed on exec)... So I'd think some more comments would help. And as mentioned, which a user can close his own fd's, the mmap call creates a special problem since the user can't work around the issue cleanly though fixed in the subprocess calls.
maurycy commentedon Dec 5, 2026
PEP 446 (3.4) addressed the
mmapcase.The duplicate descriptor is inherited across
fork()and closed atexec():cc @StanFromIreland
gh-64256: Docs: Link `os.system()` to file descriptor inheritance (#1…
[3.14] gh-64256: Docs: Link `os.system()` to file descriptor inherita…
[3.15] gh-64256: Docs: Link `os.system()` to file descriptor inherita…