From 33b3f02006733a3f0ba19ac1b03f0f34d4a952dc Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Mon, 30 Jun 2025 14:44:50 +0100 Subject: [PATCH] coap_session.c: Clean up session close down order --- man/coap_handler.txt.in | 4 ++++ src/coap_session.c | 18 +++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/man/coap_handler.txt.in b/man/coap_handler.txt.in index d3c2d25f..bc12cd29 100644 --- a/man/coap_handler.txt.in +++ b/man/coap_handler.txt.in @@ -392,6 +392,10 @@ typedef enum { } coap_event_t; ---- +*NOTE:* For the COAP_*_CLOSED events, as these are triggered when a session is +closing down, not all the information may be there in the _session_ parameter +passed to the application event handler. + EXAMPLES -------- *GET Resource Callback Handler* diff --git a/src/coap_session.c b/src/coap_session.c index 089a0652..0ff6a527 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -551,6 +551,15 @@ coap_session_mfree(coap_session_t *session) { } #endif /* COAP_CLIENT_SUPPORT */ + LL_FOREACH_SAFE(session->delayqueue, q, tmp) { + if (q->pdu->type==COAP_MESSAGE_CON) { + coap_handle_nack(session, q->pdu, + session->proto == COAP_PROTO_DTLS ? + COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE, + q->id); + } + coap_delete_node_lkd(q); + } if (session->partial_pdu) coap_delete_pdu_lkd(session->partial_pdu); if (session->sock.lfunc[COAP_LAYER_SESSION].l_close) @@ -571,15 +580,6 @@ coap_session_mfree(coap_session_t *session) { } } #endif /* COAP_PROXY_SUPPORT */ - LL_FOREACH_SAFE(session->delayqueue, q, tmp) { - if (q->pdu->type==COAP_MESSAGE_CON) { - coap_handle_nack(session, q->pdu, - session->proto == COAP_PROTO_DTLS ? - COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE, - q->id); - } - coap_delete_node_lkd(q); - } LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) { LL_DELETE(session->lg_xmit, lq); coap_block_delete_lg_xmit(session, lq);