Repository navigation
Stop the reader at MaxDuration and cancellation while it opens the PDF - #83
Merged
Merged
Conversation
The scanner checked its cancellation token only while copying the input, and MaxDuration only after PdfReader.Open returned, although the docs said the token stopped the open step. A 60 MiB file of 1.5 million objects took 10 s under --timeout 1 and 13 s to cancel after a 200 ms token. An internal ReadInterrupt, passed in PdfReaderOptions, is checked for each lexer token and in the reader's loops that do not lex (cross-reference rebuild, cross-reference stream entries, object stream members, FinalizeReferences, RereadUnicodeStrings). A stop is thrown as the reader's limit refusal, so no repair path takes it for damage; the scanner turns it into OperationCanceledException or 'Scan exceeded MaxDuration.'. The five hostile shapes now stop about 0.1 s after a 1 s limit. Docs: the guide and the MaxDuration comment say both stop the open step, and the guide states that opening keeps about 500 bytes per object in memory.
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
The 7.14.0 docs say that
MaxDurationdoes not stop the reader while it opens a PDF, but the cancellation token does. Neither did:PdfReader.Opentakes no token, and the scanner checked the token only while it copied the input. Sofreepdf-a11y --timeouthad no effect during that step either.Measured on main (fb96aae) with
--timeout 1, on synthetic files within the default 64 MiBMaxFileBytes:A token cancelled 200 ms after the start stopped the no-xref scan after 13,057 ms.
Change
ReadInterrupt, passed to the reader in an internalPdfReaderOptions.Interrupt. No public API changes.FinalizeReferencesandRereadUnicodeStrings. Every 1,024th check asks the scanner whether to stop.OperationCanceledException(token) orScan exceeded MaxDuration.(time).Openreturns.MaxDurationXML comment now say both stop the open step. The guide also states the memory that opening takes: about 500 bytes per object, so millions of small objects in object streams can need several GB withinMaxFileBytes. This PR does not limit that memory.After the change
The 200 ms token now stops the scan after 554 ms. Most of that is reading and hashing the 60 MiB input, which is a single pass.
Tests
MaxDuration_also_stops_the_reader_while_it_opens_the_fileandCancellation_also_stops_the_reader_while_it_opens_the_file, each with and without a cross-reference table. The input is a catalog without a page tree plus 2,000 objects, so the reader fails only after reading them all. The cancellation test cancels the token when the scanner's copy reaches the end of the input. All four fail on fb96aae and pass here. They do not depend on timing.PlatformBuildFileListTestschecks that.The CHANGELOG entry is under "Unreleased", and PROVENANCE.md has an entry for the new code.