Skip to content

Avoid infinite loop when cached mirror redirect keeps failing#5435

Open
bdraco wants to merge 6 commits intoplatformio:developfrom
bdraco:fix/mirror-iterator-cache-loop
Open

Avoid infinite loop when cached mirror redirect keeps failing#5435
bdraco wants to merge 6 commits intoplatformio:developfrom
bdraco:fix/mirror-iterator-cache-loop

Conversation

@bdraco
Copy link
Copy Markdown

@bdraco bdraco commented Apr 18, 2026

The registry mirror iterator caches the HEAD redirect for one hour, but the cache hit path never advanced _visited_mirrors, and the cached payload did not include X-PIO-Mirror. When a prior run cached a redirect to a mirror that now fails (for example an SSL error on the download), subsequent iterations kept hitting the same cache key and returning the same failing mirror forever, so tool installs looped until interrupted.

The fix stores X-PIO-Mirror in the cached payload, and on cache hit only reuses the entry if it names a mirror not already tried; otherwise it falls through to a fresh HEAD with bypass set. Stale cache entries written before this change lack X-PIO-Mirror and fall through naturally, so no manual cache clear is required.

Before

Tool Manager: Installing platformio/tool-esptoolpy @ ~1.30000.0
Tool Manager: Error: Please read https://bit.ly/package-manager-ioerror
Tool Manager: Warning! Package Mirror: HTTPSConnectionPool(host='usc1.contabostorage.com', port=443): Max retries exceeded with url: /.../tool-esptoolpy-1.30000.201119.tar.gz (Caused by SSLError(SSLCertVerificationError(...)))
Tool Manager: Looking for another mirror...
Tool Manager: Error: Please read https://bit.ly/package-manager-ioerror
Tool Manager: Warning! Package Mirror: HTTPSConnectionPool(host='usc1.contabostorage.com', port=443): Max retries exceeded with url: /.../tool-esptoolpy-1.30000.201119.tar.gz (Caused by SSLError(SSLCertVerificationError(...)))
Tool Manager: Looking for another mirror...
... (repeats indefinitely against the same host until the user interrupts)

After

Tool Manager: Installing platformio/tool-esptoolpy @ ~1.30000.0
Tool Manager: Error: Please read https://bit.ly/package-manager-ioerror
Tool Manager: Warning! Package Mirror: HTTPSConnectionPool(host='usc1.contabostorage.com', port=443): Max retries exceeded with url: /.../tool-esptoolpy-1.30000.201119.tar.gz (Caused by SSLError(SSLCertVerificationError(...)))
Tool Manager: Looking for another mirror...
Unpacking  [####################################]  100%
Tool Manager: tool-esptoolpy@1.30000.201119 has been installed!

The iterator now advances to a working mirror after the first failure, and if every mirror fails the loop terminates with a clear error instead of spinning forever.

The registry mirror iterator caches the HEAD redirect for one hour,
but the cache hit path never advanced _visited_mirrors, and the
cached payload did not include X-PIO-Mirror. When a prior run cached
a redirect to a mirror that now fails (for example an SSL error on
the download), subsequent iterations kept hitting the same cache key
and returning the same failing mirror forever, so tool installs
looped until interrupted.

Store X-PIO-Mirror in the cached payload, and on cache hit only
reuse the entry if it names a mirror not already tried; otherwise
fall through to a fresh HEAD with bypass set. Stale cache entries
written before this change lack X-PIO-Mirror and fall through
naturally, so the fix self heals without a manual cache clear.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 18, 2026

CLA assistant check
All committers have signed the CLA.

bdraco added 3 commits April 18, 2026 07:44
Cover the cache hit path advancing _visited_mirrors, stale cache
entries without X-PIO-Mirror falling through to a fresh HEAD, and
new HEAD responses persisting X-PIO-Mirror so later runs can tell
whether the cached redirect still names an untried mirror. Each
test fails against the previous iterator implementation.
@bdraco bdraco marked this pull request as ready for review April 18, 2026 12:50
@bdraco
Copy link
Copy Markdown
Author

bdraco commented Apr 26, 2026

This has been a night and day reliability improvement. No more having to delete the cache every few hours

@ivankravets
Copy link
Copy Markdown
Member

Hi Nick,

Thanks for the PR! 🙏 I'll review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants