From 912fd592c8b97a309ebf9fb261abe52ea1a10c2d Mon Sep 17 00:00:00 2001 From: Michał Górny Date: Mon, 27 Nov 2017 15:06:59 +0100 Subject: openpgp: Terminate gpg-agent only if gpg was actually called --- gemato/openpgp.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/gemato/openpgp.py b/gemato/openpgp.py index 3408087..6df28de 100644 --- a/gemato/openpgp.py +++ b/gemato/openpgp.py @@ -28,6 +28,9 @@ def _spawn_gpg(options, env_instance, stdin): else: raise + if env_instance is not None: + env_instance._set_started() + out, err = p.communicate(stdin) return (p.wait(), out, err) @@ -41,10 +44,11 @@ class OpenPGPEnvironment(object): or use as a context manager (via 'with'). """ - __slots__ = ['_home'] + __slots__ = ['_home', '_started'] def __init__(self): self._home = tempfile.mkdtemp() + self._started = False def __enter__(self): return self @@ -53,6 +57,9 @@ class OpenPGPEnvironment(object): if self._home is not None: self.close() + def _set_started(self): + self._started = True + @staticmethod def _rmtree_error_handler(func, path, exc_info): # ignore ENOENT -- it probably means a race condition between @@ -63,9 +70,10 @@ class OpenPGPEnvironment(object): 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() + if self._started: + # terminate the agent spawned by the process + subprocess.Popen(['gpgconf', '--kill', 'all'], + env={'GNUPGHOME': self._home}).wait() shutil.rmtree(self._home, onerror=self._rmtree_error_handler) self._home = None -- cgit v1.2.3