Skip to content

webgpu: Investigate proper cleanup in GPUBuffer #45504

Closed

Description

@jschwe

I am willing to approve this as we should not be touching any SM objects in finalizers (read drop) anyway. But this is also is not proper solution as we should still do the cleanup (so detaching the buffer) as FF is doing: https://searchfox.org/firefox-main/source/dom/webgpu/Buffer.cpp#146.

I thought maybe we could have trait LazyFinalize: Drop that would cause all such objects to be stored in some array and then dropped from outside. But the problem are subobjects (they might be dead) and realm might also be dead, so we might need to something else (prefinalizer hooks ideally -- are they equal to FinalizeRegistery?)

Originally posted by @sagudev in #45484 (review)

Activity

  1. added
    A-content/webgpu
    on Jun 9, 2026
  2. added
    C-needs-investigation
    on Jun 9, 2026
  3. changed the title [-]webgpu: Investigate proper Cleanup in GPUBuffer[/-] [+]webgpu: Investigate proper cleanup in GPUBuffer[/+] on Jun 9, 2026
  4. sagudev commented on Jun 9, 2026

    @sagudev
    Member

    FinalizeCallbacks are post finalize, but I already considered this here: #42023 (comment) but the proposal is too limited.

    Still we could use the idea of of subobjects. We need two stage GC finalize, so we can get it by two JS objects. First one is just proxy/wrapper for the second one. First one would present actual usage and would be GCed when there is no external references to it anymore. Second one would be permarooted, but the first finalizer would mark it for deletion (global array of such objects) and then outside of GC we would walk the array and do destroy (deteching buffers and stuff) and then unrooted them so next time GC is run it will finalize the second object.

    The only problem here is that permaroots are footgun on their own.

  5. sagudev commented on Jul 9, 2026

    @sagudev
    Member
  6. sagudev commented on Jul 9, 2026

    @sagudev
    Member

    I asked this on matrix and they said:

    When using NewExternalArrayBuffer one does not need to call DetachArrayBuffer as freeFunc will be called at GC. But one can still call DetachArrayBuffer to do like early destruction which will call freeFunc.

    https://matrix.to/#/!CZEbuMfGYVdQMIBKeP:mozilla.org/$YQ1O-zde0nMcpdTt_RsXGetLKuJ0BrdzSDURKSt-rOE?via=mozilla.org&via=matrix.org&via=igalia.com

  7. self-assigned this
    on Jul 9, 2026
  8. moved this from To do to Done in WebGPUon Jul 15, 2026
  9. added a commit that references this issue on Jul 15, 2026
    ba00cbe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-content/webgpuC-needs-investigation

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions