summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/opkg/opkg/0001-opkg_lock-retry-if-we-fail-to-get-lock.patch
blob: 863667fe4004962d911203de8d2dadee8f53a3f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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