#46716·servo

Borrow hazard in KeyframeEffect::GetKeyframes

Author: jdmCreated Jul 22, 2026Updated Sep 30, 2026
LabelsA-content/cssE-less-complexI-panic

https://book.servo.org/design-documentation/script/garbage-collection-and-refcell.html#garbage-collection-and-refcell

https://github.com/servo/servo/blob/9ce8b655ef05de4679f74b85714b157fdc7543ca/components/script/dom/animations/keyframeeffect.rs#L145 https://github.com/servo/servo/blob/9ce8b655ef05de4679f74b85714b157fdc7543ca/components/script/dom/animations/keyframeeffect.rs#L174

The following diff makes the compiler complain:

diff
diff --git a/components/script/dom/animations/keyframeeffect.rs b/components/script/dom/animations/keyframeeffect.rs
index f3ba0e1150a..31ed24c523a 100644
--- a/components/script/dom/animations/keyframeeffect.rs
+++ b/components/script/dom/animations/keyframeeffect.rs
@@ -142,7 +142,7 @@ impl KeyframeEffectMethods<crate::DomTypeHolder> for KeyframeEffect {
         cx: &mut JSContext,
         result: &mut RootedVec<'_, Box<Heap<*mut JSObject>>>,
     ) -> Fallible<()> {
-        let mut layout = self.window.layout_mut();
+        let mut layout = self.window.layout_mut2(cx.no_gc());
         let stylist = layout.stylist_mut();

         // Step 1. Let result be an empty sequence of objects.
diff --git a/components/script/dom/window/window.rs b/components/script/dom/window/window.rs
index 4e7fd6f191c..d42a09f608c 100644
--- a/components/script/dom/window/window.rs
+++ b/components/script/dom/window/window.rs
@@ -292,7 +292,7 @@ pub(crate) struct Window {
     script_chan: Sender<MainThreadScriptMsg>,
     #[no_trace]
     #[ignore_malloc_size_of = "TODO: Add MallocSizeOf support to layout"]
-    layout: RefCell<Box<dyn Layout>>,
+    layout: DomRefCell<Box<dyn Layout>>,
     navigator: MutNullableDom<Navigator>,
     crypto: MutNullableDom<Crypto>,
     #[no_trace]
@@ -515,6 +515,10 @@ impl Window {
         self.layout.borrow()
     }

+    pub(crate) fn layout_mut2<'a, 'b: 'a>(&'b self, no_gc: &'a NoGC) -> RefMut<'a, Box<dyn Layout>> {
+        self.layout.safe_borrow_mut(no_gc)
+    }
+
     pub(crate) fn layout_mut(&self) -> RefMut<'_, Box<dyn Layout>> {
         self.layout.borrow_mut()
     }
@@ -3883,7 +3887,7 @@ impl Window {
             caches: Default::default(),
             ongoing_navigation: Default::default(),
             script_chan,
-            layout: RefCell::new(layout),
+            layout: DomRefCell::new(layout),
             image_cache_sender,
             navigator: Default::default(),
             crypto: Default::default(),
error[E0502]: cannot borrow `*cx` as mutable because it is also borrowed as immutable
   --> components/script/dom/animations/keyframeeffect.rs:174:64
    |
145 |         let mut layout = self.window.layout_mut2(cx.no_gc());
    |                                                  -- immutable borrow occurs here
...
174 |             rooted!(&in(cx) let mut output_keyframe = unsafe { JS_NewObject(cx, ptr::null()) });
    |                                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
...
240 |     }
    |     - immutable borrow might be used here, when `layout` is dropped and runs the destructor for type `RefMut<'_, std::boxed::Box<dyn layout_api::Layout>>`

error[E0502]: cannot borrow `*cx` as mutable because it is also borrowed as immutable
   --> components/script/dom/animations/keyframeeffect.rs:175:13
    |
145 |         let mut layout = self.window.layout_mut2(cx.no_gc());
    |                                                  -- immutable borrow occurs here
...
175 |             base_keyframe.to_jsobject(cx, output_keyframe.handle_mut());
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
...
240 |     }
    |     - immutable borrow might be used here, when `layout` is dropped and runs the destructor for type `RefMut<'_, std::boxed::Box<dyn layout_api::Layout>>`

error[E0502]: cannot borrow `*cx` as mutable because it is also borrowed as immutable
   --> components/script/dom/animations/keyframeeffect.rs:208:17
    |
145 |         let mut layout = self.window.layout_mut2(cx.no_gc());
    |                                                  -- immutable borrow occurs here
...
208 |                 value_string.safe_to_jsval(cx, value.handle_mut());
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
...
240 |     }
    |     - immutable borrow might be used here, when `layout` is dropped and runs the destructor for type `RefMut<'_, std::boxed::Box<dyn layout_api::Layout>>`

error[E0502]: cannot borrow `*cx` as mutable because it is also borrowed as immutable
   --> components/script/dom/animations/keyframeeffect.rs:218:21
    |
145 |           let mut layout = self.window.layout_mut2(cx.no_gc());
    |                                                    -- immutable borrow occurs here
...
218 | /                     JS_DefineProperty(
219 | |                         cx,
220 | |                         output_keyframe.handle(),
221 | |                         property_name.as_ptr(),
222 | |                         value.handle(),
223 | |                         JSPROP_ENUMERATE as u32,
224 | |                     )
    | |_____________________^ mutable borrow occurs here
...
240 |       }
    |       - immutable borrow might be used here, when `layout` is dropped and runs the destructor for type `RefMut<'_, std::boxed::Box<dyn layout_api::Layout>>`

For more information about this error, try `rustc --explain E0502`.