From 5869554d7a8ed632d91cce03c51c6686f06b1819 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Mon, 6 Jan 2025 10:35:39 +0100 Subject: [PATCH] sys/net/nanocoap: flush stale responses before sending request --- sys/net/application_layer/nanocoap/sock.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index 73aa3db89ef5..0b3db123b777 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -195,6 +195,12 @@ static uint32_t _deadline_left_us(uint32_t deadline) return deadline - now; } +static void _sock_flush(nanocoap_sock_t *sock) +{ + void *payload, *ctx = NULL; + while (_sock_recv_buf(sock, &payload, &ctx, 0) > 0 || ctx) {} +} + ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt, coap_request_cb_t cb, void *arg) { @@ -223,6 +229,9 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt, .iol_len = coap_get_total_len(pkt), }; + /* clear out stale responses from previous requests */ + _sock_flush(sock); + while (1) { switch (state) { case STATE_REQUEST_SEND: