From 9de0ff377a3b23a0750bf0dda97a267c329b9426 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Tue, 8 Dec 2020 16:38:25 +0000 Subject: [PATCH] Observe: Fix coap_free_context() assert when active observe src/net.c: Call coap_delete_all_resources() (which may send a CON observe bumping a session ref count) before coap_delete_all() to remove any CON transmissions that are still in flight in cop_free_context(). src/resource.c: Delete observer if GET handler does not return a 2.xx code. --- src/net.c | 5 +++-- src/resource.c | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/net.c b/src/net.c index 839513d8..35d994f1 100644 --- a/src/net.c +++ b/src/net.c @@ -586,6 +586,9 @@ coap_free_context(coap_context_t *context) { if (!context) return; + /* Removing a resource may cause a CON observe to be sent */ + coap_delete_all_resources(context); + coap_delete_all(context->sendqueue); #ifdef WITH_LWIP @@ -593,8 +596,6 @@ coap_free_context(coap_context_t *context) { coap_retransmittimer_restart(context); #endif - coap_delete_all_resources(context); - #ifndef WITHOUT_ASYNC coap_delete_all_async(context); #endif /* WITHOUT_ASYNC */ diff --git a/src/resource.c b/src/resource.c index ad695482..42812500 100644 --- a/src/resource.c +++ b/src/resource.c @@ -906,6 +906,9 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r, assert(h); /* we do not allow subscriptions if no * GET/FETCH handler is defined */ h(context, r, obs->session, NULL, &token, obs->query, response); + if (COAP_RESPONSE_CLASS(response->code) > 2) { + coap_delete_observer(r, obs->session, &token); + } break; case COAP_DELETING_RESOURCE: default: @@ -913,9 +916,6 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r, break; } - /* TODO: do not send response and remove observer when - * COAP_RESPONSE_CLASS(response->hdr->code) > 2 - */ if (response->type == COAP_MESSAGE_CON || (r->flags & COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS)) { obs->non_cnt = 0;