Skip to content

Bound the time and bytes read when checking if a page embeds the client - #1037

Merged
karenrasmussen merged 3 commits into
mainfrom
fix/embed-detector-bounded-read
Oct 20, 2026
Merged

karenrasmussen merged 3 commits into
mainfrom
fix/embed-detector-bounded-read

Conversation

@santicomp2014

Copy link
Copy Markdown
Contributor

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 with Response.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):

single line size iter_lines() time
0.5 MB 0.19s
1 MB 0.79s
2 MB 3.16s

The existing safeguards did not help:

  • timeout=1.5 in requests is per socket read, not a total deadline.
  • max_lines_to_check = 5000 does not help when the problem is one huge line.
  • Because the worker is killed before the function returns, @cached never stores the result, so every hit repeats the work.

Fix

  • Scan the response with iter_content(chunk_size=64 KiB) instead of iter_lines(), matching the markers as bytes.
  • Stop after MAX_BYTES_TO_CHECK (1 MiB) or MAX_SECONDS_TO_CHECK (3s) in total, whichever comes first.
  • Keep a small overlap between chunks so markers split across chunk boundaries are still found.

The function now always returns quickly, so negative results are cached as intended.

Testing

  • Updated the existing tests to mock iter_content instead of iter_lines.
  • New tests for the byte limit, the deadline, a marker split across chunks, and the chunk size used.
  • Regression test: an 8 MiB single-line page is processed in under a second.
  • tox -e tests equivalent on Python 3.11: 88 passed, 100% coverage. black, isort and flake8 clean.

Not done yet

  • Not deployed and not to be merged yet. This is a draft for review and for correlating with the New Relic / Cloudflare data.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NLJTVvww9qDoZCG3rPw8FY

santicomp2014 and others added 2 commits October 6, 2026 12:25
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
karenrasmussen marked this pull request as ready for review October 14, 2026 16:32
Comment thread bouncer/embed_detector.py Outdated
CHUNK_SIZE = 64 * 1024

#: Never read more than this many bytes of a page.
MAX_BYTES_TO_CHECK = 1024 * 1024

@karenrasmussen karenrasmussen Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
karenrasmussen merged commit 02b9661 into main Oct 20, 2026
5 checks passed
@karenrasmussen
karenrasmussen deleted the fix/embed-detector-bounded-read branch October 20, 2026 15:26
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.

2 participants