Fix call home client thread cleanup
This commit by @thelsper fixes a race condition in call home client creation
and deletion. If a call home client is deleted and then immediately re-created
with the same client name, the old client thread will not notice that the
old client was destroyed, and will attempt to service the new client,
while the newly created client thread for the new client also attempts to
service the new client! This results in a proliferation of threads and can also
result in deadlock.
To fix this, we added a unique client ID to the client information. When
a client thread retrieves its client by name, it will now also check the client
ID against the cached value of the client ID for the client it was previously
servicing. If the client ID changes, this means that the client was destroyed,
and the thread will clean itself up.
While we were here making these changes we also changed the session ID to be
a true atomic, and removed the spin lock that was protecting it. (The atomic
operations library stdatomic.h was added in C11, which was why we also
appended `-std=gnu11` to the `CFLAGS`. Both sysrepo and netopeer2 are already
using C11.)
diff --git a/src/session_p.h b/src/session_p.h
index 2993fe1..af2291c 100644
--- a/src/session_p.h
+++ b/src/session_p.h
@@ -18,6 +18,7 @@
#include <stdint.h>
#include <pthread.h>
+#include <stdatomic.h>
#include <libyang/libyang.h>
@@ -268,14 +269,15 @@
} conn;
NC_CH_START_WITH start_with;
uint8_t max_attempts;
+ uint32_t id;
pthread_mutex_t lock;
} *ch_clients;
uint16_t ch_client_count;
pthread_rwlock_t ch_client_lock;
- /* ACCESS locked with sid_lock */
- uint32_t new_session_id;
- pthread_spinlock_t sid_lock;
+ /* Atomic IDs */
+ atomic_uint_fast32_t new_session_id;
+ atomic_uint_fast32_t new_client_id;
};
/**