Skip to content

script: Support safely using Promise objects from DOM Drop implementations - #42023

Open
jdm wants to merge 1 commit into
servo:mainfrom
jdm:promise-finalize
Open

jdm wants to merge 1 commit into
servo:mainfrom
jdm:promise-finalize

Conversation

@jdm

@jdm jdm commented Jan 20, 2026

Copy link
Copy Markdown
Member

Per #26488, we cannot safely interact with arbitrary DOM objects from a DOM object's Drop implementation, because the finalizer might be called during JS runtime teardown when the runtime is just sweeping all objects regardless of liveness. A common solution to that issue is using weak references to DOM objects, which allows us to observe if another DOM object is still valid when we want to use it.

Trying to use Promises from Drop implementations has the exact same problem, except that we can't use our DOM weak references implementation for them because they are native SpiderMonkey objects. Instead, this PR introduces an opt-in system that is more coarse-grained than per-object weak references—once JS runtime teardown begins, any conforming Drop implementation loses access to any Promise object it was holding on to. This shouldn't lead to any observable behaviour difference because we're already tearing down the world in this case, so promise reactions would never get a chance to run.

Testing: The timing of shutdown and the difficulty of observing the affected behaviour means that this particular fix is theoretical. However, existing tests validate the non-shutdown behaviour.
Fixes: part of #26488

@jdm
jdm requested review from gterzian and sagudev as code owners January 20, 2026 02:51
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Jan 20, 2026
@jdm

jdm commented Jan 20, 2026

Copy link
Copy Markdown
Member Author

This is a prerequisite for fixing #36897. FYI @willypuzzle

@jdm
jdm force-pushed the promise-finalize branch from c686f08 to df59b31 Compare January 20, 2026 02:55
@sagudev

sagudev commented Jan 20, 2026

Copy link
Copy Markdown
Member

Is JSRuntime teardown the only case of running finalizers. AFAIK if GC (assume the big one that also does sweep) detects GPUBuffer and the promise as unreachable it will mark and sweep them, thus calling finalizers/drop of only GPUBuffer and the promise (idk about the order, but I think it can be any) but not on JSRuntime.

Also if order of finalization is really not determined, then GPUBuffer can be dropped before JSRuntime.

@jdm

jdm commented Jan 20, 2026

Copy link
Copy Markdown
Member Author

Ugh. My thinking was that this situation would only happen during teardown, because the GPUBuffer keeps the promise alive otherwise so the GPUBuffer would always be finalized first. If that is not something we can rely upon then I'm not sure what to do.

@sagudev

sagudev commented Jan 20, 2026

Copy link
Copy Markdown
Member

While I was checking out this PR and SM internals I stumbled upon FinalizationRegistery, which allows to call callback after GC (finalization is done). So my idea is what if finalization just marks stuff as invalid and then actual drop happens in as part of FinalizationRegistry callback (so we need to somehow keep it alive for the time). Now it would be too much to do this for all dom_struct, but we only need to do for what is called now DroppableStruct. I think this should be possible by writing some wrapper type for deferred finalization that implements trait of Drop and CustomTrait that has invalidate method. I think mozilla also notion of detached DOM object.

In this concrete case, finalization of promise would make it None, GPUBuffer would do nothing on finalization and on finalization callback (this is after GC) we would drop GPUBuffer (promise is null so we cannot resolve/reject it).

@sagudev

sagudev commented Jan 20, 2026

Copy link
Copy Markdown
Member

actually it might be enough to just mark if promise is finalized, if it is not we can still safely resolve it? (or is this forbiden)?

@jdm

jdm commented Jan 20, 2026

Copy link
Copy Markdown
Member Author

Time to go look at #35868 again.

@sagudev

sagudev commented Jan 20, 2026

Copy link
Copy Markdown
Member

Time to go look at #35868 again.

well we might not need actual finalization registry specified by web, just the stuff that SM exposes that makes it possible to impl.

@sagudev

sagudev commented Jan 21, 2026 •

Copy link
Copy Markdown
Member

I asked on matrix for conformation and it's not safe to do any SM interaction in finalizers and order of which finalizer run are not determined: https://matrix.to/#/!CZEbuMfGYVdQMIBKeP:mozilla.org/$GjQElpCl0FjG-qGgi6q_inl8LgKZ1xPosPNqiDVOEro?via=mozilla.org&via=matrix.org&via=igalia.com

gecko has DeferredFinalizer: https://searchfox.org/firefox-main/search?q=+DeferredFinalize&path=&case=false&regexp=false but I think this is more low level than what we want/need.

@sagudev

sagudev commented Jan 23, 2026

Copy link
Copy Markdown
Member

Hm, if GPUBuffer is dropped in finalizers, then promise drop will also ran as part of same finalizer contrary to the comment in:

// Promise objects are stored inside Rc values, so Drop is run when the last Rc is dropped,

or is there another Rc clone I am not counting?

Anyway, given that promises are permarooted (cannot be GCed before holders) we could reuse same mechanism here (meaning we need to Rc) or we just mark (in bindings.conf) GPUBuffer for delay drop, that is on finalize we would add it to some array of delayed elements (not sure where this array needs to live) and then drop the content of the array in HostCleanupFinalizationRegistryCallback.

In HostCleanupFinalizationRegistryCallback we can use cx stuff, but we still cannot access stuff behind Dom, but that's okay as we only manipulate promise, so we only need to delay destruction of Droppable substruct.

@sagudev

sagudev commented Jul 9, 2026

Copy link
Copy Markdown
Member

GPUBuffer do not need interacting with promise in drop anymore, so I guess we can close this?

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.

3 participants