From ad90422193c041a95ce48f9e334e5a3fd524a42e Mon Sep 17 00:00:00 2001 From: Michał Górny Date: Mon, 27 Nov 2017 00:00:13 +0100 Subject: 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. --- gemato/openpgp.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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): -- cgit v1.2.3