From c3a3a83989cba87e7ca86b88253201e90f60b856 Mon Sep 17 00:00:00 2001 From: Ville Juven Date: Mon, 9 Oct 2023 15:09:31 +0300 Subject: [PATCH] SubscriptionCallback.hpp: Fix race condition in unregisterCb/Poll Remove _cb_handle before calling unregister_callback, to ensure no one uses the old stale handle before it is cleared. --- platforms/common/uORB/SubscriptionCallback.hpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/platforms/common/uORB/SubscriptionCallback.hpp b/platforms/common/uORB/SubscriptionCallback.hpp index b1fe664e4385..753aa5af713e 100644 --- a/platforms/common/uORB/SubscriptionCallback.hpp +++ b/platforms/common/uORB/SubscriptionCallback.hpp @@ -87,10 +87,10 @@ class SubscriptionCallback : public SubscriptionInterval void unregisterCallback() { if (registered()) { - DeviceNode::unregister_callback(_subscription.get_node(), _cb_handle); + uorb_cb_handle_t handle = _cb_handle; + _cb_handle = UORB_INVALID_CB_HANDLE; + DeviceNode::unregister_callback(_subscription.get_node(), handle); } - - _cb_handle = UORB_INVALID_CB_HANDLE; } /** @@ -203,8 +203,9 @@ class SubscriptionPollable : public SubscriptionInterval void unregisterPoll() { - DeviceNode::unregister_callback(_subscription.get_node(), _cb_handle); + uorb_cb_handle_t handle = _cb_handle; _cb_handle = UORB_INVALID_CB_HANDLE; + DeviceNode::unregister_callback(_subscription.get_node(), handle); } private: uorb_cb_handle_t _cb_handle{UORB_INVALID_CB_HANDLE};