Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

9.2 KB
Raw Blame

Library download/ingestion PDF text extraction is now bounded by three ceilings, defined as module constants in utilities/pdf_extraction_limits: pages walked (MAX_PDF_EXTRACTION_PAGES, 500 per attempt — the download service's PyPDF fallback runs only when pdfplumber produced no text, so one extraction there can walk up to 1,000 pages in total), characters kept (MAX_PDF_EXTRACTED_CHARS, 10,000,000 characters including the separators that join pages; the page that crosses the ceiling contributes the prefix that fits instead of being dropped), and CPU time between pages (MAX_PDF_EXTRACTION_CPU_SECONDS, 600 — one budget per whole extraction, shared by the pdfplumber attempt and its PyPDF fallback, which is not started once the budget is spent). The CPU budget counts only the extracting thread's own CPU time (time.thread_time()), not wall-clock time, so waiting for the GIL or for other users' extractions does not spend it; the switching overhead of GIL contention does count, and the budget is sized not to cut an honest document short under load: a dense 500-page document measured about 146 s of thread CPU alone and about 250-330 s with 4 to 16 extractions contending, leaving about 1.8x margin or more. The budget is checked between pages, so it is not a hard CPU cap: the page-tree parse, the page in progress when it runs out and, when pdfplumber found no text with budget left, the PyPDF fallback's parse all run to completion. A document longer than 500 pages therefore yields at most the text of its first 500 pages — fewer if the character ceiling, or on a much slower host the CPU budget, stops the walk first — and the stored text is not marked as partial. The download service's pdfplumber walk also closes every page it reaches, releasing that page's layout and text-map caches (about 25 MB for a dense page). Closing does not release what the PDF libraries decode and parse for a page: pdfminer and pypdf keep every decoded stream and parsed object for the life of the document, so a crafted page of 20 MB of spaces (about 20 KB compressed, no characters, about 0.17 s of CPU) added about 20 MB per page — about 10 GB at the page ceiling from a 10 MB file. pdfminer also builds every font in a page's resources (and in a Form XObject's, each time the page invokes it), with tables it expands from short ranges: each font parses its /ToUnicode CMap into its own map, so one <0000> <FFFF> range in a ~100-byte compressed CMap that many fonts share becomes 65,536 entries per font. It caches the fonts that are objects of their own and builds those written inline in a resource dictionary again on every use, caching them nowhere; either way the map stays in a reference cycle that only a garbage collection frees. 20 pages of five such fonts held about 840 MB, from an 18 KB file or, with the fonts inline, a 13 KB one. All three walks (pdfplumber, its PyPDF fallback, the base helper) now release those caches once the decoded stream bytes plus an estimate of the parsed objects and, under pdfplumber, of the fonts pdfminer has built reach MAX_PDF_RETAINED_DECODED_BYTES (64 MiB): decoded streams go back to their encoded state, and the parsed objects and fonts the walk added are dropped, to be read or built again from the file if a later page needs them; under pdfplumber the release then runs a full garbage collection (spaced like the ones below, so a release can defer it), because pdfminer's parsers keep what they built (a font's /ToUnicode map and the decoded CMap bytes) in reference cycles that dropping the font does not free — its cost grows with everything the server process holds, about 0.06 s per million objects, and counts against the CPU budget. The estimate counts the decoded stream bytes, every object the walk parsed (including those pdfminer parses out of object streams, and a stream's dictionary, read without decoding the stream) with every array item and dictionary entry nested in it, and every font pdfminer built (counted as its get_font() returns them, cached or inline, each object once) with every element nested in the tables the build stores on it — its /ToUnicode map, its encoding table, its widths (each font builds its own copy of its /Widths array, even one that many fonts share, and a CID font expands its /W and /W2 ranges) and a CID font's vertical displacements, a table that a parsed object or pdfminer still holds (a font's descriptor) counted once — with every string among all of these, dictionary keys included, counted by its size (a CID font joins its /CIDSystemInfo strings into a new one on every build), plus the decoded bytes each build feeds to a parser that pdfminer leaves in a reference cycle, where pdfminer parses them: the /ToUnicode CMap, and the first /Length1 bytes of the /FontFile whose header a Type 1 font without /Encoding (and without built-in metrics) parses from a fresh copy; other memory the libraries hold for the document is not counted by the estimate. On Linux the walks also release once the process's resident memory (/proc/self/statm) has grown by the threshold since the last release, whatever the estimate says; it is the whole process's memory, so growth from other threads can make a release that frees nothing, and on other platforms only the estimate applies. Those parsers are garbage once the build returns, and the tokens a crafted input leaves on their stack hold up to about 170 bytes per decoded byte, so the pdfplumber walk also runs a full garbage collection, without a release and in the middle of a page if need be, once the bytes fed to them since the last one could hold the threshold's worth, when the font tables it holds reach the threshold's worth and are let go of, and on Linux once the resident memory has grown by the threshold since the last collection; such a collection, and a release's, waits until the walking thread has spent nine times the last one's duration since it ended (at most about a tenth of its CPU time), unless the resident memory has grown by four times the threshold; a release that defers its collection still drops the caches. A collection runs at once, however recent the last one, once the garbage that only a collection frees, released since the last one, reaches the threshold: what those parsers were fed (a fresh copy at once, a stream's own decoded bytes once the release rewinds the stream) and the tables of the fonts built with them once those fonts are dropped (an inline font's when its build returns, a cached font's at the release); and a collection still deferred when the walk ends runs then. What those counted structures hold between pages therefore stays near that threshold — crafted PDFs that added 20-60 MB per page peaked about 100-250 MB above the starting memory, and the 840 MB font files (fonts as objects of their own, inline, or inline in a Form XObject) about 35-75 MB, whether the process held no other objects or 8 million, with or without the resident-memory trigger — and honest documents, which stay far below it, are left alone. The release reaches into library internals, written against the pdfplumber, pdfminer.six and pypdf versions pdm.lock pins; if an upgrade removes them the release is skipped, extraction carries on, and canary tests fail. The ceilings are enforced in the download service extractor and in the shared base-downloader extract_text_from_pdf that the library's downloader text branches and the search-time PDF fetcher route through. What is NOT bounded: page-tree materialisation — pdfplumber and pypdf build every page of the document before the first page is yielded, so a file with many pages costs O(all pages) time and memory before any ceiling is consulted, whatever page count it declares; and the cost of a single page — one page's extract_text() runs to completion inside the library with no check point available to the caller, and a few kilobytes of compressed content streams can take minutes or exhaust gigabytes of address space there, and what that page decodes and parses is held until the release after it. Bounding either would require a separate process. Nor is the memory held between releases bounded exactly: parsed objects and font tables are estimated per array item or dictionary entry, so it can exceed the threshold by a small multiple, and a collection that the spacing defers lets the part of the parsers' residue that the worst-case factor counts beyond what each was fed pile up until it runs, for longer the more objects the server process holds (on Linux, at most until the resident memory has grown by four times the threshold). Nor are the names and keywords pdfminer interns: it keeps them in process-wide tables (PSLiteralTable, PSKeywordTable) that it never clears and compares against by identity, so a 2 MB PDF of a million distinct names (or keywords) adds about 175 MB to the server process for good, and every upload of new ones adds more; the honest documents measured added 25 to 243 entries each. The arXiv search engine's inline PDF text extraction, the web upload route's extraction service and the document_loaders-based extraction used by RAG uploads and Zotero sync are not bounded by this change; a companion change (#6472) proposes the same page and character ceilings for the first two.