mirror of
https://github.com/denoland/deno.git
synced 2025-07-07 13:25:07 +00:00
fix(ext/napi): ensure the finalizer callback will be called (#29710)
See https://github.com/napi-rs/napi-rs/issues/2708#issuecomment-2963957944 for the context. In current implementation, the Weak `Reference` is created with `v8::Weak::with_finalizer`, but there is no guarantee as to *when* or even *if* the finalization callback: https://github.com/denoland/rusty_v8/blob/v137.2.0/src/handle.rs#L623-L627. It may cause the memory leak if Node-API caller want to do some cleanup jobs in the finalization callback.
This commit is contained in:
parent
d2d8d3775f
commit
3c3af1011a
1 changed files with 4 additions and 5 deletions
|
@ -111,12 +111,11 @@ impl Reference {
|
|||
fn set_weak(&mut self) {
|
||||
let reference = self as *mut Reference;
|
||||
if let ReferenceState::Strong(g) = &self.state {
|
||||
let cb = Box::new(move |_: &mut v8::Isolate| {
|
||||
Reference::weak_callback(reference)
|
||||
});
|
||||
let cb = Box::new(move || Reference::weak_callback(reference));
|
||||
let isolate = unsafe { (*self.env).isolate() };
|
||||
self.state =
|
||||
ReferenceState::Weak(v8::Weak::with_finalizer(isolate, g, cb));
|
||||
self.state = ReferenceState::Weak(v8::Weak::with_guaranteed_finalizer(
|
||||
isolate, g, cb,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue