Skip to content

Commit 623a1ff

Browse files
SeppoTakalommahadevan108
authored andcommitted
net: lib: coap_client: Don't decrease retry counter on send() failure
If send() fails, we have not technically send the CoAP retry yet, so restore the same pending structure, so our timeouts and retry counters stay the same. This will trigger a retry next time the poll() return POLLOUT, so we know that we can send. Signed-off-by: Seppo Takalo <[email protected]>
1 parent 6481b0e commit 623a1ff

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

subsys/net/lib/coap/coap_client.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,13 @@ static int resend_request(struct coap_client *client,
471471
{
472472
int ret = 0;
473473

474+
/* Copy the pending structure if we need to restore it */
475+
struct coap_pending tmp = internal_req->pending;
476+
474477
if (internal_req->request_ongoing &&
475478
internal_req->pending.timeout != 0 &&
476479
coap_pending_cycle(&internal_req->pending)) {
477-
LOG_ERR("Timeout in poll, retrying send");
480+
LOG_ERR("Timeout, retrying send");
478481

479482
/* Reset send block context as it was updated in previous init from packet */
480483
if (internal_req->send_blk_ctx.total_size > 0) {
@@ -483,22 +486,27 @@ static int resend_request(struct coap_client *client,
483486
ret = coap_client_init_request(client, &internal_req->coap_request,
484487
internal_req, true);
485488
if (ret < 0) {
486-
LOG_ERR("Error re-creating CoAP request");
489+
LOG_ERR("Error re-creating CoAP request %d", ret);
490+
return ret;
491+
}
492+
493+
ret = send_request(client->fd, internal_req->request.data,
494+
internal_req->request.offset, 0, &client->address,
495+
client->socklen);
496+
if (ret > 0) {
497+
ret = 0;
498+
} else if (ret == -1 && errno == EAGAIN) {
499+
/* Restore the pending structure, retry later */
500+
internal_req->pending = tmp;
501+
/* Not a fatal socket error, will trigger a retry */
502+
ret = 0;
487503
} else {
488-
ret = send_request(client->fd, internal_req->request.data,
489-
internal_req->request.offset, 0, &client->address,
490-
client->socklen);
491-
if (ret > 0) {
492-
ret = 0;
493-
} else {
494-
LOG_ERR("Failed to resend request, %d", ret);
495-
}
504+
ret = -errno;
505+
LOG_ERR("Failed to resend request, %d", ret);
496506
}
497507
} else {
498-
LOG_ERR("Timeout in poll, no more retries left");
508+
LOG_ERR("Timeout, no more retries left");
499509
ret = -ETIMEDOUT;
500-
report_callback_error(internal_req, ret);
501-
internal_req->request_ongoing = false;
502510
}
503511

504512
return ret;

0 commit comments

Comments
 (0)