From eae32b7e7aa796f6c8c852f853bdeab9648a5860 Mon Sep 17 00:00:00 2001 From: Marcin Niestroj Date: Thu, 19 Nov 2020 19:42:42 +0100 Subject: [PATCH] drivers: modem: fix race condition when waiting on cmd reply Command was sent first, then a semaphore was reset. This semaphore could be released by received reply even before semaphore was reset. This is quite unlikely, but possible result of race condition. Move k_sem_reset() call before attempt to send command over modem interface. This makes sure that receiving reply momentarily after sending request will always be handled properly. Signed-off-by: Marcin Niestroj --- drivers/modem/modem_cmd_handler.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/modem/modem_cmd_handler.c b/drivers/modem/modem_cmd_handler.c index b191265212..4753f3e87e 100644 --- a/drivers/modem/modem_cmd_handler.c +++ b/drivers/modem/modem_cmd_handler.c @@ -514,11 +514,14 @@ static int _modem_cmd_send(struct modem_iface *iface, LOG_DBG("EOL not set!!!"); } #endif + if (sem) { + k_sem_reset(sem); + } + iface->write(iface, buf, strlen(buf)); iface->write(iface, data->eol, data->eol_len); if (sem) { - k_sem_reset(sem); ret = k_sem_take(sem, timeout); if (ret == 0) {