Skip to content

Commit

Permalink
GPG: Use threading.Event for thread safety
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
zmedico committed Feb 10, 2024
1 parent d7115d1 commit 03be12e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/portage/gpg.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,6 +23,7 @@ def __init__(self, settings):
"""
self.settings = settings
self.thread = None
self._terminated = None
self.GPG_signing_base_command = self.settings.get(
"BINPKG_GPG_SIGNING_BASE_COMMAND"
)
Expand Down Expand Up @@ -73,6 +73,7 @@ def unlock(self):
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()

Expand All @@ -81,16 +82,17 @@ def stop(self):
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:
Expand All @@ -102,5 +104,5 @@ def gpg_keepalive(self):
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")

0 comments on commit 03be12e

Please sign in to comment.