Skip to content

fonts: Give workers their own font context - #50049

Merged
TimvdLippe merged 1 commit into
servo:mainfrom
rogerkorantenng:worker-font-context
Nov 20, 2026
Merged

TimvdLippe merged 1 commit into
servo:mainfrom
rogerkorantenng:worker-font-context

Conversation

@rogerkorantenng

Copy link
Copy Markdown
Contributor

Dedicated and shared workers used to share the page's FontContext. Fonts a worker loaded could end up in the page, and a font loading in a worker made the page's document.fonts.ready wait for it. Now each of these workers gets its own empty FontContext, which matches the spec ("Within a Worker document, the font source is initially empty") and what Firefox does. Fonts loaded with self.fonts are stored there, and their WebRender resources are freed when the worker shuts down, like the layout thread does for pages. Service workers already have their own FontContext, so they're unchanged.

This also fixes loading a second FontFace for a URL that was already loaded for the same family. That failed with "Failed to load font data" on pages too, because script loads skipped URLs that were already loaded.

Testing: a page's document.fonts.ready no longer waits for a slow font in a worker (7 ms instead of 2.7 s). With #49915 applied locally, a worker's canvas draws with the font the worker loaded, and can't see fonts loaded by the page. I also created and closed 20 workers that each load a font and draw text. Two page tests that loaded the same FontFace twice no longer error, and the WPT workers and canvas text tests still pass.

This builds on #50046, so its commits are included here until that one lands.

Part of #49892

@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Nov 18, 2026
@TimvdLippe

Copy link
Copy Markdown
Contributor

To me it seems like this PR is identical to the other one, as this one also has only 1 commit. Does the other PR maybe include both already?

@rogerkorantenng

Copy link
Copy Markdown
Contributor Author

This PR builds on #50046, so it includes its two commits. The new part is the last commit, "fonts: Give workers their own font context". It shows all three now.

Dedicated and shared workers used to share the page's `FontContext`, so
a font loading in a worker made the page's `document.fonts.ready` wait
for it. Now each of these workers gets its own empty `FontContext`, and
fonts loaded with `self.fonts` are stored there. Their WebRender
resources are freed when the worker shuts down.

This also fixes loading a second `FontFace` for a URL that was already
loaded for the same family.

Signed-off-by: rogerkorantenng <[email protected]>
@@ -1,5 +1,4 @@
[2d.text.measure.getActualBoundingBox.tentative.html]
expected: ERROR
[Test TextMetrics::getActualBoundingBox(), with text align left , and 0px letter spacing.]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this test still failing ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These subtests call two canvas methods Servo doesn't have yet: getActualBoundingBox() here, and ctx.shapeText() in the other file. So they fail with "not a function". Both methods are still experimental in the spec.

Before this PR, the test didn't even get that far. Each subtest loads the same font again, and that second load failed, so the whole file errored. This PR fixes the second load, so now only the missing methods fail.

I can open an issue for the missing methods and look into them separately if that's useful.

self.with_module_map(|module_map| std::mem::take(&mut *module_map.borrow_mut()));

// Dedicated and shared workers have their own font context (service workers share one),
// so free the WebRender font resources it created.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a follow-up PR, can you add a memory leak test to tests/wpt/mozilla/tests/memory that creates these workers, loads a fontface and when it tears down, ensures there are no memory leaks?

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Nov 20, 2026
@TimvdLippe
TimvdLippe added this pull request to the merge queue Nov 20, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Nov 20, 2026
Merged via the queue into servo:main with commit 2c70a27 Nov 20, 2026
35 checks passed
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Nov 20, 2026
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.

4 participants