Repository navigation
Conversation
|
This is a prerequisite for fixing #36897. FYI @willypuzzle |
…tions. Signed-off-by: Josh Matthews <[email protected]>
|
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. |
|
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. |
|
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). |
|
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)? |
|
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. |
|
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®exp=false but I think this is more low level than what we want/need. |
|
Hm, if GPUBuffer is dropped in finalizers, then promise drop will also ran as part of same finalizer contrary to the comment in: servo/components/script/dom/promise.rs Line 82 in 5d16bd6 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. |
|
GPUBuffer do not need interacting with promise in drop anymore, so I guess we can close this? |
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