diff --git a/src/ckpool.c b/src/ckpool.c index 6e2f92d1..e74aa0ca 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -745,6 +745,8 @@ json_t *json_rpc_call(connsock_t *cs, const char *rpc_req) double elapsed; int len, ret; + /* Serialise all calls in case we use cs from multiple threads */ + cksem_wait(&cs->sem); if (unlikely(cs->fd < 0)) { LOGWARNING("FD %d invalid in %s", cs->fd, __func__); goto out; @@ -839,6 +841,7 @@ out_empty: out: free(http_req); dealloc(cs->buf); + cksem_post(&cs->sem); return val; } diff --git a/src/ckpool.h b/src/ckpool.h index 573ddc8f..6d0e7348 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -79,6 +79,8 @@ struct connsock { char *buf; int bufofs; int buflen; + /* Semaphore used to serialise request/responses */ + sem_t sem; }; typedef struct connsock connsock_t; diff --git a/src/generator.c b/src/generator.c index 27df2509..2e59816c 100644 --- a/src/generator.c +++ b/src/generator.c @@ -1698,6 +1698,8 @@ static int server_mode(ckpool_t *ckp, proc_instance_t *pi) si->auth = ckp->btcdauth[i]; si->pass = ckp->btcdpass[i]; si->notify = ckp->btcdnotify[i]; + cksem_init(&si->cs.sem); + cksem_post(&si->cs.sem); } ret = gen_loop(pi);