Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11761-async-destroy-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Keep queued async resource destruction pending until all active execution scopes have emitted their after hooks. This preserves timer callback lifecycle order when an interval clears itself or a collection queues destruction during a nested scope. Add a regression that checks both nested checkpoints, resource retention, the final drain and duplicate-drain prevention.
8 changes: 8 additions & 0 deletions crates/perry-runtime/src/async_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,14 @@ pub(crate) fn gc_destroy_work_pending() -> bool {
}

pub fn drain_gc_destroy_queue() -> i32 {
// A timer's callback checkpoint runs before its `after` hook. Destruction
// queued by clearInterval (or GC) must wait until the enclosing async scope
// and all nested scopes have finished their `after` hooks. Leave the queue
// intact for the next outer checkpoint rather than destroying resources
// while their execution identities are still active.
if EXECUTION_STACK.with(|stack| !stack.borrow().is_empty()) {
return 0;
}
let ids: Vec<u64> = {
let mut q = GC_DESTROY_QUEUE.lock().unwrap();
q.drain(..).collect()
Expand Down
27 changes: 27 additions & 0 deletions crates/perry-runtime/src/async_hooks/test_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,33 @@ mod tests {
assert_eq!(b.async_id, a.async_id + 1);
}

#[test]
fn queued_destruction_waits_until_nested_execution_scopes_finish() {
reset_for_tests();
let outer = init_resource("outer", TAG_UNDEFINED_F64, true);
before(outer.async_id, outer.trigger_async_id);
let inner = init_resource("inner", TAG_UNDEFINED_F64, true);
before(inner.async_id, inner.trigger_async_id);
enqueue_gc_destroy(outer.async_id);
enqueue_gc_destroy(inner.async_id);
assert_eq!(drain_gc_destroy_queue(), 0);
assert_eq!(GC_DESTROY_QUEUE.lock().unwrap().len(), 2);
assert!(RESOURCES.lock().unwrap().contains_key(&outer.async_id));
assert!(RESOURCES.lock().unwrap().contains_key(&inner.async_id));
after(inner.async_id);
assert_eq!(execution_async_id_u64(), outer.async_id);
assert_eq!(drain_gc_destroy_queue(), 0);
assert_eq!(GC_DESTROY_QUEUE.lock().unwrap().len(), 2);
after(outer.async_id);
assert_eq!(execution_async_id_u64(), 0);
assert_eq!(drain_gc_destroy_queue(), 2);
assert!(GC_DESTROY_QUEUE.lock().unwrap().is_empty());
assert!(!RESOURCES.lock().unwrap().contains_key(&outer.async_id));
assert!(!RESOURCES.lock().unwrap().contains_key(&inner.async_id));
assert_eq!(drain_gc_destroy_queue(), 0);
reset_for_tests();
}

#[test]
fn before_after_restore_execution_ids() {
reset_for_tests();
Expand Down
Loading