Skip to content

Keep PDF CPU work off the worker event loop - #606

Merged
suguanYang merged 2 commits into
mainfrom
fix/wangbinqi/pdf-main-loop-blocking
Dec 4, 2026
Merged

suguanYang merged 2 commits into
mainfrom
fix/wangbinqi/pdf-main-loop-blocking

Conversation

@suguanYang

Copy link
Copy Markdown
Contributor

Summary

Production v1.2.76 workers stalled the gevent loop for up to 20 s on large PDFs. The worst stall was page-screen tiling and PNG encoding on the hub thread. While it ran, every greenlet stopped: lease renewal (20 s), the heartbeat (45 s stale), and the other 9 tasks.

 ContactSheets.grids
   run_in_child_process(_render_pages_worker)        # child, unchanged
-  for sheet in sheets: _tile(sheet).save(png)       # hub thread, 40-50 s on 804 pages
-  base64(png)
+  run_in_child_process(_tile_grids_worker)          # child writes PNGs
+  base64(read PNG file)                             # hub thread, ~0.5 s
 _probe_visual_assets(page)
   bboxes = image rects
-  table search
-  drawing clustering
+  if not bboxes: table search          # only has_asset reads this geometry
+  if not bboxes: drawing clustering
 @worker wrapper (spawned child only)
+  os.nice(+10)                         # event loop wins CPU when 2 vCPUs are saturated
+  report child_cpu_ms in the done log

Observation stays in logs, at WARNING level, with no alerts:

Event When
worker.event_loop_blocked (new EventLoopMonitor) Hub blocked ≥ 2 s; one log per stall, with blocked_ms and the blocking stack
worker.runtime_pressure Now carries max_gevent_delay_ms, the worst delay since the last rate-limited warning
PyMuPDF child done log Now carries child_cpu_ms

Evidence

Replayed locally on the original production PDFs, in a 2 vCPU / 4 GiB container with no network. Each scenario ran release code b0d6cab against this branch.

  • Before: page screen, 804 pages, 90 sheets. Main-thread CPU 41.2–50.9 s, max gevent delay 41.1–51.4 s (3 runs).
    After: main-thread CPU 0.39–0.63 s, max gevent delay 0.23–0.37 s. VLM input sha256 identical across all runs.
  • Before: release code plus only the monitor logged one worker.event_loop_blocked per stall (40,596 / 47,419 / 41,743 ms). The stack ended in PIL ImageFile._encode_tile → encoder.encode.
    After: 0 stall logs.
  • Before: asset probe child CPU 131.8 / 168.1 / 164.4 s (560 / 804 / 1,153 pages).
    After: 107.4 / 135.2 / 139.5 s (−15 to −20 %). Per-page has_asset results are hash-identical.
  • Before: 1,153-page render overlapping a 560-page probe, pinned to 2 CPUs. Main-thread load got 0.65 of a core.
    After: 0.97 of a core, children at niceness 10. A prod worker task reports nproc=2 with no CFS quota, so this matches production.
  • Tests: 35 passed, covering the new contract tests for the monitor, the heartbeat max delay, child niceness and CPU reporting, child tiling, and probe short-circuits. Ruff passes, and pyright reports 0 errors on the changed modules.

Merge Danger

Door: two-way

No schema, queue, or message format changes. Reverting restores the previous behavior.

Blast Radius: worker

  • Tiling now queues for one of the two PyMuPDF child slots, so page screen can take longer on a busy worker. Queue time is outside the child timeout. The tile timeout is max(120 s, 2 s × sheets).
  • table_count is now 0 on pages that already have an image or table. It is serialized only and read by no decision.
  • Children yield CPU to the event loop under saturation, so PDF stages may run a few seconds longer when both CPUs are busy.
  • Celery dispatch is unchanged: tasks are still taken per free gevent slot, not by CPU. Load-aware admission is deferred until pymupdf_queue_wait_ms shows a cross-worker imbalance.

🤖 Generated with Claude Code

Page-screen tiling and PNG encoding ran on the gevent hub and stalled
every greenlet for 40-50 s on an 804-page PDF, starving lease renewal
and the heartbeat. Tiling now runs in the PyMuPDF child process.

- Skip the table search and drawing clustering in the asset probe once
  a page already has an asset; only has_asset reads that geometry.
- Run PyMuPDF children at nice +10 so the event loop is scheduled first
  when the task's two CPUs are saturated.
- Log event-loop stalls over 2 s with the blocking stack, keep the worst
  gevent delay between rate-limited pressure warnings, and report child
  CPU time. All observations are WARNING logs; no alerts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…has a side-effect'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@suguanYang
suguanYang merged commit 78dd49a into main Dec 4, 2026
8 of 9 checks passed
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