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.

(cherry picked from commit bb450eb)

Original-Signed-off-by: Ambroise Vincent <[email protected]>
GitOrigin-RevId: bb450eb
Change-Id: Iea780f4f18e9d72718035598dbbaabc610a6ac75
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/4875291
Tested-by: ChromeOS Prod (Robot) <[email protected]>
Tested-by: Al Semjonovs <[email protected]>
Commit-Queue: Al Semjonovs <[email protected]>
Reviewed-by: Al Semjonovs <[email protected]>
  • Loading branch information
ambroise-arm authored and Chromeos LUCI committed Sep 19, 2023
1 parent b80f95c commit 9456149
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 9456149

Please sign in to comment.