diff mbox series

[v2,1/2] lib/oe/gpg_sign.py: serialize signing instead of passing agent options

Message ID 20261202125343.310035-2-adarsh.jagadish.kamini@est.tech
State Under Review
Headers show
Series gpg_sign: avoid unsupported gpg-agent argument passing | expand

Commit Message

Adarsh Jagadish Kamini Dec. 2, 2026, 12:53 p.m. UTC
The "--agent-program=gpg-agent|--auto-expand-secmem" syntax is what the
gpg manual calls a regression test suite hack, and it has no effect on
an agent that is already running, in which case parallel signing fails
with "Cannot allocate memory". Instead, take a lock in TMPDIR around
signing, which serializes OE signing operations that share a TMPDIR,
including sstate signing. Set GPG_SIGN_SERIALIZE = "0" to opt out when
the agent runs with auto-expand-secmem.

In a signed core-image-minimal build with an already running agent,
138 of 141 do_package_write_rpm tasks failed without the lock and none
with it, with no clear slowdown observed. The cost for sstate signing
was not measured. Tested with oe-selftest -r signing, not with TMPDIR
on a network file system.

[YOCTO #16480]

AI-Generated: Uses Kiro (claude-opus-5.5)
Signed-off-by: Adarsh Jagadish Kamini <adarsh.jagadish.kamini@est.tech>
---
 meta/classes/sign_ipk.bbclass          |  3 +++
 meta/classes/sign_package_feed.bbclass |  3 +++
 meta/classes/sign_rpm.bbclass          |  3 +++
 meta/lib/oe/gpg_sign.py                | 22 ++++++++++++----------
 4 files changed, 21 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/meta/classes/sign_ipk.bbclass b/meta/classes/sign_ipk.bbclass
index de11b9f32f..9782bc66c2 100644
--- a/meta/classes/sign_ipk.bbclass
+++ b/meta/classes/sign_ipk.bbclass
@@ -24,6 +24,9 @@ 
 #           signing.
 # GPG_PATH
 #           Optional variable for specifying the gnupg "home" directory:
+# GPG_SIGN_SERIALIZE
+#           Optional variable; "1" (default) serializes signing operations
+#           that share a TMPDIR, "0" allows parallel signing.
 #
 
 IPK_SIGN_PACKAGES = '1'
diff --git a/meta/classes/sign_package_feed.bbclass b/meta/classes/sign_package_feed.bbclass
index d33e850dbc..2077b00668 100644
--- a/meta/classes/sign_package_feed.bbclass
+++ b/meta/classes/sign_package_feed.bbclass
@@ -27,6 +27,9 @@ 
 #           signing.
 # GPG_PATH
 #           Optional variable for specifying the gnupg "home" directory:
+# GPG_SIGN_SERIALIZE
+#           Optional variable; "1" (default) serializes signing operations
+#           that share a TMPDIR, "0" allows parallel signing.
 #
 
 PACKAGE_FEED_SIGN = '1'
diff --git a/meta/classes/sign_rpm.bbclass b/meta/classes/sign_rpm.bbclass
index 62a5e72475..ac124b8096 100644
--- a/meta/classes/sign_rpm.bbclass
+++ b/meta/classes/sign_rpm.bbclass
@@ -30,6 +30,9 @@ 
 #           invocation
 # GPG_PATH
 #           Optional variable for specifying the gnupg "home" directory:
+# GPG_SIGN_SERIALIZE
+#           Optional variable; "1" (default) serializes signing operations
+#           that share a TMPDIR, "0" allows parallel signing.
 
 RPM_SIGN_PACKAGES = '1'
 RPM_SIGN_FILES ?= '0'
diff --git a/meta/lib/oe/gpg_sign.py b/meta/lib/oe/gpg_sign.py
index ede6186c84..c2debfa968 100644
--- a/meta/lib/oe/gpg_sign.py
+++ b/meta/lib/oe/gpg_sign.py
@@ -18,13 +18,13 @@  class LocalSigner(object):
         self.gpg_bin = d.getVar('GPG_BIN') or \
                   bb.utils.which(os.getenv('PATH'), 'gpg')
         self.gpg_cmd = [self.gpg_bin]
-        self.gpg_agent_bin = bb.utils.which(os.getenv('PATH'), "gpg-agent")
-        # Without this we see "Cannot allocate memory" errors when running processes in parallel
-        # It needs to be set for any gpg command since any agent launched can stick around in memory
-        # and this parameter must be set.
-        if self.gpg_agent_bin:
-            self.gpg_cmd += ["--agent-program=%s|--auto-expand-secmem" % (self.gpg_agent_bin)]
         self.gpg_path = d.getVar('GPG_PATH')
+        # Concurrent signing can exhaust gpg-agent's secure memory unless it
+        # runs with auto-expand-secmem, so by default serialize signing
+        # operations that share a TMPDIR. (AI-generated: Kiro)
+        self.sign_lock = None
+        if bb.utils.to_boolean(d.getVar('GPG_SIGN_SERIALIZE'), True):
+            self.sign_lock = os.path.join(d.getVar('TMPDIR'), 'gpg-sign.lock')
         self.rpm_bin = bb.utils.which(os.getenv('PATH'), "rpmsign")
         self.gpg_version = self.get_gpg_version()
 
@@ -43,7 +43,7 @@  class LocalSigner(object):
         """Sign RPM files"""
 
         cmd = self.rpm_bin + " --addsign --define '_gpg_name %s'  " % keyid
-        gpg_args = '--no-permission-warning --batch --passphrase=%s --agent-program=%s|--auto-expand-secmem' % (passphrase, self.gpg_agent_bin)
+        gpg_args = '--no-permission-warning --batch --passphrase=%s' % passphrase
         if self.gpg_version > (2,1,):
             gpg_args += ' --pinentry-mode=loopback'
         cmd += "--define '_gpg_sign_cmd_extra_args %s' " % gpg_args
@@ -59,7 +59,8 @@  class LocalSigner(object):
 
         # Sign in chunks
         for i in range(0, len(files), sign_chunk):
-            subprocess.check_output(shlex.split(cmd + ' '.join(files[i:i+sign_chunk])), stderr=subprocess.STDOUT)
+            with bb.utils.fileslocked([self.sign_lock]):
+                subprocess.check_output(shlex.split(cmd + ' '.join(files[i:i+sign_chunk])), stderr=subprocess.STDOUT)
 
     def detach_sign(self, input_file, keyid, passphrase_file, passphrase=None, armor=True, output_suffix=None, use_sha256=False):
         """Create a detached signature of a file"""
@@ -96,8 +97,9 @@  class LocalSigner(object):
 
                 cmd += [input_file]
 
-                job = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
-                (_, stderr) = job.communicate(passphrase.encode("utf-8"))
+                with bb.utils.fileslocked([self.sign_lock]):
+                    job = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
+                    (_, stderr) = job.communicate(passphrase.encode("utf-8"))
 
                 if job.returncode:
                     bb.fatal("GPG exited with code %d: %s" % (job.returncode, stderr.decode("utf-8")))