From 1228af2d7d3711ffad4889cfad4bddb555998350 Mon Sep 17 00:00:00 2001 From: Damian Krolik Date: Mon, 20 Oct 2025 11:30:25 +0200 Subject: [PATCH] nrf_rpc: fix initialization corner case When initializing multiple groups that share the same transport, it may happen that a device receives an init packet for a group whose internal members have not been initialized yet, leading to the bus fault. Make sure that internal members of all groups have been initialized before ANY transport is started to prevent that. Signed-off-by: Damian Krolik --- nrf_rpc/nrf_rpc.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/nrf_rpc/nrf_rpc.c b/nrf_rpc/nrf_rpc.c index d6d7dc40f6..528781efc6 100644 --- a/nrf_rpc/nrf_rpc.c +++ b/nrf_rpc/nrf_rpc.c @@ -426,18 +426,6 @@ static int transport_init(nrf_rpc_tr_receive_handler_t receive_cb) const struct nrf_rpc_tr *transport = group->transport; struct nrf_rpc_group_data *data = group->data; - NRF_RPC_ASSERT(transport != NULL); - - /* Initialize all dependencies of `receive_handler` before calling the transport - * init to avoid possible data race if `receive_handler` was invoked before this - * function was completed. */ - if (auto_free_rx_buf(transport)) { - err = nrf_rpc_os_event_init(&data->decode_done_event); - if (err < 0) { - continue; - } - } - err = transport->api->init(transport, receive_cb, NULL); if (err) { NRF_RPC_ERR("Failed to initialize transport, err: %d", err); @@ -1138,6 +1126,7 @@ int nrf_rpc_init(nrf_rpc_err_handler_t err_handler) for (NRF_RPC_AUTO_ARR_FOR(iter, group, &nrf_rpc_groups_array, const struct nrf_rpc_group)) { struct nrf_rpc_group_data *data = group->data; + const struct nrf_rpc_tr *transport = group->transport; if (group_id >= 0xFF) { return -NRF_ENOMEM; @@ -1149,6 +1138,18 @@ int nrf_rpc_init(nrf_rpc_err_handler_t err_handler) if (group->flags & NRF_RPC_FLAGS_WAIT_ON_INIT) { wait_count++; } + + NRF_RPC_ASSERT(transport != NULL); + + /* Initialize all groups' members before starting transports to avoid the risk of + * receiving a packet for a group that hasn't been initialized yet in case a single + * transport is used across multiple groups. */ + if (auto_free_rx_buf(transport)) { + err = nrf_rpc_os_event_init(&data->decode_done_event); + if (err < 0) { + return err; + } + } } group_count = group_id;