Repository navigation
fix(content): keep a sliced run's interpreter out of every thread's cache while it runs - #585
Merged
Merged
Conversation
…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.
This was referenced Nov 18, 2026
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.
Resolves #584
Cause
ContentInterpretercaches one interpreter per thread ([ThreadStatic] _cached).Rentused to leave the instance in the slot and mark it_busy;Releaseput 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'sReleasethen nulled_context.Documentunder the other, which is theNullReferenceExceptionatContentInterpreter.Text.cs:197(document.ColorSpaceswithdocumentnull).What changed
ContentInterpreter.Renttakes the interpreter out of the thread's slot (asTextExtractor,DisplayListBuilderandGeometryLayoutalready do), so an instance is cached by at most one thread and never while it runs.ContinuePageRunthrowsInvalidOperationExceptionafter the run ended (or after a slice threw) or while another slice of it runs;EndPageRunthrows 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.ReadInSlicesAsynccounts 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).DisplayListBuilduses the same page run and is covered by the same fix.Tests
SlicedRunThreadingTests(Broadside.Tests):Assert.NotSame),NullReferenceExceptioninSetFont),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 withObjectDisposedException/OperationCanceledException, and no other exception goes unobserved.Measurements
SlicedRunThreadingTestsfailed on every run (Assert.NotSame), and the hand-off test hit the issue'sNullReferenceExceptionatContentInterpreter.SetFont(ContentInterpreter.Text.cs:197). With the fix, the class passed 15 runs in a row.Notes for review
ContentInterpreteris internal: no public API change, no conformance row touched.