aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2024-02-09 14:12:02 -0800
committerZac Medico <zmedico@gentoo.org>2024-02-09 22:08:59 -0800
commit03be12ec5f46629fa928e5fcd45d3fe6745d5d0a (patch)
treee6d6c506c5d9b26a40facaa64524b0773d1c1075 /lib/portage
parentGPG: Proactively stop to avoid "GPG keepalive failed" error in pypy ci jobs (diff)
downloadportage-03be12ec5f46629fa928e5fcd45d3fe6745d5d0a.tar.gz
portage-03be12ec5f46629fa928e5fcd45d3fe6745d5d0a.tar.bz2
portage-03be12ec5f46629fa928e5fcd45d3fe6745d5d0a.zip
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@gentoo.org>
Diffstat (limited to 'lib/portage')
-rw-r--r--lib/portage/gpg.py14
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/portage/gpg.py b/lib/portage/gpg.py
index 306787224..d8a4cfcfc 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")