fix(src/config/so): Ensure that the plugin keeps running

This commit is contained in:
Benedikt Peetz 2024-05-04 20:03:02 +02:00
parent d76f279a05
commit e8a3370dce
2 changed files with 20 additions and 13 deletions

View File

@ -1,22 +1,29 @@
use std::{ffi::c_int, path::Path}; use std::{
ffi::c_int,
path::PathBuf,
thread::{self},
};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use cli_log::info; use cli_log::info;
use libloading::{Library, Symbol}; use libloading::{Library, Symbol};
pub fn load(plugin: &Path) -> Result<()> { pub fn load(plugin: PathBuf) -> Result<()> {
info!("Loading a plugin from '{}'", plugin.display()); info!("Loading a plugin from '{}'", plugin.display());
unsafe { let _handle = thread::spawn(move || -> Result<()> {
let lib = Library::new(plugin).context("Failed to load plugin")?; unsafe {
let func: Symbol<unsafe fn() -> c_int> = lib let lib = Library::new(plugin).context("Failed to load plugin")?;
.get(b"plugin_main") let func: Symbol<unsafe fn() -> c_int> = lib
.context("Plugin does not have a 'plugin_main' symbol")?; .get(b"plugin_main")
.context("Plugin does not have a 'plugin_main' symbol")?;
info!("Starting plugin"); info!("Starting plugin");
let out = func(); let out = func();
info!("Plugin finished with: {}", out); info!("Plugin finished with: {}", out);
} }
Ok(())
});
Ok(()) Ok(())
} }

View File

@ -6,7 +6,7 @@ pub mod status;
use std::{collections::HashMap, path::PathBuf, sync::OnceLock}; use std::{collections::HashMap, path::PathBuf, sync::OnceLock};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use cli_log::warn; use cli_log::{debug, warn};
use directories::ProjectDirs; use directories::ProjectDirs;
use keymaps::trie::Node; use keymaps::trie::Node;
use tokio::sync::mpsc::{self, Sender}; use tokio::sync::mpsc::{self, Sender};
@ -93,7 +93,7 @@ impl<U: TrinitrixUi> App<U> {
} }
if let Some(plugin) = plugin_path { if let Some(plugin) = plugin_path {
config::shared_objects::load(&plugin) config::shared_objects::load(plugin.clone())
.with_context(|| format!("Failed to load a pluging at '{}'", plugin.display()))?; .with_context(|| format!("Failed to load a pluging at '{}'", plugin.display()))?;
} }