arduino/Arduino · error · TooManyRedirects

Exceeded %s redirects.

Error message

Exceeded %s redirects.

What it means

Session.resolve_redirects follows 3xx responses up to self.max_redirects times (default 30). When the redirect chain exceeds that limit — typically a redirect loop — it raises TooManyRedirects instead of looping forever.

Source

Thrown at arduino-core/src/processing/app/i18n/python/requests/sessions.py:101

                          verify=True, cert=None, proxies=None):
        """Receives a Response. Returns a generator of Responses."""

        i = 0
        prepared_request = PreparedRequest()
        prepared_request.body = req.body
        prepared_request.headers = req.headers.copy()
        prepared_request.hooks = req.hooks
        prepared_request.method = req.method
        prepared_request.url = req.url
        cookiejar = resp.cookies

        # ((resp.status_code is codes.see_other))
        while (('location' in resp.headers and resp.status_code in REDIRECT_STATI)):

            resp.content  # Consume socket so it can be released

            if i >= self.max_redirects:
                raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects)

            # Release the connection back into the pool.
            resp.close()

            url = resp.headers['location']
            method = prepared_request.method

            # Handle redirection without scheme (see: RFC 1808 Section 4)
            if url.startswith('//'):
                parsed_rurl = urlparse(resp.url)
                url = '%s:%s' % (parsed_rurl.scheme, url)

            # Facilitate non-RFC2616-compliant 'location' headers
            # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')
            if not urlparse(url).netloc:
                # Compliant with RFC3986, we percent encode the url.
                url = urljoin(resp.url, requote_uri(url))

View on GitHub (pinned to a0df6e0e83)

Solutions

  1. Inspect the Location header chain (print each resp.url/resp.headers['location'] while iterating) and fix the server-side redirect loop (scheme, trailing slash, cookie logic).
  2. Ensure cookies/session state are preserved (use requests.Session, correct domain/path) so auth-gated redirects stop.
  3. Raise the limit if the chain is legitimately long: session.max_redirects = 50.
  4. Use allow_redirects=False and handle 3xx manually if you need custom redirect logic.

Example fix

// before
s = requests.Session()
r = s.get(url)  # TooManyRedirects (loop)
// after
s = requests.Session()
r = s.get(url, allow_redirects=False)
print(r.status_code, r.headers.get('location'))  # diagnose the loop first
Defensive patterns

Strategy: validation

Validate before calling

seen = set()
current = url
for _ in range(session.max_redirects + 1):
    if current in seen:
        raise ValueError('Redirect loop detected at %s' % current)
    seen.add(current)
    r = requests.get(current, allow_redirects=False)
    if r.status_code not in (301, 302, 303, 307, 308):
        break
    current = r.headers['location']

Try / catch

from requests.exceptions import TooManyRedirects
try:
    resp = session.get(url, timeout=10)
except TooManyRedirects:
    log.error('Redirect loop on %s; check scheme/cookies/server config', url)
    raise

Prevention

When it happens

Trigger: A server that redirects a URL back to itself or cycles A->B->A (often due to HTTP/HTTPS mismatch, missing trailing slash, or cookie-based redirect logic); more than max_redirects chained redirects; calling send() manually with resp.raw not consumed so 'location' headers keep appearing.

Common situations: Auth cookies not being sent so the login page keeps redirecting back; load balancer redirect loops between http and https; sites behind misconfigured CDNs; programmatic use of Session with max_redirects lowered to a small number.

Related errors


AI-assisted analysis of arduino/Arduino@a0df6e0e83 (2026-09-06). Data as JSON: /api/errors/18f505549b7db55c. Report an issue: GitHub.