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,12 +1,17 @@
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());
let _handle = thread::spawn(move || -> Result<()> {
unsafe { unsafe {
let lib = Library::new(plugin).context("Failed to load plugin")?; let lib = Library::new(plugin).context("Failed to load plugin")?;
let func: Symbol<unsafe fn() -> c_int> = lib let func: Symbol<unsafe fn() -> c_int> = lib
@ -17,6 +22,8 @@ pub fn load(plugin: &Path) -> Result<()> {
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()))?;
} }