From c583e35ee0628bcb702e2e65b459fe06231e6026 Mon Sep 17 00:00:00 2001 From: Jakub Rzeszutko Date: Mon, 20 Oct 2025 12:24:31 +0200 Subject: [PATCH] shell: fix deadlock in the bypass function Previously, the bypass() function was called while the shell mutex was still held, preventing the use of shell APIs (e.g. shell_print()) inside the bypass context. This change unlocks the shell mutex before invoking bypass() and locks it again afterwards, restoring proper shell context handling and preventing potential deadlocks. Issue fixed by: roni1234321 Fixes #97722 Signed-off-by: Jakub Rzeszutko --- subsys/shell/shell.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index da203ddc62c2f..8c3380d23ba6d 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -999,7 +999,18 @@ static void state_collect(const struct shell *sh) sizeof(buf), &count); if (count) { z_flag_cmd_ctx_set(sh, true); + /** Unlock the shell mutex before calling the bypass function, + * allowing shell APIs (e.g. shell_print()) to be used inside it. + * Since these APIs require the mutex to be unlocked, + * we temporarily leave the shell context and transfer control + * to the bypass function. + */ + z_shell_unlock(sh); bypass(sh, buf, count); + /* After returning, we're back in the shell context — re-acquire + * the shell mutex on the shell thread. + */ + z_shell_lock(sh); z_flag_cmd_ctx_set(sh, false); /* Check if bypass mode ended. */ if (!(volatile shell_bypass_cb_t *)sh->ctx->bypass) {