Skip to content

Commit 8a1f57f

Browse files
jbrzozoskirlubos
authored andcommitted
net: lib: download_client: fix errors in retry mechanism
Depending on when the socket error happened, a partial buffer was sometimes left behind causing the retry to fail immediately. If the failure happens when there is a partial fragment in the buffer, hand it to the application first if necessary, then clear the buffer before restarting. Signed-off-by: Justin Brzozoski <[email protected]> Signed-off-by: Emanuele Di Santo <[email protected]>
1 parent 3f91a69 commit 8a1f57f

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

subsys/net/lib/download_client/src/download_client.c

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -410,24 +410,39 @@ void download_thread(void *client, void *a, void *b)
410410
while (true) {
411411
__ASSERT(dl->offset < sizeof(dl->buf), "Buffer overflow");
412412

413-
LOG_DBG("Receiving bytes..");
413+
LOG_DBG("Receiving up to %d bytes at %p...",
414+
(sizeof(dl->buf) - dl->offset), (dl->buf + dl->offset));
415+
414416
len = recv(dl->fd, dl->buf + dl->offset,
415417
sizeof(dl->buf) - dl->offset, 0);
416418

417-
if (len == -1) {
418-
LOG_ERR("Error reading from socket, errno %d", errno);
419-
rc = error_evt_send(dl, ENOTCONN);
420-
if (rc) {
421-
/* Restart and suspend */
422-
break;
419+
if ((len == 0) || (len == -1)) {
420+
/* We just had an unexpected socket error or closure */
421+
422+
/* If there is a partial data payload in our buffer,
423+
* and it has been accounted in our progress, we have
424+
* to hand it to the application before discarding it.
425+
*/
426+
if ((dl->offset > 0) && (dl->has_header)) {
427+
rc = fragment_evt_send(dl);
428+
if (rc) {
429+
/* Restart and suspend */
430+
LOG_INF("Fragment refused, download "
431+
"stopped.");
432+
break;
433+
}
434+
}
435+
436+
if (len == -1) {
437+
LOG_ERR("Error in recv(), errno %d", errno);
438+
rc = error_evt_send(dl, ENOTCONN);
439+
}
440+
441+
if (len == 0) {
442+
LOG_WRN("Peer closed connection!");
443+
rc = error_evt_send(dl, ECONNRESET);
423444
}
424-
reconnect(dl);
425-
goto send_again;
426-
}
427445

428-
if (len == 0) {
429-
LOG_WRN("Peer closed connection!");
430-
rc = error_evt_send(dl, ECONNRESET);
431446
if (rc) {
432447
/* Restart and suspend */
433448
break;
@@ -501,18 +516,18 @@ void download_thread(void *client, void *a, void *b)
501516
break;
502517
}
503518

504-
/* Request next fragment */
505-
dl->offset = 0;
506-
dl->has_header = false;
507-
508519
/* Attempt to reconnect if the connection was closed */
509520
if (dl->connection_close) {
510521
dl->connection_close = false;
511522
reconnect(dl);
512523
}
513524

525+
/* Request next fragment */
514526
/* Send a GET request for the next bytes */
515527
send_again:
528+
dl->offset = 0;
529+
dl->has_header = false;
530+
516531
rc = get_request_send(dl);
517532
if (rc) {
518533
rc = error_evt_send(dl, ECONNRESET);

0 commit comments

Comments
 (0)