Observe: Make sending observe failed messages optional

When a server cleanly closes down (calls coap_free_context()), when
a resource gets deleted, a 4.04 message is sent out (not when using
coap_persist(3)) for any active observe subscriptions. If a server knows it is
going to be restarted, then there is no need to send out a observe failed message.

When deleting a resource when normally running, it is correct to send out a 4.04
Not Found response as the resource has genuinely gone away.  However, when the
server is cleaning shutting down, it is better to use 5.03 Service Unavailable to
indicate the situation may be temporary.
This commit is contained in:
Jon Shallow authored and Jon Shallow committed 2025-03-28 12:25:05 +00:00
1 parent d38e177af1
commit 1ba3158db8
8 files changed
+58 -10

No files matched your search

+11
View File
@@ -572,6 +572,15 @@ coap_context_get_session_timeout(const coap_context_t *context) {
return context->session_timeout;
}
void
coap_context_set_shutdown_no_observe(coap_context_t *context) {
#if COAP_SERVER_SUPPORT
context->shutdown_no_send_observe = 1;
#else /* COAP_SERVER_SUPPORT */
(void)context;
#endif /* COAP_SERVER_SUPPORT */
}
int
coap_context_get_coap_fd(const coap_context_t *context) {
#ifdef COAP_EPOLL_SUPPORT
@@ -803,6 +812,8 @@ coap_free_context_lkd(coap_context_t *context) {
coap_lock_check_locked(context);
#if COAP_SERVER_SUPPORT
/* Removing a resource may cause a NON unsolicited observe to be sent */
if (context->shutdown_no_send_observe)
context->observe_no_clear = 1;
coap_delete_all_resources(context);
#endif /* COAP_SERVER_SUPPORT */
+19 -9
View File
@@ -476,14 +476,15 @@ coap_delete_attr(coap_attr_t *attr) {
typedef enum coap_deleting_resource_t {
COAP_DELETING_RESOURCE,
COAP_NOT_DELETING_RESOURCE
COAP_NOT_DELETING_RESOURCE,
COAP_DELETING_RESOURCE_ON_EXIT
} coap_deleting_resource_t;
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r,
coap_deleting_resource_t deleting);
static void
coap_free_resource(coap_resource_t *resource) {
coap_free_resource(coap_resource_t *resource, coap_deleting_resource_t deleting) {
coap_attr_t *attr, *tmp;
coap_subscription_t *obs, *otmp;
@@ -491,7 +492,7 @@ coap_free_resource(coap_resource_t *resource) {
if (!resource->context->observe_no_clear) {
coap_resource_notify_observers_lkd(resource, NULL);
coap_notify_observers(resource->context, resource, COAP_DELETING_RESOURCE);
coap_notify_observers(resource->context, resource, deleting);
}
if (resource->context->resource_deleted)
@@ -536,11 +537,11 @@ coap_add_resource_lkd(coap_context_t *context, coap_resource_t *resource) {
coap_lock_check_locked(context);
if (resource->is_unknown) {
if (context->unknown_resource)
coap_free_resource(context->unknown_resource);
coap_free_resource(context->unknown_resource, COAP_DELETING_RESOURCE);
context->unknown_resource = resource;
} else if (resource->is_proxy_uri) {
if (context->proxy_uri_resource)
coap_free_resource(context->proxy_uri_resource);
coap_free_resource(context->proxy_uri_resource, COAP_DELETING_RESOURCE);
context->proxy_uri_resource = resource;
} else {
coap_resource_t *r = coap_get_resource_from_uri_path_lkd(context,
@@ -616,7 +617,7 @@ coap_delete_resource_lkd(coap_context_t *context, coap_resource_t *resource) {
}
/* and free its allocated memory */
coap_free_resource(resource);
coap_free_resource(resource, COAP_DELETING_RESOURCE);
return 1;
}
@@ -631,17 +632,17 @@ coap_delete_all_resources(coap_context_t *context) {
HASH_ITER(hh, context->resources, res, rtmp) {
HASH_DELETE(hh, context->resources, res);
coap_free_resource(res);
coap_free_resource(res, COAP_DELETING_RESOURCE_ON_EXIT);
}
context->resources = NULL;
if (context->unknown_resource) {
coap_free_resource(context->unknown_resource);
coap_free_resource(context->unknown_resource, COAP_DELETING_RESOURCE_ON_EXIT);
context->unknown_resource = NULL;
}
if (context->proxy_uri_resource) {
coap_free_resource(context->proxy_uri_resource);
coap_free_resource(context->proxy_uri_resource, COAP_DELETING_RESOURCE_ON_EXIT);
context->proxy_uri_resource = NULL;
}
}
@@ -1233,6 +1234,15 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r,
obs = NULL;
}
break;
case COAP_DELETING_RESOURCE_ON_EXIT:
/* Don't worry if it does not get there */
response->type = COAP_MESSAGE_NON;
response->code = COAP_RESPONSE_CODE(503);
coap_add_option_internal(response, COAP_OPTION_MAXAGE,
coap_encode_var_safe(buf, sizeof(buf),
30),
buf);
break;
case COAP_DELETING_RESOURCE:
default:
/* Don't worry if it does not get there */