[New-bugs-announce] [issue20057] wrong behavior with fork and mmap
Brett Tiplitz
report at bugs.python.org
Mon Dec 23 22:05:19 CET 2013
New submission from Brett Tiplitz:
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
# write a simple example file
with open("hello.txt", "wb") as f:
f.write(bytes("Hello Python!\n", 'UTF-8'))
with open("hello.txt", "r+b") as f:
# memory-map the file, size 0 means whole file
os.system("/bin/ls -l /proc/"+str(os.getpid())+"/fd")
mm = mmap.mmap(f.fileno(), 0)
os.system("/bin/ls -l /proc/"+str(os.getpid())+"/fd")
os.system("/bin/ls -l /proc/self/fd")
# read content via standard file methods
t1 = mm.readline() # used to print out
# prints "Hello Python!"
# read content via slice notation
t2=mm[:5]
# print mm[:5] # prints "Hello"
# update content using slice notation;
# note that new content must have same size
mm[6:] = bytes(" world!\n", 'UTF-8')
# ... and read again using standard file methods
mm.seek(0)
t3=mm.readline()
# print mm.readline() # prints "Hello world!"
# close the map
mm.close()
~
----------
components: Library (Lib)
messages: 206871
nosy: btiplitz
priority: normal
severity: normal
status: open
title: wrong behavior with fork and mmap
type: behavior
versions: Python 2.7
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20057>
_______________________________________
More information about the New-bugs-announce
mailing list