forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: change plugin interface to avoid unloading plugin before it's sa…
…fe (denoland#5210) I seem to have solved the crashes related to the plugin's op dispatchers and op futures getting dropped after the plugin is unloaded. However `runTestPluginClose()` still crashes, it's not quite clear to me yet what causes it.
- Loading branch information
1 parent
e9318aa
commit fc42ccf
Showing
9 changed files
with
104 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use deno_core::CoreIsolate; | ||
use deno_core::Op; | ||
use deno_core::OpAsyncFuture; | ||
use deno_core::OpDispatcher; | ||
use deno_core::OpId; | ||
use dlopen::symbor::Library; | ||
use futures::prelude::*; | ||
use std::rc::Rc; | ||
|
||
/// Context passed to the `deno_plugin_init()` function that plugins have to | ||
/// exports. This avoids the need to expose the `Library` type in the public | ||
/// interface. | ||
pub struct InitContext<'a> { | ||
isolate: &'a mut CoreIsolate, | ||
plugin_lib: &'a Rc<Library>, | ||
} | ||
|
||
impl<'a> InitContext<'a> { | ||
pub(crate) fn new( | ||
isolate: &'a mut CoreIsolate, | ||
plugin_lib: &'a Rc<Library>, | ||
) -> Self { | ||
Self { | ||
isolate, | ||
plugin_lib, | ||
} | ||
} | ||
} | ||
|
||
/// Does the same as `core::Isolate::register_op()`, but additionally makes | ||
/// the registered op dispatcher, as well as the op futures created by it, keep | ||
/// a reference to plugin, so that the plugin doesn't get unloaded before all | ||
/// its op registrations and the futures created by them are dropped. | ||
pub fn register_op( | ||
context: &mut InitContext, | ||
name: &str, | ||
dispatcher: impl OpDispatcher, | ||
) -> OpId { | ||
let InitContext { | ||
isolate, | ||
plugin_lib, | ||
} = context; | ||
let plugin_lib = plugin_lib.clone(); | ||
|
||
let wrap_async_op = | ||
|plugin_lib: &Rc<Library>, fut: OpAsyncFuture| -> OpAsyncFuture { | ||
let plugin_lib = plugin_lib.clone(); | ||
async move { | ||
let _ = plugin_lib; | ||
fut.await | ||
} | ||
.boxed_local() | ||
}; | ||
|
||
isolate | ||
.op_registry | ||
.register(name, move |isolate, control, zero_copy| { | ||
match dispatcher(isolate, control, zero_copy) { | ||
sync_op @ Op::Sync(..) => sync_op, | ||
Op::Async(fut) => Op::Async(wrap_async_op(&plugin_lib, fut)), | ||
Op::AsyncUnref(fut) => Op::AsyncUnref(wrap_async_op(&plugin_lib, fut)), | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters