commit:     03be12ec5f46629fa928e5fcd45d3fe6745d5d0a
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Fri Feb  9 22:12:02 2024 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Sat Feb 10 06:08:59 2024 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=03be12ec

GPG: Use threading.Event for thread safety

Use threading.Event for thread safety during GPG stop, and use the
wait method to improve responsiveness for stop requests.

Bug: https://bugs.gentoo.org/924192
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>

 lib/portage/gpg.py | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/lib/portage/gpg.py b/lib/portage/gpg.py
index 3067872244..d8a4cfcfc4 100644
--- a/lib/portage/gpg.py
+++ b/lib/portage/gpg.py
@@ -1,10 +1,9 @@
-# Copyright 2001-2020 Gentoo Authors
+# Copyright 2001-2024 Gentoo Authors
 # Distributed under the terms of the GNU General Public License v2
 
 import subprocess
 import sys
 import threading
-import time
 
 from portage import os
 from portage.const import SUPPORTED_GENTOO_BINPKG_FORMATS
@@ -24,6 +23,7 @@ class GPG:
         """
         self.settings = settings
         self.thread = None
+        self._terminated = None
         self.GPG_signing_base_command = self.settings.get(
             "BINPKG_GPG_SIGNING_BASE_COMMAND"
         )
@@ -73,6 +73,7 @@ class GPG:
                 self.GPG_unlock_command = shlex_split(
                     varexpand(self.GPG_unlock_command, mydict=self.settings)
                 )
+                self._terminated = threading.Event()
                 self.thread = threading.Thread(target=self.gpg_keepalive, 
daemon=True)
                 self.thread.start()
 
@@ -81,16 +82,17 @@ class GPG:
         Stop keepalive thread.
         """
         if self.thread is not None:
-            self.keepalive = False
+            self._terminated.set()
 
     def gpg_keepalive(self):
         """
         Call GPG unlock command every 5 mins to avoid the passphrase expired.
         """
         count = 0
-        while self.keepalive:
+        while not self._terminated.is_set():
             if count < 5:
-                time.sleep(60)
+                if self._terminated.wait(60):
+                    break
                 count += 1
                 continue
             else:
@@ -102,5 +104,5 @@ class GPG:
                 stdout=subprocess.DEVNULL,
                 stderr=subprocess.STDOUT,
             )
-            if proc.wait() != os.EX_OK:
+            if proc.wait() != os.EX_OK and not self._terminated.is_set():
                 raise GPGException("GPG keepalive failed")

Reply via email to