Skip to content

fix(content): keep a sliced run's interpreter out of every thread's cache while it runs - #585

Merged
michaelstonis merged 2 commits into
spec/33-broadside-v1from
584-sliced-extraction-nre
Nov 18, 2026
Merged

michaelstonis merged 2 commits into
spec/33-broadside-v1from
584-sliced-extraction-nre

Conversation

@michaelstonis

@michaelstonis michaelstonis commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Resolves #584

Cause

ContentInterpreter caches one interpreter per thread ([ThreadStatic] _cached). Rent used to leave the instance in the slot and mark it _busy; Release put it back in the releasing thread's slot. A run in slices (BeginPageRun / ContinuePageRun / EndPageRun, from #546) can end on another thread than the one it began on, because the viewer awaits between slices (ConfigureAwait(false)). After such a run the same instance sat idle in two threads' slots, so two runs on those threads could rent it at the same moment. One run's Release then nulled _context.Document under the other, which is the NullReferenceException at ContentInterpreter.Text.cs:197 (document.ColorSpaces with document null).

What changed

  • ContentInterpreter.Rent takes the interpreter out of the thread's slot (as TextExtractor, DisplayListBuilder and GeometryLayout already do), so an instance is cached by at most one thread and never while it runs.
  • A sliced run is guarded: ContinuePageRun throws InvalidOperationException after the run ended (or after a slice threw) or while another slice of it runs; EndPageRun throws while a slice runs or when the interpreter was already given back. It can no longer silently drive an interpreter that runs another page.
  • DocumentSession.ReadInSlicesAsync counts each slice (begin, step, result) as pending work, so closing the session between slices releases the document only after a running slice returns; the next slice sees the session closed (ObjectDisposedException, as before).

DisplayListBuild uses the same page run and is covered by the same fix.

Tests

  • SlicedRunThreadingTests (Broadside.Tests):
    • a run begun on one thread and ended on another is reused by the ending thread only (deterministic; fails on the base branch with Assert.NotSame),
    • a sliced run is handed thread to thread, then every thread extracts the page at once (fails on the base branch with the issue's exact NullReferenceException in SetFont),
    • continuing after the end, ending twice, and continuing or ending during a running slice are refused,
    • text, layout and display-list builds in slices hopping pool threads beside whole extractions, with abandoned runs, all read the page.
  • SlicedPageModelTests.A_session_closed_between_two_slices_ends_the_load_with_the_closing_and_nothing_else (Broadside.Viewer.Tests): the session is closed from another thread at each of the first 24 yields of a text and an accessible-tree load; each load completes or ends with ObjectDisposedException/OperationCanceledException, and no other exception goes unobserved.

Measurements

  • The Uno (18 tests), Avalonia (39) and WinUI (20) viewer test suites, run back to back 50 times on macOS (16 cores) beside 24 busy-loop CPU burners: 150 of 150 suite runs passed, no failure and no unobserved exception.
  • Before the fix, SlicedRunThreadingTests failed on every run (Assert.NotSame), and the hand-off test hit the issue's NullReferenceException at ContentInterpreter.SetFont (ContentInterpreter.Text.cs:197). With the fix, the class passed 15 runs in a row.
  • No allocation change on the run path: the cache still holds one interpreter per thread.

Notes for review

  • ContentInterpreter is internal: no public API change, no conformance row touched.
  • The WinUI viewer test suite is the platform-free net10.0 one and runs on macOS; the WinUI UI tests need the Windows runner.

…ache while it runs (#584)

A run in slices can end on another thread than the one it began on. The
thread-static interpreter cache kept the instance in the beginning
thread's slot while it ran, so once it was released on the other thread
both threads cached it and two runs could share it; one then read the
other's released state (NullReferenceException in Tf).

Rent now takes the interpreter out of the slot. A sliced run refuses to
continue after it ended, or while another slice of it runs, and the
viewer counts each slice as pending work so closing the session waits
for a running slice before releasing the document.
@michaelstonis
michaelstonis merged commit a5234a9 into spec/33-broadside-v1 Nov 18, 2026
55 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