summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichał Górny <mgorny@gentoo.org>2017-11-27 00:00:13 +0100
committerMichał Górny <mgorny@gentoo.org>2017-11-27 00:00:13 +0100
commitad90422193c041a95ce48f9e334e5a3fd524a42e (patch)
tree41eabd74895441e6cb180b31c006827bd33f7144
parentdf47175a74674c42171deb0aef8175081e0e8f5b (diff)
downloadgemato-ad90422193c041a95ce48f9e334e5a3fd524a42e.tar.gz
openpgp: Fix race condition error handling on cleanup
Provide a proper error handler to rmtree() method in OpenPGP cleanup handler. Ignore ENOENT when removing files to prevent race conditions between the rmtree() function and gpg-agent cleaning up its own sockets.
-rw-r--r--gemato/openpgp.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/gemato/openpgp.py b/gemato/openpgp.py
index 9078b59..ab55791 100644
--- a/gemato/openpgp.py
+++ b/gemato/openpgp.py
@@ -53,12 +53,20 @@ class OpenPGPEnvironment(object):
if self._home is not None:
self.close()
+ @staticmethod
+ def _rmtree_error_handler(func, path, exc_info):
+ # ignore ENOENT -- it probably means a race condition between
+ # us and gpg-agent cleaning up after itself
+ if (not isinstance(exc_info[1], OSError)
+ or exc_info[1].errno != errno.ENOENT):
+ raise exc_info[1]
+
def close(self):
if self._home is not None:
# terminate the agent spawned by the process
subprocess.Popen(['gpgconf', '--kill', 'all'],
env={'GNUPGHOME': self._home}).wait()
- shutil.rmtree(self._home)
+ shutil.rmtree(self._home, onerror=self._rmtree_error_handler)
self._home = None
def import_key(self, keyfile):