{"record":{"id":"18f505549b7db55c","repo":"arduino/Arduino","slug":"exceeded-s-redirects","errorCode":null,"errorMessage":"Exceeded %s redirects.","messagePattern":"Exceeded (.+?) redirects\\.","errorType":"exception","errorClass":"TooManyRedirects","httpStatus":null,"severity":"error","filePath":"arduino-core/src/processing/app/i18n/python/requests/sessions.py","lineNumber":101,"sourceCode":"                          verify=True, cert=None, proxies=None):\n        \"\"\"Receives a Response. Returns a generator of Responses.\"\"\"\n\n        i = 0\n        prepared_request = PreparedRequest()\n        prepared_request.body = req.body\n        prepared_request.headers = req.headers.copy()\n        prepared_request.hooks = req.hooks\n        prepared_request.method = req.method\n        prepared_request.url = req.url\n        cookiejar = resp.cookies\n\n        # ((resp.status_code is codes.see_other))\n        while (('location' in resp.headers and resp.status_code in REDIRECT_STATI)):\n\n            resp.content  # Consume socket so it can be released\n\n            if i >= self.max_redirects:\n                raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects)\n\n            # Release the connection back into the pool.\n            resp.close()\n\n            url = resp.headers['location']\n            method = prepared_request.method\n\n            # Handle redirection without scheme (see: RFC 1808 Section 4)\n            if url.startswith('//'):\n                parsed_rurl = urlparse(resp.url)\n                url = '%s:%s' % (parsed_rurl.scheme, url)\n\n            # Facilitate non-RFC2616-compliant 'location' headers\n            # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')\n            if not urlparse(url).netloc:\n                # Compliant with RFC3986, we percent encode the url.\n                url = urljoin(resp.url, requote_uri(url))\n","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/arduino/Arduino/blob/a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee/arduino-core/src/processing/app/i18n/python/requests/sessions.py#L83-L119","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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).","Ensure cookies/session state are preserved (use requests.Session, correct domain/path) so auth-gated redirects stop.","Raise the limit if the chain is legitimately long: session.max_redirects = 50.","Use allow_redirects=False and handle 3xx manually if you need custom redirect logic."],"exampleFix":"// before\ns = requests.Session()\nr = s.get(url)  # TooManyRedirects (loop)\n// after\ns = requests.Session()\nr = s.get(url, allow_redirects=False)\nprint(r.status_code, r.headers.get('location'))  # diagnose the loop first","handlingStrategy":"validation","validationCode":"seen = set()\ncurrent = url\nfor _ in range(session.max_redirects + 1):\n    if current in seen:\n        raise ValueError('Redirect loop detected at %s' % current)\n    seen.add(current)\n    r = requests.get(current, allow_redirects=False)\n    if r.status_code not in (301, 302, 303, 307, 308):\n        break\n    current = r.headers['location']","typeGuard":null,"tryCatchPattern":"from requests.exceptions import TooManyRedirects\ntry:\n    resp = session.get(url, timeout=10)\nexcept TooManyRedirects:\n    log.error('Redirect loop on %s; check scheme/cookies/server config', url)\n    raise","preventionTips":["Use one Session so cookies persist across redirects (auth loops often stem from lost cookies).","Fix http->https redirect loops on the server (HSTS, single scheme).","Log each hop with allow_redirects=False when debugging.","Don't lower max_redirects below ~10 without a reason."],"tags":["http","redirect","requests","loop","network"],"backgroundTag":"too-many-redirects","analyzedSha":"a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee","analyzedAt":"2026-09-06T10:13:38.901Z","contentChangedAt":"2026-09-06T10:13:38.901Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}