WeKnora icon indicating copy to clipboard operation
WeKnora copied to clipboard

Document detail page freezes then crashes (Page unresponsive / SIGTRAP) on a large .xlsx — client-side excel preview renders the entire workbook with no size guard

Open vicfei opened this issue 2 months ago • 3 comments

Environment

  • WeKnora v0.8.0, docker-compose deployment
  • Browser: 360 (Chromium-based) on UOS; same behavior expected on any Chromium browser
  • Code path re-verified unchanged on current main

Description

Uploading a large spreadsheet (9.28 MB .xlsx → 24,283 chunks after parsing) produces a document whose detail page cannot be opened:

  • Opening the document detail drawer renders the metadata header (file type, chunk count "24283 个片段") but the content area stays completely blank, and within seconds the browser shows the "Page unresponsive" dialog (main thread blocked).
  • After dismissing / reloading, the next attempt kills the renderer: the tab shows "喔唷,崩溃啦!错误代码:SIGTRAP" (renderer-process crash, typically memory exhaustion).
  • The behavior is identical while the document is still parsing and after parsing completes — it does not depend on ingestion state.
  • The paginated chunks API itself is fine (25 per page, KNOWLEDGE_CHUNK_PAGE_SIZE); the problem is the document-content/preview area of the detail drawer.

Excel row-based chunking makes this easy to hit for spreadsheet-heavy knowledge bases.

Root cause (code, current main)

  1. frontend/src/utils/filePreview.ts:49 registers xlsx as previewable kind excel, and the detail drawer defaults file-type previewable documents to the preview view (the details.id watch in doc-content.vue).
  2. frontend/src/components/document-preview.vue renderExcel() (~:241) then, with no size/row guard of any kind:
    • XLSX.read(arrayBuffer) — parses the entire workbook client-side (SheetJS);
    • workbook.SheetNames.forEach(...) — converts every sheet, all rows to HTML via sheet_to_html (~:262-266), producing a tens-of-MB <table> string for a 24K-row workbook;
    • sanitizeHTML(html) over the whole string, then a single v-html injection (~:583) — hundreds of thousands of DOM nodes at once.

Main thread blocks first (the unresponsive dialog), then the renderer runs out of memory (SIGTRAP).

Expected behavior

  • Above a threshold (file size or estimated row/sheet count), the preview should degrade gracefully: show a notice like "file too large to preview" with the existing download action, or render only the first N rows with an explicit truncation notice.
  • Metadata (chunk count, parse status) must stay reachable without rendering the content.

Steps to reproduce

  1. Create a KB, upload a data-dense .xlsx (~9 MB / 24K rows).
  2. While it parses — or after it completes — open the document detail page.
  3. Content area stays blank → "Page unresponsive"; retry → renderer crash (SIGTRAP).

Impact

UX only — backend, retrieval and the paginated chunks API are unaffected. But any spreadsheet-heavy KB has effectively un-viewable documents, and the crashed tab makes them look broken.

Suggested fix

Add a guard in renderExcel() (and the preview-kind dispatch): refuse or truncate above a size/row threshold with a notice + download fallback; optionally virtualize for large sheets.

vicfei avatar Sep 22 '26 15:09 vicfei

Additional reproduction and a locally validated mitigation: small XLSX files can trigger this too when formatting inflates the worksheet range. A file-size-only guard would miss this case.

Environment

  • Windows 11, native source build / Lite web UI, based on commit a89a5a2c43dec80df19ee79dc9412122048afe71 (v0.8.0), with local customizations.
  • The same renderExcel() path (SheetNames.forEach → unrestricted sheet_to_html → whole-workbook sanitization / v-html) was also checked on upstream's default branch when posting this comment.

Observed real-file cases (business contents omitted)

Workbook Compressed file size Parsed chunks Inflated worksheet range Actual value extent on that sheet
A 657,908 bytes 707 A1:XFD833 833 rows × 42 columns
B 1,306,057 bytes 1,416 A1:BI1048576 547 rows × 39 columns

Both documents had completed ingestion. Opening their default Excel preview froze the browser. Chunk retrieval is already paginated at 25 per request; the expensive path here is original-file preview. The declared worksheet ranges make HTML conversion iterate millions of mostly empty cells.

Minimal synthetic fixture

This generates a tiny workbook with two populated cells and formatting on a distant empty cell:

from openpyxl import Workbook
from openpyxl.styles import PatternFill

wb = Workbook()
ws = wb.active
ws['A1'] = 'Preview test'
ws['B2'] = 'Data'
ws['BI1048576'].fill = PatternFill(fill_type='solid', fgColor='FFFF00')
wb.save('spreadsheet-inflated-range.xlsx')

The resulting worksheet XML dimension was verified as A1:BI1048576. Upload and open the original-file preview to exercise the problematic range. The unpatched UI was not rerun against this synthetic fixture; the reported freezes came from the two real workbooks above.

Locally validated fix approach

  • Parse the workbook in a Web Worker so parsing does not block the UI thread.
  • Determine bounds from cells with actual values rather than trusting !ref or formatting-only cells; preserve valid zero/false values.
  • Render only the selected worksheet and a bounded page (tested with 50 rows × 25 columns), with row/column navigation. Avoid converting the entire workbook to HTML first.
  • Terminate obsolete workers when the component is disposed or its source changes; ignore stale replies. Bound oversized cell text as well.

After this local frontend change, both real workbooks opened successfully in headless Edge 153 on the same machine, in approximately 4.5 s and 2.5 s including page initialization. Worksheet switching and row/column paging passed without page errors. Relevant regression tests: 21 passed. These are local validation results, not an upstream fix or a general performance benchmark.

No original business files or document contents are attached.

EthanBird avatar Sep 23 '26 05:09 EthanBird

Opened an implementation PR: #3613 — fix(frontend): paginate spreadsheet previews in a web worker.

It adds off-thread parsing, value-based worksheet bounds, worksheet selection, row/column pagination, cancellation/timeouts, localized messages and a synthetic-data screenshot. The original files and backend parsing/indexing are unchanged.

Validation: 31 focused tests, 13 locale checks, type-check and the production build passed. Browser smoke tests passed on both affected workbooks, including worksheet changes, row/column paging and worker disposal. The full frontend suite has four failures also reproduced on the unchanged base; details and commands are in the PR.

I noticed the existing #3592 proposal and explicitly linked it in #3613. The new PR is an alternative that preserves paginated browsing instead of only rejecting previews above a threshold. Maintainers can choose the preferred approach. No business files or contents were uploaded.

EthanBird avatar Sep 23 '26 06:09 EthanBird

Still reproducible on the main-branch images (2026-11-16 build, docker-compose). frontend/src/utils/filePreview.ts and renderExcel() in frontend/src/components/document-preview.vue (sheet_to_html over all sheets) are unchanged, so the original analysis still holds.

Fresh reproduction data (only sizes shared; no file will be attached):

  • .xlsx file size: ~25 MB, ~200k rows
  • extracted text volume: ~69.7M characters
  • GET /api/v1/knowledge/{id}/preview response body: ~25 MB (the entire raw file)

Two additional observations:

  1. The guard needs to cover the payload too, not only the render loop: the preview endpoint returns the whole raw binary (see response size above), so the tab blocks even before renderExcel() starts rendering.
  2. Parse-failed documents hit the same path — the detail drawer defaults to the preview view, and users opening the drawer to check the failure reason are exactly the ones who trigger the freeze.

FYI @EthanBird's #3613 was closed unmerged — is a different fix approach planned, or would reopening some form of it make sense?

heaton0987 avatar Nov 17 '26 08:11 heaton0987