test_opengraph_render_thread.py
python
sha256:0af3bf6553a7c16ddb72e82d4880bb33123098add75155868f5a5af1f1401723
fix(opengraph): run Playwright's sync API on one dedicated …
Opus 5.5
minor
⚠ breaking
2 days ago
| 1 | """The Playwright renderer runs every sync-API call on ONE dedicated thread and bounds each render. |
| 2 | |
| 3 | Production (2026-11-14): renders went through ``asyncio.to_thread`` — successive calls landed on different pool threads, but Playwright's |
| 4 | sync API is bound to the thread that created it, so renders failed ("render failed" 503s, repeated Chromium launches) or hung forever. |
| 5 | A hung render is a request task that never finishes: draining uvicorn workers waited on "background tasks" indefinitely. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import asyncio |
| 10 | import threading |
| 11 | import time |
| 12 | |
| 13 | import pytest |
| 14 | |
| 15 | from musehub.services import opengraph_renderer as R |
| 16 | from musehub.services.opengraph_cards import OpenGraphCardContext |
| 17 | |
| 18 | CTX = OpenGraphCardContext(owner="o", repo_slug="r", description="d", domain_id=None, total_commits=1, fork_count=0, open_issue_count=0, |
| 19 | owner_identity_hex="00", identity_type="human", avatar_png_b64="") |
| 20 | |
| 21 | |
| 22 | async def test_every_render_runs_on_the_same_dedicated_thread(monkeypatch: pytest.MonkeyPatch) -> None: |
| 23 | seen: list[tuple[int, str]] = [] |
| 24 | |
| 25 | def fake_screenshot(html: str) -> bytes: |
| 26 | seen.append((threading.get_ident(), threading.current_thread().name)) |
| 27 | time.sleep(0.01) |
| 28 | return b"png" |
| 29 | |
| 30 | monkeypatch.setattr(R, "_screenshot_html_sync", fake_screenshot) |
| 31 | out = await asyncio.gather(*(R.PlaywrightOpenGraphRenderer().render(CTX) for _ in range(12))) |
| 32 | assert out == [b"png"] * 12 |
| 33 | assert len({tid for tid, _ in seen}) == 1, seen |
| 34 | assert seen[0][1].startswith("opengraph-render") |
| 35 | assert threading.get_ident() not in {tid for tid, _ in seen} # never on the event loop thread |
| 36 | |
| 37 | |
| 38 | async def test_a_stuck_render_times_out_and_later_renders_still_work(monkeypatch: pytest.MonkeyPatch) -> None: |
| 39 | calls = {"n": 0} |
| 40 | |
| 41 | def screenshot(html: str) -> bytes: |
| 42 | calls["n"] += 1 |
| 43 | if calls["n"] == 1: |
| 44 | time.sleep(0.5) # stuck (in production: until Playwright's page timeout frees the thread) |
| 45 | return b"png" |
| 46 | |
| 47 | monkeypatch.setattr(R, "_screenshot_html_sync", screenshot) |
| 48 | monkeypatch.setattr(R, "_RENDER_TIMEOUT_S", 0.1) |
| 49 | with pytest.raises(asyncio.TimeoutError): |
| 50 | await R.PlaywrightOpenGraphRenderer().render(CTX) |
| 51 | monkeypatch.setattr(R, "_RENDER_TIMEOUT_S", 5.0) |
| 52 | assert await R.PlaywrightOpenGraphRenderer().render(CTX) == b"png" # the thread recovers once the stuck call returns |
| 53 | |
| 54 | |
| 55 | def test_render_bounds_are_set() -> None: |
| 56 | assert 0 < R._PAGE_TIMEOUT_MS / 1000 < R._RENDER_TIMEOUT_S |
| 57 | assert R._RENDER_THREAD._max_workers == 1 |
File History
1 commit
sha256:0af3bf6553a7c16ddb72e82d4880bb33123098add75155868f5a5af1f1401723
fix(opengraph): run Playwright's sync API on one dedicated …
Opus 5.5
minor
⚠
2 days ago