Repository navigation
Bound the time and bytes read when checking if a page embeds the client - #1037
Merged
Merged
Conversation
page_embeds_client() used Response.iter_lines(), which is quadratic on long lines. A page with a multi-megabyte single line took ~30s of CPU per request, tying up a worker until it was killed, so the result was never cached either. Scan the response in 64 KiB chunks instead, stop after 1 MiB or 3s in total, and keep a small overlap so markers split across chunks are still found. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NLJTVvww9qDoZCG3rPw8FY
Cover the byte limit, the total deadline, markers split across chunks, and add a regression test that an 8 MiB single-line page is processed in under a second. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NLJTVvww9qDoZCG3rPw8FY
karenrasmussen
marked this pull request as ready for review
October 14, 2026 16:32
| CHUNK_SIZE = 64 * 1024 | ||
|
|
||
| #: Never read more than this many bytes of a page. | ||
| MAX_BYTES_TO_CHECK = 1024 * 1024 |
Contributor
There was a problem hiding this comment.
1 MB may be too low: a large page with a late snippet would report that the client isn't embedded purely because we stopped reading. Would it make sense to bump this to 2 MB? Reading more bytes is cheap now and MAX_SECONDS_TO_CHECK still caps how long we spend on a page either way.
karenrasmussen
approved these changes
Oct 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Bouncer (hyp.is) went down on 2026-10-06 around 12:00 (UTC-3).
From ~2026-10-04 19:00, BingBot started crawling hyp.is links for annotations on
correlacional.metodos-facso.org/assignment/07-practico.html. Every one of those requests took ~30.6s and was killed by the worker timeout (~120-140/hour, 4,951 of ~5,000 from BingBot). Average Bouncer response time went from ~0.3s to ~0.8s; DB and external time were unchanged, so the time was spent in Python.The cause is
page_embeds_client(), which downloads the annotated page and scans it withResponse.iter_lines().iter_lines()reads 512 bytes at a time and re-joins its pending buffer on every read, so it is quadratic on long lines. That page is ~8.2 MB of HTML with a single ~3.8 MB line (a self-contained export with inlined images):iter_lines()timeThe existing safeguards did not help:
timeout=1.5inrequestsis per socket read, not a total deadline.max_lines_to_check = 5000does not help when the problem is one huge line.@cachednever stores the result, so every hit repeats the work.Fix
iter_content(chunk_size=64 KiB)instead ofiter_lines(), matching the markers as bytes.MAX_BYTES_TO_CHECK(1 MiB) orMAX_SECONDS_TO_CHECK(3s) in total, whichever comes first.The function now always returns quickly, so negative results are cached as intended.
Testing
iter_contentinstead ofiter_lines.tox -e testsequivalent on Python 3.11: 88 passed, 100% coverage.black,isortandflake8clean.Not done yet
🤖 Generated with Claude Code
https://claude.ai/code/session_01NLJTVvww9qDoZCG3rPw8FY