Skip to content

script: Create a FontFace's promise when it's first used - #50064

Open
rogerkorantenng wants to merge 1 commit into
servo:mainfrom
rogerkorantenng:fontface-promise-oom
Open

rogerkorantenng wants to merge 1 commit into
servo:mainfrom
rogerkorantenng:fontface-promise-oom

Conversation

@rogerkorantenng

@rogerkorantenng rogerkorantenng commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Creating a FontFace close to the recursion limit crashed Servo, because Promise::new asserts when SpiderMonkey can't create the promise.

My first version made the constructor throw, but the spec's constructor never throws. Gecko and Blink don't create the [[FontStatusPromise]] in the constructor at all. They create it the first time loaded or load() needs it, so I've done the same here. If the font has already loaded or failed by then, the new promise is settled right away.

The promise is created with a new Promise::try_new. loaded and load() return promises, so if creating it fails, the bindings hand script a rejected promise instead of crashing.

Testing: the new crash test (fontface-too-much-recursion-crash.html) crashes on main and passes with this change, and the repro from #50063 passed 60 runs in a row. Two css-font-loading tests used to end in ERROR because of an unhandled rejection from a FontFace nobody used. They're OK now, same as in Chrome and Firefox. Tidy, clippy, crown and the script unit tests pass too.

Fixes: #50063

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

Copy link
Copy Markdown
Contributor

This approach looks fine to me, but I am interested in what @jdm and @mrobinson think.

[Exposed=(Window,Worker), Pref="dom_fontface_enabled"]
interface FontFace {
constructor(CSSOMString family, (CSSOMString or BufferSource) source,
[Throws] constructor(CSSOMString family, (CSSOMString or BufferSource) source,

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.

This does not throw in the specification, so is it right to modify the WebIDL to cause it to throw...

@rogerkorantenng rogerkorantenng Nov 19, 2026 •

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.

Good point. I looked at how Gecko and Blink handle this, and neither creates the promise in the constructor. They create it the first time loaded or load() is used, then resolve or reject it based on the current status. I'll do the same here and drop [Throws] from the constructor, so it matches the spec.

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.

Would you be willing to come to my office hours next week to discuss this?

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.

Throws is not part of WebIDL and our own custom thing afaik

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.

Ah, good to know!

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.

Would you be willing to come to my office hours next week to discuss this?

@mrobinson Thank you, I am really looking forward to it! I am not free next week, but I will be available the week after this week (from 30 November). Please tell me the day and time that suits you. I am in Accra, which is UTC+0.

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.

You can find the office hours and days at #general > Office Hours @ 💬

Creating a FontFace close to the recursion limit crashed Servo, because
Promise::new asserts when SpiderMonkey can't create the promise. Like
Gecko and Blink, the [[FontStatusPromise]] is now created the first time
loaded or load() needs it, so the constructor can't fail, as the spec
says. If the font has already loaded or failed by then, the promise is
settled straight away.

Creating it uses a new Promise::try_new, which returns an error instead
of asserting. Promise-returning members turn that error into a rejected
promise, so script sees a rejected promise instead of a crash.
Promise::new works the same as before.

This also fixes two css-font-loading tests that used to end with an
unhandled rejection from a FontFace that was never used.

Fixes servo#50063

Signed-off-by: rogerkorantenng <[email protected]>
@rogerkorantenng rogerkorantenng changed the title script: Throw instead of panicking when a FontFace can't create its promise script: Create a FontFace's promise when it's first used Nov 24, 2026
Comment on lines +271 to +273
if obj.is_null() {
return;
}

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.

Let's instead make create_js_promise return Result<(), Error>, return Err(Error::JSFailed) here, and then the caller can just do: Promise::create_js_promise(cx, obj.handle_mut())?; and move the assert into the caller.

[Exposed=(Window,Worker), Pref="dom_fontface_enabled"]
interface FontFace {
constructor(CSSOMString family, (CSSOMString or BufferSource) source,
[Throws] constructor(CSSOMString family, (CSSOMString or BufferSource) source,

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.

You can find the office hours and days at #general > Office Hours @ 💬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-review There is new code that needs to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intermittent crash in /css/css-font-loading/fontfaceset-worker-fontface-crash.html

5 participants