fix(api): run native document conversion off the Node.js event loop - #4433
Open
abimaelmartell wants to merge 1 commit into
Open
fix(api): run native document conversion off the Node.js event loop#4433abimaelmartell wants to merge 1 commit into
abimaelmartell wants to merge 1 commit into
Conversation
convert_document_to_markdown was a synchronous #[napi] fn, so the await at its call site was a no-op: every DOCX/ODT/RTF/XLSX/PPTX/EPUB conversion ran on the Node.js event loop of whichever pod executed the scrape, including API app pods running sync scrapes in-process. Same bug class as the PDF one fixed in e33e1f6. Convert it to an async fn that offloads to tokio's blocking pool, mirroring process_pdf/detect_pdf, and add a regression test that asserts the event loop stays responsive during a large conversion.
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
convert_document_to_markdowninapps/api/native/src/document.rswas a synchronous#[napi]fn, so theawaitat its call site in the document engine was a no-op. Every DOCX/ODT/RTF/XLSX/PPTX/EPUB conversion ran directly on the Node.js event loop of whichever pod executed the scrape — including API app pods that run sync scrapes in-process — freezing every unrelated in-flight request for the duration of the conversion.This is the same bug class fixed for PDFs in e33e1f6 (
processPdf/detectPdf).Fix
convert_document_to_markdowntopub async fnthat offloads the CPU-bound conversion to tokio's blocking thread pool viatokio::task::spawn_blocking, mirroring the pattern inapps/api/native/src/pdf.rs. The binding now takes an ownedUint8Arrayand returnsPromise<string>.awaited, so it needed no change; the direct calls in the document-converter snips test are now awaited.Tests
apps/api/src/scraper/scrapeURL/engines/document/__tests__/nativeBinding.test.ts, modeled on the PDF one: generates a ~20MB RTF (no fixtures) whose conversion takes ~1s of CPU and asserts max event-loop lag stays under 250ms. With the old synchronous binding, lag ≈ the full conversion time; with this fix it measures ~2ms.🤖 Generated with Claude Code
Summary by cubic
Makes native document conversion (DOCX/ODT/RTF/XLSX/PPTX/EPUB) run off the Node.js event loop so large conversions no longer freeze unrelated in-flight requests.
Bug Fixes
awaitat the engine call site was a no-op and the full conversion blocked the event loop.spawn_blocking, matching the PDFprocessPdf/detectPdffix.Uint8Arrayand returnsPromise<string>; the engine call site already awaited and needed no change.Written for commit ce2be95. Summary will update on new commits.