Skip to content

webgpu: Proper cleanup in GPUBuffer - #46415

Merged
sagudev merged 5 commits into
servo:mainfrom
sagudev:buffer-clean
Jul 15, 2026
Merged

sagudev merged 5 commits into
servo:mainfrom
sagudev:buffer-clean

Conversation

@sagudev

@sagudev sagudev commented Jul 9, 2026

Copy link
Copy Markdown
Member

In first commit we remove drop from DataView (JSTraceable should have no drop). This is safe because on GC SM will take care for calling freeFunc of externalArrayBuffer. For manual destruction (detach array buffer) we simply pass &mut JSContext. In second commit we add droppable struct.

Testing: Should be covered by WebGPU CTS.
Fixes: #45504
Fixes: #44640

sagudev added 4 commits July 9, 2026 18:56
Signed-off-by: sagudev <[email protected]>
Signed-off-by: sagudev <[email protected]>
Signed-off-by: sagudev <[email protected]>
// we need to pop one by one so we can root one by one for detach
while let Some(DataView { buffer, .. }) = self.data_views.pop() {
rooted!(&in(cx) let b = unsafe { buffer.underlying_object().get() });
assert!(unsafe { DetachArrayBuffer(cx, b.handle()) })

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This carries many important lessons:

  • destruct can be useful (so drop should be prevented not dummy implemented)
  • containers are hard
    • esp when you can get content out of them (pop, drain), cut it's not rooted really and it's not typed to be rooted/unrooted.
      • so removing crown is hard, but we could do this using own container types (would that cover all cases?)

#[dom_struct]
pub(crate) struct GPUBuffer {
reflector_: Reflector,
#[derive(JSTraceable, MallocSizeOf)]

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.

I noticed other instances in the codebase where structs derives JSTraceable, don't contain any field that needs to be traced by mozjs and are filled with #[no_trace], could we avoid it?

@sagudev sagudev Jul 9, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As I said many times I hate what JSTraceable is and I do plan to work on JSManaged which should be more fine grained. As for why we do this is simply because this is currently the only to ensure all fields that needed are traced (w.r.t adding new fields in the future - future proofing). If we make droppable field #[no_trace] and if one adds JSManaged (read type with actual tracer that is not nop) field into droppable struct it would not be traced. Now the proper solution is to somehow bubble the JSManaged property (if type contains JSMnaged type then itself must be jsmanaged). Why this does not work with JSTraceable? Because non JSManaged types are also traceable with nop tracers (like rust primitive types). And that is the main rational behind JSManaged trait/derive I am planning (apart from forbidding drops).

@sagudev
sagudev marked this pull request as ready for review July 10, 2026 04:01
@sagudev
sagudev requested a review from gterzian as a code owner July 10, 2026 04:01
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Jul 10, 2026
@sagudev
sagudev requested a review from jdm July 10, 2026 04:01

@jdm jdm left a comment

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.

We just need

to be cleaned up and this can merge.

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Jul 14, 2026
Signed-off-by: sagudev <[email protected]>
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Jul 15, 2026
@sagudev
sagudev enabled auto-merge July 15, 2026 03:34
@sagudev
sagudev added this pull request to the merge queue Jul 15, 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 Jul 15, 2026
Merged via the queue into servo:main with commit ba00cbe Jul 15, 2026
34 checks passed
@sagudev
sagudev deleted the buffer-clean branch July 15, 2026 07:31
@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 Jul 15, 2026
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.

webgpu: Investigate proper cleanup in GPUBuffer Remove drop implementaiton for GPUBuffer

4 participants