Skip to content

Commit

Permalink
[nrf fromtree] net: lib: coap_client: Remove unnecessary atomic variable
Browse files Browse the repository at this point in the history
In receiving thread, continuing the loops is based on
has_ongoing_exchanges() so it does not need atomic
coap_client_recv_active variable.

When idling, it wakes from semaphore. But there was potential
deadlock when coap_client_schedule_poll() would not signal the
semaphore, if atomic variable was already showing that it runs.
Removing the atomic variable removes this deadlock.

Signed-off-by: Seppo Takalo <[email protected]>
(cherry picked from commit 1e5a537)
  • Loading branch information
SeppoTakalo authored and rlubos committed Nov 4, 2024
1 parent 2e57969 commit 53b26fb
Showing 1 changed file with 1 addition and 8 deletions.
9 changes: 1 addition & 8 deletions subsys/net/lib/coap/coap_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ static K_MUTEX_DEFINE(coap_client_mutex);
static struct coap_client *clients[CONFIG_COAP_CLIENT_MAX_INSTANCES];
static int num_clients;
static K_SEM_DEFINE(coap_client_recv_sem, 0, 1);
static atomic_t coap_client_recv_active;

static bool timeout_expired(struct coap_client_internal_request *internal_req);
static void cancel_requests_with(struct coap_client *client, int error);
Expand Down Expand Up @@ -82,10 +81,7 @@ static int coap_client_schedule_poll(struct coap_client *client, int sock,
memcpy(&internal_req->coap_request, req, sizeof(struct coap_client_request));
internal_req->request_ongoing = true;

if (!coap_client_recv_active) {
k_sem_give(&coap_client_recv_sem);
}
atomic_set(&coap_client_recv_active, 1);
k_sem_give(&coap_client_recv_sem);

return 0;
}
Expand Down Expand Up @@ -956,7 +952,6 @@ static void cancel_requests_with(struct coap_client *client, int error)
reset_internal_request(&client->requests[i]);
}
}
atomic_clear(&coap_client_recv_active);
k_mutex_unlock(&client->lock);

}
Expand All @@ -974,7 +969,6 @@ void coap_client_recv(void *coap_cl, void *a, void *b)

k_sem_take(&coap_client_recv_sem, K_FOREVER);
while (true) {
atomic_set(&coap_client_recv_active, 1);
ret = handle_poll();
if (ret < 0) {
/* Error in polling */
Expand All @@ -987,7 +981,6 @@ void coap_client_recv(void *coap_cl, void *a, void *b)
continue;
} else {
idle:
atomic_set(&coap_client_recv_active, 0);
k_sem_take(&coap_client_recv_sem, K_FOREVER);
}
}
Expand Down

0 comments on commit 53b26fb

Please sign in to comment.