From ac1ea09a554c787edb0c3d9fee8ce8561270c1ab Mon Sep 17 00:00:00 2001 From: Ambroise Vincent Date: Tue, 12 Sep 2023 16:43:28 +0100 Subject: [PATCH] net: sockets: Keep lock when notifying condvar 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 --- subsys/net/lib/sockets/sockets.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/subsys/net/lib/sockets/sockets.c b/subsys/net/lib/sockets/sockets.c index 801ed99fe60b76f..facc509acaf3bf8 100644 --- a/subsys/net/lib/sockets/sockets.c +++ b/subsys/net/lib/sockets/sockets.c @@ -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 */ @@ -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)