Skip to content

Commit

Permalink
net: sockets: Keep lock when notifying condvar
Browse files Browse the repository at this point in the history
Releasing the lock before notifying condvar led to a race condition
between a thread calling k_condvar_wait to wait for a condition variable
and another thread signalling for this same condition variable. This
resulted in the waiting thread to stay pending and the handle to it
getting removed from the notifyq, meaning it couldn't get woken up
again.

Signed-off-by: Ambroise Vincent <[email protected]>
  • Loading branch information
ambroise-arm authored and coran21 committed Sep 21, 2023
1 parent bd6f366 commit ac1ea09
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions subsys/net/lib/sockets/sockets.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2017 Linaro Limited
* Copyright (c) 2021 Nordic Semiconductor
* Copyright (c) 2023 Arm Limited (or its affiliates). All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -423,12 +424,12 @@ static void zsock_received_cb(struct net_context *ctx,
k_fifo_put(&ctx->recv_q, pkt);

unlock:
/* Wake reader if it was sleeping */
(void)k_condvar_signal(&ctx->cond.recv);

if (ctx->cond.lock) {
(void)k_mutex_unlock(ctx->cond.lock);
}

/* Wake reader if it was sleeping */
(void)k_condvar_signal(&ctx->cond.recv);
}

int zsock_shutdown_ctx(struct net_context *ctx, int how)
Expand Down

0 comments on commit ac1ea09

Please sign in to comment.