Skip to content

Stop the reader at MaxDuration and cancellation while it opens the PDF - #83

Merged
christianz merged 1 commit into
mainfrom
feature/accessibility-interruptible-open-CZ
Nov 19, 2026
Merged

christianz merged 1 commit into
mainfrom
feature/accessibility-interruptible-open-CZ

Conversation

@christianz

Copy link
Copy Markdown
Member

Problem

The 7.14.0 docs say that MaxDuration does not stop the reader while it opens a PDF, but the cancellation token does. Neither did: PdfReader.Open takes no token, and the scanner checked the token only while it copied the input. So freepdf-a11y --timeout had no effect during that step either.

Measured on main (fb96aae) with --timeout 1, on synthetic files within the default 64 MiB MaxFileBytes:

Input File Time Peak RSS
1.5 M small objects, xref table 60.5 MiB 5.0 s 699 MB
the same, no xref (rebuilt) 60.5 MiB 10.1 s 872 MB
3 M objects in object streams 18.9 MiB 5.8 s 1,477 MB
500 k pages 49.9 MiB 6.6 s 877 MB
500 k incremental updates 59.3 MiB 2.6 s 518 MB

A token cancelled 200 ms after the start stopped the no-xref scan after 13,057 ms.

Change

  • New internal ReadInterrupt, passed to the reader in an internal PdfReaderOptions.Interrupt. No public API changes.
  • The lexer checks it for each token. The reader's loops that do not lex check it too: the cross-reference rebuild, cross-reference stream entries, object stream members, FinalizeReferences and RereadUnicodeStrings. Every 1,024th check asks the scanner whether to stop.
  • A stop is thrown as the reader's existing limit refusal, so no repair path takes it for damage. The scanner turns it into OperationCanceledException (token) or Scan exceeded MaxDuration. (time).
  • The reader clears the interrupt from the document's lexer when Open returns.
  • Docs: the guide and the MaxDuration XML 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 within MaxFileBytes. This PR does not limit that memory.

After the change

Input Time Peak RSS
1.5 M small objects, xref table 1.1 s 320 MB
the same, no xref (rebuilt) 1.1 s 483 MB
3 M objects in object streams 1.1 s 434 MB
500 k pages 1.1 s 271 MB
500 k incremental updates 1.1 s 298 MB

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_file and Cancellation_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.
  • Linux, Release, net10.0: all 1,334 regression tests and all 70 accessibility tool tests pass. The test projects build for net8.0, net10.0 and net472. CI runs net8.0, net472 and Windows.
  • The new file is linked into the GDI and WPF projects; PlatformBuildFileListTests checks that.

The CHANGELOG entry is under "Unreleased", and PROVENANCE.md has an entry for the new code.

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.
@christianz
christianz merged commit 7885705 into main Nov 19, 2026
41 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.

1 participant