diff mbox series

opkg: retry opkg_lock

Message ID 20240321164744.3007283-1-tim.orling@konsulko.com
State New
Headers show
Series opkg: retry opkg_lock | expand

Commit Message

Tim Orling March 21, 2024, 4:47 p.m. UTC
When systemd is enabled, we can get into a race condition
between run-postinsts and systemctl restarting services.

If we fail to create the lock file (or fd), then
sleep 10 seconds and retry up to 5 times.

[YOCTO #15428]

Patch submitted upstream to opkg:
https://lists.yoctoproject.org/g/opkg/message/60

Signed-off-by: Tim Orling <tim.orling@konsulko.com>
---
 ...kg_lock-retry-if-we-fail-to-get-lock.patch | 59 +++++++++++++++++++
 meta/recipes-devtools/opkg/opkg_0.6.3.bb      |  1 +
 2 files changed, 60 insertions(+)
 create mode 100644 meta/recipes-devtools/opkg/opkg/0001-opkg_lock-retry-if-we-fail-to-get-lock.patch
diff mbox series

Patch

diff --git a/meta/recipes-devtools/opkg/opkg/0001-opkg_lock-retry-if-we-fail-to-get-lock.patch b/meta/recipes-devtools/opkg/opkg/0001-opkg_lock-retry-if-we-fail-to-get-lock.patch
new file mode 100644
index 00000000000..863667fe400
--- /dev/null
+++ b/meta/recipes-devtools/opkg/opkg/0001-opkg_lock-retry-if-we-fail-to-get-lock.patch
@@ -0,0 +1,59 @@ 
+From cdc7d347f41a7095ba0e29515951a1394c632479 Mon Sep 17 00:00:00 2001
+From: Tim Orling <tim.orling@konsulko.com>
+Date: Wed, 20 Mar 2024 23:00:31 -0700
+Subject: [opkg][PATCH] opkg_lock: retry if we fail to get lock
+To: opkg@lists.yoctoproject.org
+
+When systemd is enabled, we can get into a race condition
+between run-postinsts and systemctl restarting services.
+
+If we fail to create the lock file (or fd), then
+sleep 10 seconds and retry up to 5 times.
+
+[YOCTO #15428]
+
+Upstream-Status: Submitted [https://lists.yoctoproject.org/g/opkg/message/60]
+
+Signed-off-by: Tim Orling <tim.orling@konsulko.com>
+---
+ libopkg/opkg_conf.c | 22 ++++++++++++++++------
+ 1 file changed, 16 insertions(+), 6 deletions(-)
+
+diff --git a/libopkg/opkg_conf.c b/libopkg/opkg_conf.c
+index 4dc777b..03bb40a 100644
+--- a/libopkg/opkg_conf.c
++++ b/libopkg/opkg_conf.c
+@@ -660,14 +660,24 @@ int opkg_lock()
+         return -1;
+     }
+ 
+-    r = lockf(lock_fd, F_TLOCK, (off_t) 0);
++    int retry = 5;
++    do {
++        r = lockf(lock_fd, F_TLOCK, (off_t) 0);
++        if (r == -1) {
++            opkg_perror(INFO, "Could not lock %s, retry %d", opkg_config->lock_file, retry);
++            r = close(lock_fd);
++	    if (r == -1)
++                opkg_perror(ERROR, "Couldn't close descriptor %d (%s)", lock_fd,
++                            opkg_config->lock_file);
++	    retry--;
++            sleep(10);
++        }
++    }
++    while (retry > 0 && r == -1);
++
+     if (r == -1) {
+         opkg_perror(ERROR, "Could not lock %s", opkg_config->lock_file);
+-        r = close(lock_fd);
+-        if (r == -1)
+-            opkg_perror(ERROR, "Couldn't close descriptor %d (%s)", lock_fd,
+-                        opkg_config->lock_file);
+-        lock_fd = -1;
++	lock_fd = -1;
+         return -1;
+     }
+ 
+-- 
+2.34.1
+
diff --git a/meta/recipes-devtools/opkg/opkg_0.6.3.bb b/meta/recipes-devtools/opkg/opkg_0.6.3.bb
index 9592ffc5d6d..b4c7c114bcb 100644
--- a/meta/recipes-devtools/opkg/opkg_0.6.3.bb
+++ b/meta/recipes-devtools/opkg/opkg_0.6.3.bb
@@ -16,6 +16,7 @@  SRC_URI = "http://downloads.yoctoproject.org/releases/${BPN}/${BPN}-${PV}.tar.gz
            file://opkg.conf \
            file://0001-opkg_conf-create-opkg.lock-in-run-instead-of-var-run.patch \
            file://0001-libopkg-Use-libgen.h-to-provide-basename-API.patch \
+	   file://0001-opkg_lock-retry-if-we-fail-to-get-lock.patch \
            file://run-ptest \
            "