[ python-Bugs-1423153 ] mmap module leaks file descriptors on UNIX

SourceForge.net noreply at sourceforge.net
Fri Feb 3 15:51:30 CET 2006


Bugs item #1423153, was opened at 2006-02-02 18:25
Message generated for change (Comment added) made by kdart
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1423153&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Extension Modules
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Fazal Majid (majid)
Assigned to: Nobody/Anonymous (nobody)
Summary: mmap module leaks file descriptors on UNIX

Initial Comment:
On UNIX, mmapmodule.c makes a call to dup(2) prior to
mapping the file descriptor into memory (oddly enough,
it doesn't map the new fd resulting from dup(), but the
old one).

The close method does not release this file descriptor,
however, leading to a leak. If mmap is used repeatedly,
the process will eventually run out of file
descriptors, as can easily be shown with this test case:

import sys, os, mmap

for i in xrange(2000):
  f = os.open('/dev/zero', os.O_RDWR)
  m = mmap.mmap(f, 10000)
  os.close(f)
  a = m[0:100]
  m[100:200] = 'F' * 100
  m.close()


----------------------------------------------------------------------

Comment By: Keith Dart (kdart)
Date: 2006-02-03 06:51

Message:
Logged In: YES 
user_id=16527

I would also like to add that the following no longer works
in Python 2.4:

_buf = mmap.mmap(-1, 8192,
flags=mmap.MAP_PRIVATE|mmap.MAP_ANONYMOUS,
prot=mmap.PROT_READ|mmap.PROT_WRITE )

This is because the fd value of -1 raises an exception
because it is being dup-ed inside the mmap module. But the
fd is ignored in anonymous mmaps and does not need to be
dup-ed. This worked in older Python (2.3). Is the dup() call
really necessary?


----------------------------------------------------------------------

Comment By: Fazal Majid (majid)
Date: 2006-02-02 18:44

Message:
Logged In: YES 
user_id=110477

The following patch closes the file descriptor when the mmap
object's close method is called, and seems to resolve this
problem.

----------------------------------------------------------------------

Comment By: Fazal Majid (majid)
Date: 2006-02-02 18:27

Message:
Logged In: YES 
user_id=110477

SourceForge munged the formatting of the test case, so I am
attaching a copy as well.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1423153&group_id=5470


More information about the Python-bugs-list mailing list