about summary refs log tree commit diff
path: root/debian/patches/platform-reorder-code-in-_netns_stack_get_impl.patch
blob: d68983af2906e44de4469f2d7c3bf63c10208b08 (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
From: Thomas Haller <thaller@redhat.com>
Date: Mon, 15 Feb 2021 20:45:21 +0100
Subject: platform: reorder code in _netns_stack_get_impl()

We should always register the GArray stack with pthread
for cleanup the thread local storage. Do that first, before
creating the NMPNetns instance at the bottom of the stack.

(cherry picked from commit f9636080ace7d7834083dcb1a3300e08368ee4eb)
---
 shared/nm-platform/nmp-netns.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/shared/nm-platform/nmp-netns.c b/shared/nm-platform/nmp-netns.c
index c7cb617..0828f4a 100644
--- a/shared/nm-platform/nmp-netns.c
+++ b/shared/nm-platform/nmp-netns.c
@@ -158,9 +158,18 @@ _netns_stack_get_impl(void)
     g_array_set_clear_func(s, _netns_stack_clear_cb);
     _netns_stack = s;
 
+    /* register a destructor function to cleanup the array. If we fail
+     * to do so, we will leak NMPNetns instances (and their file descriptor) when the
+     * thread exits. */
+    if (pthread_key_create(&key, (void (*)(void *)) g_array_unref) != 0)
+        _LOGE(NULL, "failure to initialize thread-local storage");
+    else if (pthread_setspecific(key, s) != 0)
+        _LOGE(NULL, "failure to set thread-local storage");
+
     /* at the bottom of the stack we must try to create a netns instance
      * that we never pop. It's the base to which we need to return. */
     netns = _netns_new(&error);
+
     if (!netns) {
         _LOGE(NULL, "failed to create initial netns: %s", error->message);
         return s;
@@ -169,14 +178,6 @@ _netns_stack_get_impl(void)
     /* we leak this instance inside the stack. */
     _stack_push(s, netns, _CLONE_NS_ALL);
 
-    /* finally, register a destructor function to cleanup the array. If we fail
-     * to do so, we will leak NMPNetns instances (and their file descriptor) when the
-     * thread exits. */
-    if (pthread_key_create(&key, (void (*)(void *)) g_array_unref) != 0)
-        _LOGE(NULL, "failure to initialize thread-local storage");
-    else if (pthread_setspecific(key, s) != 0)
-        _LOGE(NULL, "failure to set thread-local storage");
-
     return s;
 }