Skip to content

Make the caches shared between containers thread safe - #302

Open
eXpl0it3r wants to merge 1 commit into
masterfrom
fix/regex-cache-thread-safety
Open

eXpl0it3r wants to merge 1 commit into
masterfrom
fix/regex-cache-thread-safety

Conversation

@eXpl0it3r

Copy link
Copy Markdown
Collaborator

Description

The layout tests of the integration project have been flaky, roughly one in three full runs failed in a random LayoutHarness test with "Operations that change non-concurrent collections must have exclusive access". The tests run in parallel, and every container shares the static regex cache and the process-wide adapter, whose caches are plain dictionaries without any locking. The same applies to anyone rendering on several threads, e.g. generating PDFs on a server.

  • RegexParserUtils caches its regexes in a ConcurrentDictionary
  • FontsHandler takes a lock around its public methods
  • The brush and @font-face caches of RAdapter are locked, if two containers load the same face at the same time, the first one is kept
  • Pens are cached per color, width and dash style and never changed after that

The last one also fixes borders being drawn with the wrong width: BordersDrawHandler set Width and DashStyle on the one shared pen of a color, so a parallel render could draw a 2px border with 1px. As a side effect a double, groove etc. border no longer inherits whatever dash style the pen had last, it's solid.

The PdfSharp FontResolver has the same kind of unlocked lists, the PDF tests don't run in parallel, so that's left for later.

How to test this PR?

CI should succeed. Locally nine full integration runs in a row passed, where one in three failed before. PenCacheTests covers the pens, the border test fails with the old BordersDrawHandler.

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