Truly independent web browser
LibWeb: Drop retired clock lanes outside the render state lock
Andreas Klingf172ce03ee11
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock.rs b/Libraries/LibWeb/Rust/src/render_state/clock.rsindex d07fda237fc..aa71371800e 100644--- a/Libraries/LibWeb/Rust/src/render_state/clock.rs+++ b/Libraries/LibWeb/Rust/src/render_state/clock.rs@@ -63,7 +63,7 @@ pub(crate) mod hover;mod lane;pub(crate) use hover::PendingPointer;pub(crate) use lane::LaneDelivery;-pub(super) use lane::{LaneSlot, note_host_write, seal_plan, take_lanes_in};+pub(super) use lane::{LaneSlot, note_host_write, retire_slot, seal_plan, take_lanes_in};/// What the ticks of a lane sample, sealed at the end of the lane's frame's rendering update.pub(crate) struct ClockPlan {diff --git a/Libraries/LibWeb/Rust/src/render_state/clock/lane.rs b/Libraries/LibWeb/Rust/src/render_state/clock/lane.rsindex 868240860b5..36e5fdc0cdc 100644--- a/Libraries/LibWeb/Rust/src/render_state/clock/lane.rs+++ b/Libraries/LibWeb/Rust/src/render_state/clock/lane.rs@@ -133,6 +133,19 @@ enum FramePlan {Sealed(ClockPlan),}+/// Lets go of `lanes`, whose forks copy the document's whole render state: they are dropped once the owner no longer+/// holds the render states, which the host may be waiting for.+fn retire(lanes: Lanes) {+ owner::drop_after_hold(lanes);+}++/// On the render owner: drops the slot of a retired document, whose lanes it lets go of.+pub(in crate::render_state) fn retire_slot(slot: Option<LaneSlot>) {+ if let Some(mut slot) = slot {+ retire(std::mem::take(&mut slot.lanes));+ }+}+/// What hands the lane of a frame the pieces the Paint thread makes, once it presented the frame.pub(crate) struct LaneDelivery {ticks: Arc<ClockTicks>,@@ -195,7 +208,10 @@ pub(in crate::render_state) fn note_sampled(ticks: &Arc<ClockTicks>) -> LaneDelilane.state.move_on();Some(lane)}- Lanes::Coming { earlier, .. } => earlier,+ Lanes::Coming { pieces, earlier } => {+ retire(Lanes::Coming { pieces, earlier: None });+ earlier+ }Lanes::None => None,};slot.lanes = Lanes::Coming {@@ -295,7 +311,9 @@ impl LaneSlot {if let Lanes::Coming { pieces, earlier } = &mut self.lanes {pieces.recorder = Some(recorder);// The frame presented, and the presenter takes no frame of the lane of an earlier one after it.- *earlier = None;+ if let Some(earlier) = earlier.take() {+ retire(Lanes::Newest(earlier));+ }self.come_together();}}@@ -303,7 +321,7 @@ impl LaneSlot {/// Has the frame `frame`, from which no lane starts, end the lanes: a move waits for nothing more.fn forget_coming_lane(&mut self, frame: u64) {if frame == self.sampled && matches!(self.lanes, Lanes::Coming { .. }) {- self.lanes = Lanes::None;+ self.retire_lanes();}}@@ -338,10 +356,15 @@ impl LaneSlot {// One that seals no plan leaves a lane that showed no sample following nothing: a later update that presents// no frame of its own has it follow its plan again, which a lane that went away could not.(Lanes::Newest(lane), None) if lane.samples_nothing() => lane.unplan(),- (_, _) => self.lanes = Lanes::None,+ (_, _) => self.retire_lanes(),}}+ /// Lets go of the lanes.+ fn retire_lanes(&mut self) {+ retire(std::mem::take(&mut self.lanes));+ }+/// Has the lane of the frame sampled last take the place of the lane before it, once its pieces have come with a/// plan. The lanes presented nothing since the rendering update that sampled the frame took them in, so the frame/// shows all they did, and a move that waits is the new lane's first tick's to hover.@@ -354,8 +377,13 @@ impl LaneSlot {recorder: Some(recorder),plan: FramePlan::Sealed(plan),},- ..- } => Lane::new(recorder, plan, state),+ earlier,+ } => {+ if let Some(earlier) = earlier {+ retire(Lanes::Newest(earlier));+ }+ Lane::new(recorder, plan, state)+ }// A frame with no plan yet has no lane: the moves are the host's.Lanes::Coming {pieces:@@ -364,8 +392,11 @@ impl LaneSlot {plan: FramePlan::None,..},- ..+ earlier,} => {+ if let Some(earlier) = earlier {+ retire(Lanes::Newest(earlier));+ }self.lanes = Lanes::Coming { pieces, earlier: None };return;}diff --git a/Libraries/LibWeb/Rust/src/render_state/owner.rs b/Libraries/LibWeb/Rust/src/render_state/owner.rsindex 4d89278db3d..326f06575dc 100644--- a/Libraries/LibWeb/Rust/src/render_state/owner.rs+++ b/Libraries/LibWeb/Rust/src/render_state/owner.rs@@ -93,6 +93,15 @@ static SLOT: LazyLock<Slot> = LazyLock::new(|| Slot {thread_local! {/// How many holds of the slot the calling thread is in, which a host callback of a job takes again.static HOLDS: Cell<u32> = const { Cell::new(0) };+ /// What the calling thread let go of while it held the slot, which it drops once it no longer holds it.+ static DROPPED_AFTER_HOLD: RefCell<Vec<Box<dyn std::any::Any>>> = const { RefCell::new(Vec::new()) };+}++/// Drops `value`, which the calling thread let go of while it holds the slot, once its outermost hold ends: a value+/// that takes long to drop, such as a fork of a whole render state, does not keep the threads waiting for the slot.+pub(super) fn drop_after_hold(value: impl std::any::Any) {+ debug_assert!(is_held_here(), "only a thread holding the slot defers a drop past it");+ DROPPED_AFTER_HOLD.with_borrow_mut(|values| values.push(Box::new(value)));}/// Whether the calling thread holds the slot: it runs a job on the render states, or a host callback of one.@@ -107,8 +116,19 @@ pub(super) fn hold<R>(job: impl FnOnce() -> R) -> R {impl Drop for Holding {fn drop(&mut self) {HOLDS.set(HOLDS.get() - 1);- if self.0.is_some() {- crate::stage_thread::tsan::release(&SLOT.tsan_key);+ let Some(guard) = self.0.take() else {+ return;+ };+ crate::stage_thread::tsan::release(&SLOT.tsan_key);+ drop(guard);+ // NB: Taken out before dropping them, since a drop may hold the slot again.+ let dropped = DROPPED_AFTER_HOLD.take();+ // A lane's drop resumes the panic of the recording it waits for, which a thread that is already unwinding+ // cannot survive: such a thread leaks what it let go of instead.+ if std::thread::panicking() {+ std::mem::forget(dropped);+ } else {+ drop(dropped);}}}@@ -190,7 +210,7 @@ pub(super) fn retire(document: DocumentId) {hold(|| {if let Some(entry) = with_states(|states| states.remove(&document)) {let DocumentEntry { state, lanes } = *entry;- drop(lanes);+ super::clock::retire_slot(lanes.into_inner());state.into_inner().retire();}});diff --git a/Meta/Linters/check_rust_process_wide_state.py b/Meta/Linters/check_rust_process_wide_state.pyindex e5bbc899d18..62d7479cc63 100755--- a/Meta/Linters/check_rust_process_wide_state.py+++ b/Meta/Linters/check_rust_process_wide_state.py@@ -136,6 +136,10 @@ RENDER_STATE_ALLOWED = {"per-thread count of the holds of the render owner's slot the thread is in",["render_state/owner.rs:HOLDS"],),+ **render_state_entries(+ "per-thread list of what the thread let go of in its hold of the render owner's slot, dropped as the hold ends",+ ["render_state/owner.rs:DROPPED_AFTER_HOLD"],+ ),f"{RENDER_STATE_CRATE}/src/render_state.rs:STACK_BASE": "per-thread cache of the thread's own stack bounds",f"{RENDER_STATE_CRATE}/src/stage_thread.rs:UNFINISHED_HANDED": "per-thread count of the jobs the thread made for each stage thread that have yet to run",**render_state_entries(