All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Ravi Mistry via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,
	 Vaidas Pilkauskas <vaidas.pilkauskas@shopify.com>,
	Patrick Steinhardt <ps@pks.im>,
	 "brian m. carlson" <sandals@crustytoothpaste.net>,
	 Ravi Mistry <rmistry@google.com>
Subject: Re: [PATCH v4 4/5] remote-curl: retry transient HTTP errors in post_rpc()
Date: Tue, 24 Nov 2026 15:04:02 -0800	[thread overview]
Message-ID: <xmqq4id5kf19.fsf@gitster.g> (raw)
In-Reply-To: <ca247f390c8ecd2df20f7054f3357bf8da86bee2.1795103883.git.gitgitgadget@gmail.com> (Ravi Mistry via GitGitGadget's message of "Thu, 19 Nov 2026 15:58:02 +0000")

"Ravi Mistry via GitGitGadget" <gitgitgadget@gmail.com> writes:

> diff --git a/Documentation/config/http.adoc b/Documentation/config/http.adoc
> index 726669e85c..72e9bf011d 100644
> --- a/Documentation/config/http.adoc
> +++ b/Documentation/config/http.adoc
> @@ -350,7 +350,9 @@ http.maxRetries::
>  	Maximum number of times to retry after receiving a retryable HTTP
>  	status code. The set of retryable status codes is configured by
>  	`http.retryStatusCodes`, which includes only HTTP 429 (Too Many
> -	Requests) by default. Set to 0 (the default) to disable retries.
> +	Requests) by default. Retries apply to HTTP GET requests and
> +	read-only smart-HTTP POST requests (`git-upload-pack` and
> +	`git-upload-archive`). Set to 0 (the default) to disable retries.
>  	Can be overridden by the `GIT_HTTP_MAX_RETRIES` environment variable.
>  	See also `http.retryAfter`, `http.maxRetryTime`, and
>  	`http.retryStatusCodes`.
> diff --git a/http.c b/http.c
> index 4b92819abd..f7f4af7a31 100644
> --- a/http.c
> +++ b/http.c
> @@ -2479,6 +2479,34 @@ static long handle_rate_limit_retry(long slot_retry_after, size_t attempt)
>  	}
>  }
>  
> +int http_retry_wait(long http_code, long slot_retry_after, size_t attempt)
> +{
> +	long retry_delay;
> +
> +	if (http_max_retries <= 0)
> +		return -1;
> +	if (attempt > (size_t)http_max_retries) {
> +		trace2_data_string("http", the_repository,
> +				   "http/429-error", "retries-exhausted");
> +		return -1;
> +	}
> +	retry_delay = handle_rate_limit_retry(slot_retry_after, attempt);
> +	if (retry_delay < 0)
> +		return -1;
> +	if (retry_delay > 0) {
> +		if (http_code == 429)
> +			warning(_("rate limited, waiting %ld seconds before retry"),
> +				retry_delay);
> +		else
> +			warning(_("HTTP %ld, waiting %ld seconds before retry"),
> +				http_code, retry_delay);
> +		trace2_data_intmax("http", the_repository,
> +				   "http/retry-sleep-seconds", retry_delay);
> +		sleep(retry_delay);
> +	}
> +	return 0;
> +}
> +
>  static int http_request_recoverable(const char *url,
>  			       void *result, int target,
>  			       struct http_get_options *options)
> @@ -2486,7 +2514,7 @@ static int http_request_recoverable(const char *url,
>  	static struct http_get_options empty_opts;
>  	int i = 3;
>  	int ret;
> -	int rate_limit_retries = http_max_retries;
> +	size_t attempt = 0;
>  
>  	if (!options)
>  		options = &empty_opts;
> @@ -2508,9 +2536,8 @@ static int http_request_recoverable(const char *url,
>  	}
>  
>  	while ((ret == HTTP_REAUTH && --i) ||
> -	       (ret == HTTP_RATE_LIMITED && http_max_retries > 0 &&
> -		rate_limit_retries-- > 0)) {
> -		long retry_delay = -1;
> +	       (ret == HTTP_RATE_LIMITED &&
> +		!http_retry_wait(options->http_code, options->retry_after, ++attempt))) {
>  		/*
>  		 * The previous request may have put cruft into our output stream; we
>  		 * should clear it out before making our next request.
> @@ -2535,37 +2562,15 @@ static int http_request_recoverable(const char *url,
>  		default:
>  			BUG("Unknown http_request target");
>  		}
> -		if (ret == HTTP_RATE_LIMITED) {
> -			retry_delay = handle_rate_limit_retry(
> -				options->retry_after,
> -				(size_t)(http_max_retries - rate_limit_retries));
> -			if (retry_delay < 0)
> -				return HTTP_ERROR;
> -
> -			if (retry_delay > 0) {
> -				if (options->http_code == 429)
> -					warning(_("rate limited, waiting %ld seconds before retry"),
> -						retry_delay);
> -				else
> -					warning(_("HTTP %ld, waiting %ld seconds before retry"),
> -						options->http_code, retry_delay);
> -				trace2_data_intmax("http", the_repository,
> -						   "http/retry-sleep-seconds", retry_delay);
> -				sleep(retry_delay);
> -			}
> -		} else if (ret == HTTP_REAUTH) {
> +		if (ret == HTTP_REAUTH)
>  			http_reauth_prepare(1);
> -		}
>  
>  		ret = http_request(url, result, target, options);
>  	}
> -	if (ret == HTTP_RATE_LIMITED) {
> -		if (http_max_retries <= 0)
> -			return HTTP_ERROR;
> -		trace2_data_string("http", the_repository,
> -				   "http/429-error", "retries-exhausted");
> -		return options->http_code == 429 ? HTTP_RATE_LIMITED : HTTP_ERROR;
> -	}
> +	if (ret == HTTP_RATE_LIMITED &&
> +	    (http_max_retries <= 0 || attempt <= (size_t)http_max_retries ||
> +	     options->http_code != 429))
> +		return HTTP_ERROR;
>  	return ret;
>  }
>  
> diff --git a/http.h b/http.h
> index 5985a0f71f..ec96b6d2c7 100644
> --- a/http.h
> +++ b/http.h
> @@ -201,6 +201,12 @@ int http_get_strbuf(const char *url, struct strbuf *result, struct http_get_opti
>  int http_get_file(const char *url, const char *filename,
>  		  struct http_get_options *options);
>  
> +/*
> + * Wait before retrying a transient HTTP error (1-based attempt count).
> + * Returns 0 if the caller should retry, or -1 to stop retrying.
> + */
> +int http_retry_wait(long http_code, long slot_retry_after, size_t attempt);
> +
>  int http_fetch_ref(const char *base, struct ref *ref);
>  
>  struct curl_slist *http_append_auth_header(const struct credential *c,
> diff --git a/remote-curl.c b/remote-curl.c
> index 2c35dd5240..b92f33609d 100644
> --- a/remote-curl.c
> +++ b/remote-curl.c
> @@ -845,35 +845,22 @@ static size_t rpc_in(char *ptr, size_t eltsize,
>  	return size;
>  }
>  
> -static int run_slot(struct active_request_slot *slot,
> -		    struct slot_results *results)
> +static void report_rpc_error(struct slot_results *results)
>  {
> -	int err;
> -	struct slot_results results_buf;
> -
> -	if (!results)
> -		results = &results_buf;
> -
> -	err = run_one_slot(slot, results);
> -
> -	if (err != HTTP_OK && err != HTTP_REAUTH) {
> -		struct strbuf msg = STRBUF_INIT;
> -		if (results->http_code && results->http_code != 200)
> -			strbuf_addf(&msg, "HTTP %ld", results->http_code);
> -		if (results->curl_result != CURLE_OK) {
> -			if (msg.len)
> -				strbuf_addch(&msg, ' ');
> -			strbuf_addf(&msg, "curl %d", results->curl_result);
> -			if (curl_errorstr[0]) {
> -				strbuf_addch(&msg, ' ');
> -				strbuf_addstr(&msg, curl_errorstr);
> -			}
> +	struct strbuf msg = STRBUF_INIT;
> +	if (results->http_code && results->http_code != 200)
> +		strbuf_addf(&msg, "HTTP %ld", results->http_code);
> +	if (results->curl_result != CURLE_OK) {
> +		if (msg.len)
> +			strbuf_addch(&msg, ' ');
> +		strbuf_addf(&msg, "curl %d", results->curl_result);
> +		if (curl_errorstr[0]) {
> +			strbuf_addch(&msg, ' ');
> +			strbuf_addstr(&msg, curl_errorstr);
>  		}
> -		error(_("RPC failed; %s"), msg.buf);
> -		strbuf_release(&msg);
>  	}
> -
> -	return err;
> +	error(_("RPC failed; %s"), msg.buf);
> +	strbuf_release(&msg);
>  }
>  
>  static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
> @@ -899,13 +886,19 @@ static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
>  	curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
>  	curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buf);
>  
> -	err = run_slot(slot, results);
> +	err = run_one_slot(slot, results);
>  
>  	curl_slist_free_all(headers);
>  	strbuf_release(&buf);
>  	return err;
>  }
>  
> +static int can_retry_rpc(const struct rpc_state *rpc)
> +{
> +	return !strcmp(rpc->service_name, "git-upload-pack") ||
> +	       !strcmp(rpc->service_name, "git-upload-archive");
> +}
> +
>  /*
>   * If flush_received is true, do not attempt to read any more; just use what's
>   * in rpc->buf.
> @@ -913,12 +906,15 @@ static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
>  static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
>  {
>  	struct active_request_slot *slot;
> +	struct slot_results results = { 0 };
>  	struct curl_slist *headers = NULL;
>  	int use_gzip = rpc->gzip_request;
>  	char *gzip_body = NULL;
>  	size_t gzip_size = 0;
>  	int err, large_request = 0;
>  	int needs_100_continue = 0;
> +	int can_retry = can_retry_rpc(rpc);
> +	size_t attempt = 0;
>  	struct rpc_in_data rpc_in_data;
>  
>  	/* Try to load the entire request, if we can fit it into the
> @@ -941,15 +937,18 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
>  	}
>  
>  	if (large_request) {
> -		struct slot_results results;
> -
>  		do {
>  			err = probe_rpc(rpc, &results);
>  			if (err == HTTP_REAUTH)
>  				http_reauth_prepare(0);
> -		} while (err == HTTP_REAUTH);
> -		if (err != HTTP_OK)
> +		} while (err == HTTP_REAUTH ||
> +			 (can_retry && err == HTTP_RATE_LIMITED &&
> +			  !http_retry_wait(results.http_code,
> +					   results.retry_after, ++attempt)));
> +		if (err != HTTP_OK) {
> +			report_rpc_error(&results);
>  			return -1;
> +		}
>  
>  		if (results.auth_avail & CURLAUTH_GSSNEGOTIATE || http_auth.authtype)
>  			needs_100_continue = 1;
> @@ -998,10 +997,11 @@ retry:
>  
>  	} else if (gzip_body) {
>  		/*
> -		 * If we are looping to retry authentication, then the previous
> -		 * run will have set up the headers and gzip buffer already,
> -		 * and we just need to send it.
> +		 * If we are looping to retry, then the previous run will
> +		 * have set up the gzip buffer already, and we just need to
> +		 * send it.
>  		 */
> +		headers = curl_slist_append(headers, "Content-Encoding: gzip");
>  		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
>  		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, cast_size_t_to_curl_off_t(gzip_size));

Nice catch here.  The code down below before it jumps back to retry:
label called curl_slist_free_all(headers) and we have added the default
one, but haven't added back Content-Encoding, so we need to add it here.

> @@ -1066,14 +1066,20 @@ retry:
>  
>  
>  	rpc->any_written = 0;
> -	err = run_slot(slot, NULL);
> -	if (err == HTTP_REAUTH && !large_request) {
> -		http_reauth_prepare(0);
> +	err = run_one_slot(slot, &results);
> +	if (!large_request && !rpc->any_written &&
> +	    (err == HTTP_REAUTH ||
> +	     (can_retry && err == HTTP_RATE_LIMITED &&
> +	      !http_retry_wait(results.http_code, results.retry_after, ++attempt)))) {
> +		if (err == HTTP_REAUTH)
> +			http_reauth_prepare(0);
>  		curl_slist_free_all(headers);
>  		goto retry;
>  	}
> -	if (err != HTTP_OK)
> +	if (err != HTTP_OK) {
> +		report_rpc_error(&results);
>  		err = -1;
> +	}

Deferring report_rpc_error() until after the retry loops and guarding
with !large_request and !rpc->any_written ensures we neither print
spurious "RPC failed" errors for recovered attempts nor retry after
partial response data has been forwarded to rpc->in.

The variable result is initialized at the beginning of the function just
once, but is not reset before we retry.  Would it cause issues if
run_one_slot() returns early due to start_active_slot() failing (which I
presume would not cause finish_active_slot() to be called on it to
update slot's results)?  Just wondering if we need to somehow clear to
give a fresh results object to run_one_slot() here.



  reply	other threads:[~2026-11-24 23:04 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-31 15:57 [PATCH 0/4] http: exponential backoff, configurable status codes, and post_rpc() retries Ravi Mistry via GitGitGadget
2026-10-31 15:57 ` [PATCH 1/4] http: fix retry count and Retry-After timing Ravi Mistry via GitGitGadget
2026-11-03  1:03   ` Junio C Hamano
2026-11-03 19:05     ` Ravi Mistry
2026-10-31 15:57 ` [PATCH 2/4] http: add exponential backoff without Retry-After Ravi Mistry via GitGitGadget
2026-11-03 13:21   ` Junio C Hamano
2026-11-03 19:05     ` Ravi Mistry
2026-10-31 15:57 ` [PATCH 3/4] http: add http.retryStatusCodes config option Ravi Mistry via GitGitGadget
2026-11-03 13:41   ` Junio C Hamano
2026-11-03 19:06     ` Ravi Mistry
2026-10-31 15:57 ` [PATCH 4/4] remote-curl: retry transient HTTP errors in post_rpc() Ravi Mistry via GitGitGadget
2026-11-03 14:47   ` Junio C Hamano
2026-11-03 18:57 ` [PATCH v2 0/4] http: exponential backoff, configurable status codes, and post_rpc() retries Ravi Mistry via GitGitGadget
2026-11-03 18:57   ` [PATCH v2 1/4] http: fix retry count and Retry-After timing Ravi Mistry via GitGitGadget
2026-11-03 18:57   ` [PATCH v2 2/4] http: add exponential backoff without Retry-After Ravi Mistry via GitGitGadget
2026-11-04 22:05     ` brian m. carlson
2026-11-05  0:10       ` Ravi Mistry
2026-11-05 17:45         ` Junio C Hamano
2026-11-05 22:32         ` brian m. carlson
2026-11-06 16:27           ` Ravi Mistry
2026-11-03 18:57   ` [PATCH v2 3/4] http: add http.retryStatusCodes config option Ravi Mistry via GitGitGadget
2026-11-03 20:56     ` Junio C Hamano
2026-11-03 18:57   ` [PATCH v2 4/4] remote-curl: retry transient HTTP errors in post_rpc() Ravi Mistry via GitGitGadget
2026-11-07  3:40 ` [PATCH v3 0/5] http: exponential backoff, configurable status codes, and post_rpc() retries Ravi Mistry via GitGitGadget
2026-11-07  3:40   ` [PATCH v3 1/5] http: fix retry count and Retry-After timing Ravi Mistry via GitGitGadget
2026-11-18 12:00     ` Patrick Steinhardt
2026-11-18 21:52       ` Ravi Mistry
2026-11-07  3:40   ` [PATCH v3 2/5] http: add exponential backoff without Retry-After Ravi Mistry via GitGitGadget
2026-11-18 12:00     ` Patrick Steinhardt
2026-11-18 21:53       ` Ravi Mistry
2026-11-07  3:40   ` [PATCH v3 3/5] http: add http.retryStatusCodes config option Ravi Mistry via GitGitGadget
2026-11-18 12:00     ` Patrick Steinhardt
2026-11-18 21:53       ` Ravi Mistry
2026-11-19 11:28         ` Patrick Steinhardt
2026-11-19 12:23           ` Ravi Mistry
2026-11-07  3:40   ` [PATCH v3 4/5] remote-curl: retry transient HTTP errors in post_rpc() Ravi Mistry via GitGitGadget
2026-11-18 12:01     ` Patrick Steinhardt
2026-11-18 21:53       ` Ravi Mistry
2026-11-07  3:40   ` [PATCH v3 5/5] http: default http.retryAfter to 10 seconds Ravi Mistry via GitGitGadget
2026-11-18 12:01     ` Patrick Steinhardt
2026-11-18 21:54       ` Ravi Mistry
2026-11-07  4:37   ` [PATCH v3 0/5] http: exponential backoff, configurable status codes, and post_rpc() retries Junio C Hamano
2026-11-10  3:58     ` Ravi Mistry
2026-11-19 15:57 ` [PATCH v4 " Ravi Mistry via GitGitGadget
2026-11-19 15:57   ` [PATCH v4 1/5] http: fix retry count and Retry-After timing Ravi Mistry via GitGitGadget
2026-11-24 22:23     ` Junio C Hamano
2026-11-19 15:58   ` [PATCH v4 2/5] http: add exponential backoff without Retry-After Ravi Mistry via GitGitGadget
2026-11-24 22:31     ` Junio C Hamano
2026-11-19 15:58   ` [PATCH v4 3/5] http: add http.retryStatusCodes config option Ravi Mistry via GitGitGadget
2026-11-24 22:44     ` Junio C Hamano
2026-11-25 15:13       ` Ravi Mistry
2026-11-19 15:58   ` [PATCH v4 4/5] remote-curl: retry transient HTTP errors in post_rpc() Ravi Mistry via GitGitGadget
2026-11-24 23:04     ` Junio C Hamano [this message]
2026-11-25 15:13       ` Ravi Mistry
2026-11-19 15:58   ` [PATCH v4 5/5] http: default http.retryAfter to 10 seconds Ravi Mistry via GitGitGadget
2026-11-24 23:04   ` [PATCH v4 0/5] http: exponential backoff, configurable status codes, and post_rpc() retries Junio C Hamano
2026-11-25 17:48 ` [PATCH v5 " Ravi Mistry via GitGitGadget
2026-11-25 17:48   ` [PATCH v5 1/5] http: fix retry count and Retry-After timing Ravi Mistry via GitGitGadget
2026-11-25 17:48   ` [PATCH v5 2/5] http: add exponential backoff without Retry-After Ravi Mistry via GitGitGadget
2026-11-25 17:48   ` [PATCH v5 3/5] http: add http.retryStatusCodes config option Ravi Mistry via GitGitGadget
2026-11-25 17:48   ` [PATCH v5 4/5] remote-curl: retry transient HTTP errors in post_rpc() Ravi Mistry via GitGitGadget
2026-11-25 17:48   ` [PATCH v5 5/5] http: default http.retryAfter to 10 seconds Ravi Mistry via GitGitGadget
2026-11-25 19:04   ` [PATCH v5 0/5] http: exponential backoff, configurable status codes, and post_rpc() retries Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq4id5kf19.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=ps@pks.im \
    --cc=rmistry@google.com \
    --cc=sandals@crustytoothpaste.net \
    --cc=vaidas.pilkauskas@shopify.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.