diff options
author | Giovanny GutiƩrrez <giovanny.gutierrez@commure.com> | 2022-08-21 14:03:56 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-21 21:03:56 +0200 |
commit | 5ea51702bd4a3f7b14cd754b3218e50f9ed07bcc (patch) | |
tree | 31a2d07ea6e18bf9a65a508d9702ea786843b292 | |
parent | 97954003cc87b664768918173e8d00f6df35e04f (diff) |
fix: Free up JsRuntime state global handles before snapshot (#15491)
-rw-r--r-- | core/runtime.rs | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/core/runtime.rs b/core/runtime.rs index 06c777687..4c8bf0db5 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -767,7 +767,14 @@ impl JsRuntime { .clear_all_slots(self.v8_isolate()); } } - state.borrow_mut().known_realms.clear(); + let mut state = state.borrow_mut(); + state.known_realms.clear(); + // Free up additional global handles before creating the snapshot + state.js_macrotask_cbs.clear(); + state.js_nexttick_cbs.clear(); + state.js_wasm_streaming_cb = None; + state.js_format_exception_cb = None; + state.js_promise_reject_cb = None; } let snapshot_creator = self.snapshot_creator.as_mut().unwrap(); @@ -2675,6 +2682,42 @@ pub mod tests { .execute_script("check.js", "if (a != 3) throw Error('x')") .unwrap(); } + #[test] + fn test_snapshot_callbacks() { + let snapshot = { + let mut runtime = JsRuntime::new(RuntimeOptions { + will_snapshot: true, + ..Default::default() + }); + runtime + .execute_script( + "a.js", + r#" + Deno.core.ops.op_set_macrotask_callback(() => { + return true; + }); + Deno.core.ops.op_set_format_exception_callback(()=> { + return null; + }) + Deno.core.setPromiseRejectCallback(() => { + return false; + }); + a = 1 + 2; + "#, + ) + .unwrap(); + runtime.snapshot() + }; + + let snapshot = Snapshot::JustCreated(snapshot); + let mut runtime2 = JsRuntime::new(RuntimeOptions { + startup_snapshot: Some(snapshot), + ..Default::default() + }); + runtime2 + .execute_script("check.js", "if (a != 3) throw Error('x')") + .unwrap(); + } #[test] fn test_from_boxed_snapshot() { |