Hi All,

I was running our rate limiting tests on rc-8_24_0-1, and one throttled 
download test started measuring half of the expected speed, which we didn't 
observe before.

And it seems that the cause is a regression from a8b9f53118 ("setopt: keep a 
paused transfer paused when the speed limit is changed", #23462),
which is in rc-8_24_0-1 and still in master.

Here is the problem:
When an application sets CURLOPT_MAX_RECV_SPEED_LARGE (or 
CURLOPT_MAX_SEND_SPEED_LARGE) on an easy handle that has already done
a transfer, the next transfer waits about one second before it reads the first 
byte.

After that the rate is correct, so a 1 MB download at 1 MB/s takes 2 s instead 
of 1 s.

Since a8b9f53118, setopt calls Curl_rlimit_change(), which keeps the limiter's 
timestamp and token count from the previous transfer.
The previous, unlimited transfer left them at the start of its body with zero 
tokens (its rate was 0).

The next transfer checks the limiter before reading the response headers: less 
than one step (1 s) has passed since that old timestamp,
so no tokens are added, and the transfer is blocked until the step ends.
Curl_rlimit_start() resets the limiter only once the body starts, which is too 
late.

Before a8b9f53118, setopt re-initialized the limiter, so this did not happen; 
but that also unpaused a paused transfer, which is what a8b9f53118 fixed.

Here are how to reproduce this issue:

Any fast local HTTP server will do, for example:

  head -c 1000000 /dev/zero > 1mb.bin
  python3 -m http.server 8000

Then build and run the program below against it:

  cc rlimit_reuse.c -o rlimit_reuse -lcurl
  ./rlimit_reuse http://localhost:8000/1mb.bin

---8<--- rlimit_reuse.c
#include <stdio.h>
#include <curl/curl.h>

static size_t sink(char *ptr, size_t size, size_t nmemb, void *userp)
{
  (void)ptr;
  (void)userp;
  return size * nmemb;
}

static void get(CURL *curl, const char *what)
{
  double ttfb = 0, total = 0;
  CURLcode result = curl_easy_perform(curl);
  curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, &ttfb);
  curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total);
  printf("%-22s result %d, first byte after %.3f s, total %.3f s\n",
         what, (int)result, ttfb, total);
}

int main(int argc, char **argv)
{
  CURL *curl;

  if(argc < 2) {
    fprintf(stderr, "usage: %s <URL of a ~1 MB file>\n", argv[0]);
    return 1;
  }
  curl_global_init(CURL_GLOBAL_DEFAULT);
  curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, sink);

  get(curl, "1. unlimited");

  /* same handle, now limited to 1 MB/s (8 Mbps) */
  curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)1000000);
  get(curl, "2. limited, 1 MB/s");
  get(curl, "3. limited, again");

  curl_easy_cleanup(curl);
  curl_global_cleanup();
  return 0;
}
---8<---

Here are the possible results:

  1. unlimited           result 0, first byte after 0.005 s, total 0.006 s
  2. limited, 1 MB/s     result 0, first byte after 1.001 s, total 1.998 s
  3. limited, again      result 0, first byte after 0.002 s, total 0.998 s

Transfer 2 should take about 1 s, like transfer 3.

---------------------------------------------------------------------------------------------------------------
This issue can be possibly fixed like:

We keep a8b9f53118's behavior for a paused transfer, but start rate limiting 
afresh when the speed limit is set:
after Curl_rlimit_change(), call Curl_rlimit_start() unless the limiter is 
blocked.
The new rate then does not inherit the time step and tokens of an earlier 
transfer.

This can be implemented like:

---8<--- proposed change to lib/setopt.c
--- a/lib/setopt.c
+++ b/lib/setopt.c
@@ -2713,6 +2713,19 @@ static CURLcode setopt_func(struct Curl_easy *data, 
CURLoption option,
   return CURLE_OK;
}

+/* Apply a new transfer speed limit. A paused transfer stays paused (the
+ * limiter is blocked); otherwise rate limiting starts afresh now, so the
+ * new rate does not inherit the time step and token count of an earlier
+ * transfer on the same handle. */
+static void setopt_rlimit(struct Curl_easy *data, struct Curl_rlimit *r,
+                          curl_off_t rate)
+{
+  /* use minimal burst rate of 32k. some protocol batch IO */
+  Curl_rlimit_change(r, rate, CURLMAX(rate, (32 * 1024)));
+  if(!Curl_rlimit_is_blocked(r))
+    Curl_rlimit_start(r, Curl_pgrs_now(data), -1);
+}
+
static CURLcode setopt_offt(struct Curl_easy *data, CURLoption option,
                             curl_off_t offt)
{
@@ -2759,9 +2772,7 @@ static CURLcode setopt_offt(struct Curl_easy *data, 
CURLoption option,
     if(offt < 0)
       return CURLE_BAD_FUNCTION_ARGUMENT;
     s->max_send_speed = offt;
-    /* use minimal burst rate of 32k. some protocol batch IO */
-    Curl_rlimit_change(&data->progress.ul.rlimit, offt,
-                     CURLMAX(offt, (32 * 1024)));
+    setopt_rlimit(data, &data->progress.ul.rlimit, offt);
     break;
   case CURLOPT_MAX_RECV_SPEED_LARGE:
     /*
@@ -2771,9 +2782,7 @@ static CURLcode setopt_offt(struct Curl_easy *data, 
CURLoption option,
     if(offt < 0)
       return CURLE_BAD_FUNCTION_ARGUMENT;
     s->max_recv_speed = offt;
-    /* use minimal burst rate of 32k. some protocol batch IO */
-    Curl_rlimit_change(&data->progress.dl.rlimit, offt,
-                     CURLMAX(offt, (32 * 1024)));
+    setopt_rlimit(data, &data->progress.dl.rlimit, offt);
     break;
   case CURLOPT_RESUME_FROM_LARGE:
     /*
---8<---

With this change, the same test program gives:

  1. unlimited             result 0, first byte after 0.001 s, total 0.002 s
  2. limited, 1 MB/s result 0, first byte after 0.001 s, total 0.998 s
  3. limited, again      result 0, first byte after 0.001 s, total 0.998 s

And this change fixed our tests as well, but maybe it misses something that I 
don't see.

Thanks,
Dmitry Karpov



-- 
Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html

Reply via email to