feat: checkpoint mount preparation and defer task cancellation to unmount

This commit is contained in:
2026-10-23 01:33:58 +08:00
parent 8f7255f4cf
commit cd07c958c1
2 changed files with 140 additions and 2 deletions
+137 -2
View File
@@ -302,13 +302,17 @@ struct LifecycleCall {
function: ObjectRef,
block: ObjectRef,
mount: ObjectRef,
unmount: bool,
}
impl NativeCallable for LifecycleCall {
fn arity(&self) -> Option<endor_core::CallArity> {
Some(endor_core::CallArity::exact(0))
}
fn invoke(&self, _: &mut NativeContext<'_>, _: &[Value]) -> Result<NativeResult, VmError> {
fn invoke(&self, cx: &mut NativeContext<'_>, _: &[Value]) -> Result<NativeResult, VmError> {
if self.unmount {
crate::tasks::cancel_owner(cx, self.mount)?;
}
Ok(NativeResult::Invoke {
function: self.function,
arguments: vec![Value::Object(self.block), Value::Object(self.mount)],
@@ -336,6 +340,7 @@ fn lifecycle_call(
function,
block,
mount,
unmount: name == "onUnmount",
},
vec![function, block, mount],
))
@@ -491,7 +496,6 @@ impl NativeCallable for Sync {
&& objects.get(id) != Some(&object)
{
let mut mount = cx.store().payload::<Mount>(object, "Mount")?.clone();
crate::tasks::cancel_owner(cx, object)?;
mount.mounted = false;
mount.layout = None;
mount.parent = None;
@@ -530,7 +534,138 @@ impl NativeCallable for Sync {
}
}
/// Explicitly owned roots protect the old presentation while preparation
/// temporarily replaces its desktop dictionary and Mount payloads.
pub struct Checkpoint {
table: MountTable,
dictionary: Option<ObjectRef>,
states: Vec<(ObjectRef, Mount, Option<Value>, Option<Value>)>,
roots: Vec<ObjectRef>,
}
impl Checkpoint {
pub fn release(self, vm: &mut Vm) -> Result<(), VmError> {
let mut result = Ok(());
for root in self.roots.into_iter().rev() {
if let Err(error) = vm.release(root) {
result = Err(error);
}
}
result
}
pub fn restore(
self,
vm: &mut Vm,
desktop: ObjectRef,
table: &mut MountTable,
) -> Result<(), VmError> {
let result = (|| {
let old: std::collections::HashSet<_> =
self.states.iter().map(|state| state.0).collect();
for weak in table.ordered() {
if let Some(object) = vm.upgrade(weak)? {
let result = (|| {
if !old.contains(&object) {
let mut mount = vm.payload::<Mount>(object, "Mount")?.clone();
mount.mounted = false;
mount.layout = None;
mount.parent = None;
mount.first_child = None;
mount.next_sibling = None;
vm.replace_payload(object, mount)?;
vm.set_attr(object, "mounted", Value::Bool(false))?;
}
Ok::<_, VmError>(())
})();
vm.release(object)?;
result?;
}
}
for (object, mount, mounted, version) in &self.states {
vm.replace_payload(*object, mount.clone())?;
for (name, value) in [("mounted", mounted), ("layoutVersion", version)] {
if let Some(value) = value {
vm.set_attr(*object, name, value.clone())?;
} else {
match vm.del_attr(*object, name) {
Ok(()) | Err(VmError::AttributeNotFound(_)) => {}
Err(error) => return Err(error),
}
}
}
}
if let Some(dictionary) = self.dictionary {
vm.set_attr(desktop, "mounts", Value::Object(dictionary))?;
} else {
match vm.del_attr(desktop, "mounts") {
Ok(()) | Err(VmError::AttributeNotFound(_)) => {}
Err(error) => return Err(error),
}
}
*table = self.table.clone();
Ok(())
})();
let released = self.release(vm);
result?;
released
}
}
impl MountTable {
pub fn checkpoint(&self, vm: &mut Vm, desktop: ObjectRef) -> Result<Checkpoint, VmError> {
let dictionary = match vm.get_attr(desktop, "mounts") {
Ok(Value::Object(dictionary)) => Some(dictionary),
Err(VmError::AttributeNotFound(_)) => None,
Ok(_) => return Err(VmError::InvalidArgument("invalid mount dictionary")),
Err(error) => return Err(error),
};
let mut roots = Vec::new();
if let Some(dictionary) = dictionary {
vm.retain(dictionary)?;
roots.push(dictionary);
}
let mut checkpoint = Checkpoint {
table: self.clone(),
dictionary,
states: Vec::new(),
roots,
};
let result = (|| {
for weak in self.ordered() {
if let Some(object) = vm.upgrade(weak)? {
checkpoint.roots.push(object);
let mount = vm.payload::<Mount>(object, "Mount")?.clone();
let mut attributes = Vec::new();
for name in ["mounted", "layoutVersion"] {
let value = match vm.get_attr(object, name) {
Ok(value) => Some(value),
Err(VmError::AttributeNotFound(_)) => None,
Err(error) => return Err(error),
};
if let Some(Value::Object(object)) = &value {
vm.retain(*object)?;
checkpoint.roots.push(*object);
}
attributes.push(value);
}
checkpoint.states.push((
object,
mount,
attributes.remove(0),
attributes.remove(0),
));
}
}
Ok::<_, VmError>(())
})();
if let Err(error) = result {
checkpoint.release(vm)?;
return Err(error);
}
Ok(checkpoint)
}
/// A faulted domain cannot run user onUnmount callbacks. Still detach every
/// context and release its owning dictionary through the normal heap barrier.
pub fn retire(&mut self, vm: &mut Vm, desktop: ObjectRef) -> Result<(), VmError> {