This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 397f2fceee3139759962e5d4bc1541c0794ff26d Author: likun17 <[email protected]> AuthorDate: Thu Jan 8 20:12:21 2026 +0800 drivers/sensors/sensor_rpmsg: Recheck the stub after getting a tx buffer. sensor_rpmsg_push_event_persist() waits for a tx buffer with rpmsg_get_tx_payload_buffer(). While it waits, the rptun thread can handle the next message from the same endpoint, for example an UNSUBSCRIBE that frees the stub: sensor_rpmsg_sub_handler -> rpmsg_get_tx_payload_buffer -> rptun_notify_wait -> rptun_worker -> sensor_rpmsg_ept_cb -> sensor_rpmsg_unsub_handler (frees the stub) <- continues with the freed stub Look the stub up again by endpoint and cookie after the buffer is obtained, release the buffer and stop if it no longer exists, and return the current stub to the caller. Signed-off-by: likun17 <[email protected]> --- drivers/sensors/sensor_rpmsg.c | 57 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/drivers/sensors/sensor_rpmsg.c b/drivers/sensors/sensor_rpmsg.c index b8ff9950151..752a1baea82 100644 --- a/drivers/sensors/sensor_rpmsg.c +++ b/drivers/sensors/sensor_rpmsg.c @@ -599,13 +599,17 @@ sensor_rpmsg_alloc_proxy(FAR struct sensor_rpmsg_dev_s *dev, return proxy; } -static -void sensor_rpmsg_push_event_persist(FAR struct sensor_rpmsg_dev_s *dev, - FAR struct sensor_rpmsg_stub_s *stub) +static FAR struct sensor_rpmsg_stub_s * +sensor_rpmsg_push_event_persist(FAR struct sensor_rpmsg_dev_s *dev, + FAR struct rpmsg_endpoint *ept, + FAR struct sensor_rpmsg_stub_s *stub, + uint64_t cookie) { FAR struct sensor_rpmsg_cell_s *cell; + FAR struct sensor_rpmsg_stub_s *temp; FAR struct sensor_rpmsg_data_s *msg; FAR struct sensor_rpmsg_ept_s *sre; + bool find = false; uint32_t space; int ret; @@ -615,7 +619,46 @@ void sensor_rpmsg_push_event_persist(FAR struct sensor_rpmsg_dev_s *dev, { snerr("ERROR: push event persist get buffer failed:%s\n", rpmsg_get_cpuname(sre->ept.rdev)); - return; + return NULL; + } + + /* Check if the stub still exists. + * rpmsg involves recursive calls, which may result in the sub_handler + * not completing its execution, the rpmsg recursively switches to + * the unsub_handler and releases the stub. + */ + + list_for_every_entry(&dev->stublist, temp, + struct sensor_rpmsg_stub_s, node) + { + /* Check if this stub matches the given ept and cookie. */ + + if (temp->ept == ept && temp->cookie == cookie) + { + find = true; + break; + } + } + + /* The current position requires determining whether the stub + * node exists. There are three possibilities. + * 1.The node exists, and it's still the address originally allocated + * by malloc. + * 2.The node exists, but the address has changed due to reallocation. + * the stub address needs to be updated. Return to the new address + * of the stub + * 3.The node does not exist anymore,return directly. + */ + + if (!find) + { + rpmsg_release_tx_buffer(&sre->ept, msg); + return NULL; + } + else if (stub != temp) + { + snwarn("WARN: push event persist stub changed:%p -> %p\n", stub, temp); + stub = temp; } msg->command = SENSOR_RPMSG_PUBLISH; @@ -634,6 +677,8 @@ void sensor_rpmsg_push_event_persist(FAR struct sensor_rpmsg_dev_s *dev, { rpmsg_release_tx_buffer(&sre->ept, msg); } + + return stub; } static FAR struct sensor_rpmsg_stub_s * @@ -680,8 +725,8 @@ sensor_rpmsg_alloc_stub(FAR struct sensor_rpmsg_dev_s *dev, if (dev->lower.persist) { - sminfo(dev->name, "push event persist:%p", stub); - sensor_rpmsg_push_event_persist(dev, stub); + stub = sensor_rpmsg_push_event_persist(dev, ept, stub, cookie); + sminfo(dev->name, "push event persist status:%p", stub); } sensor_rpmsg_unlock(dev);
