summaryrefslogtreecommitdiffstats
path: root/pym/portage/locks.py
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2012-03-27 11:28:12 -0700
committerZac Medico <zmedico@gentoo.org>2012-03-27 11:28:12 -0700
commit144c23efbb4e9565debad03c13c5bcab833a8336 (patch)
treeab9093b86335c68e8f425ceb56249b308bceb93a /pym/portage/locks.py
parentb344222a9a4dfbd811e196bd755ccbec511d0721 (diff)
downloadportage-144c23efbb4e9565debad03c13c5bcab833a8336.tar.gz
portage-144c23efbb4e9565debad03c13c5bcab833a8336.tar.bz2
portage-144c23efbb4e9565debad03c13c5bcab833a8336.zip
Close fewer file descriptors for fork / no exec.
This will fix bug #374335.
Diffstat (limited to 'pym/portage/locks.py')
-rw-r--r--pym/portage/locks.py23
1 files changed, 22 insertions, 1 deletions
diff --git a/pym/portage/locks.py b/pym/portage/locks.py
index 9fee5ae5d..87fbe0726 100644
--- a/pym/portage/locks.py
+++ b/pym/portage/locks.py
@@ -1,5 +1,5 @@
# portage: Lock management code
-# Copyright 2004-2011 Gentoo Foundation
+# Copyright 2004-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
__all__ = ["lockdir", "unlockdir", "lockfile", "unlockfile", \
@@ -36,6 +36,20 @@ if platform.python_implementation() == 'PyPy':
# so that it doesn't interfere with the status display.
_quiet = False
+
+_open_fds = set()
+
+def _close_fds():
+ """
+ This is intended to be called after a fork, in order to close file
+ descriptors for locks held by the parent process. This can be called
+ safely after a fork without exec, unlike the _setup_pipes close_fds
+ behavior.
+ .
+ """
+ while _open_fds:
+ os.close(_open_fds.pop())
+
def lockdir(mydir, flags=0):
return lockfile(mydir, wantnewlockfile=1, flags=flags)
def unlockdir(mylock):
@@ -193,6 +207,9 @@ def lockfile(mypath, wantnewlockfile=0, unlinkfile=0,
mypath, wantnewlockfile=wantnewlockfile, unlinkfile=unlinkfile,
waiting_msg=waiting_msg, flags=flags)
+ if myfd != HARDLINK_FD:
+ _open_fds.add(myfd)
+
writemsg(str((lockfilename,myfd,unlinkfile))+"\n",1)
return (lockfilename,myfd,unlinkfile,locking_method)
@@ -233,6 +250,7 @@ def unlockfile(mytuple):
writemsg(_("lockfile does not exist '%s'\n") % lockfilename,1)
if myfd is not None:
os.close(myfd)
+ _open_fds.remove(myfd)
return False
try:
@@ -243,6 +261,7 @@ def unlockfile(mytuple):
except OSError:
if isinstance(lockfilename, basestring):
os.close(myfd)
+ _open_fds.remove(myfd)
raise IOError(_("Failed to unlock file '%s'\n") % lockfilename)
try:
@@ -264,6 +283,7 @@ def unlockfile(mytuple):
else:
writemsg(_("lockfile does not exist '%s'\n") % lockfilename, 1)
os.close(myfd)
+ _open_fds.remove(myfd)
return False
except SystemExit:
raise
@@ -276,6 +296,7 @@ def unlockfile(mytuple):
# open fd closed automatically on them.
if isinstance(lockfilename, basestring):
os.close(myfd)
+ _open_fds.remove(myfd)
return True