Skip to content

Commit abc6f23

Browse files
committed
session server BUGFIX do not join ch threads
Made CH threads detached to avoid a deadlock where thread A holds config wr lock and tries to join thread B, thread B wakes up and tries to lock config read lock.
1 parent 3f50870 commit abc6f23

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

src/server_config.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -872,22 +872,18 @@ nc_server_config_ch_del_endpt(struct nc_ch_client *ch_client, struct nc_ch_endpt
872872
static void
873873
nc_server_config_destroy_ch_client(struct nc_ch_client *ch_client)
874874
{
875-
pthread_t tid;
876875
uint16_t i, ch_endpt_count;
877876

878877
/* CH COND LOCK */
879878
pthread_mutex_lock(&ch_client->thread_data->cond_lock);
880879
if (ch_client->thread_data->thread_running) {
880+
/* notify the thread to exit,
881+
* it will stop once the current thread is finished applying the configuration
882+
* and releases the config lock */
881883
ch_client->thread_data->thread_running = 0;
882884
pthread_cond_signal(&ch_client->thread_data->cond);
883885
/* CH COND UNLOCK */
884886
pthread_mutex_unlock(&ch_client->thread_data->cond_lock);
885-
886-
/* get tid */
887-
tid = ch_client->tid;
888-
889-
/* wait for the thread to terminate */
890-
pthread_join(tid, NULL);
891887
} else {
892888
/* CH COND UNLOCK */
893889
pthread_mutex_unlock(&ch_client->thread_data->cond_lock);

src/session_p.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,6 @@ struct nc_server_opts {
557557

558558
struct nc_ch_client {
559559
char *name; /**< Identifier of the Call Home client. */
560-
pthread_t tid; /**< Call Home client's thread ID */
561560
struct nc_ch_client_thread_arg *thread_data; /**< Data of the Call Home client's thread */
562561

563562
struct nc_ch_endpt {

src/session_server.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,11 +3249,25 @@ nc_connect_ch_client_dispatch(const char *client_name, nc_server_ch_session_acqu
32493249
pthread_t tid;
32503250
struct nc_ch_client_thread_arg *arg = NULL;
32513251
struct nc_ch_client *ch_client;
3252+
pthread_attr_t attr;
32523253

32533254
NC_CHECK_ARG_RET(NULL, client_name, acquire_ctx_cb, release_ctx_cb, new_session_cb, -1);
32543255

32553256
NC_CHECK_SRV_INIT_RET(-1);
32563257

3258+
/* init pthread attribute */
3259+
if ((r = pthread_attr_init(&attr))) {
3260+
ERR(NULL, "Initializing pthread attributes failed (%s).", strerror(r));
3261+
return -1;
3262+
}
3263+
3264+
/* set the thread to be detached */
3265+
if ((r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) {
3266+
ERR(NULL, "Setting pthread attributes to detached failed (%s).", strerror(r));
3267+
rc = -1;
3268+
goto cleanup;
3269+
}
3270+
32573271
/* CONFIG READ LOCK */
32583272
pthread_rwlock_rdlock(&server_opts.config_lock);
32593273

@@ -3265,7 +3279,8 @@ nc_connect_ch_client_dispatch(const char *client_name, nc_server_ch_session_acqu
32653279

32663280
if (!ch_client) {
32673281
ERR(NULL, "Client \"%s\" not found.", client_name);
3268-
return -1;
3282+
rc = -1;
3283+
goto cleanup;
32693284
}
32703285

32713286
/* create the thread argument */
@@ -3283,18 +3298,18 @@ nc_connect_ch_client_dispatch(const char *client_name, nc_server_ch_session_acqu
32833298

32843299
/* creating the thread */
32853300
arg->thread_running = 1;
3286-
if ((r = pthread_create(&tid, NULL, nc_ch_client_thread, arg))) {
3301+
if ((r = pthread_create(&tid, &attr, nc_ch_client_thread, arg))) {
32873302
ERR(NULL, "Creating a new thread failed (%s).", strerror(r));
32883303
rc = -1;
32893304
goto cleanup;
32903305
}
32913306

32923307
/* the thread now manages arg */
3293-
ch_client->tid = tid;
32943308
ch_client->thread_data = arg;
32953309
arg = NULL;
32963310

32973311
cleanup:
3312+
pthread_attr_destroy(&attr);
32983313
if (arg) {
32993314
free(arg->client_name);
33003315
free(arg);

0 commit comments

Comments
 (0)