From e8a3370dce0cba1af2be4d0182a3750a70d3c130 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 4 May 2024 20:03:02 +0200 Subject: [PATCH] fix(src/config/so): Ensure that the plugin keeps running --- src/app/config/shared_objects/mod.rs | 29 +++++++++++++++++----------- src/app/mod.rs | 4 ++-- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/app/config/shared_objects/mod.rs b/src/app/config/shared_objects/mod.rs index 737eff1..7d74119 100644 --- a/src/app/config/shared_objects/mod.rs +++ b/src/app/config/shared_objects/mod.rs @@ -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 cli_log::info; use libloading::{Library, Symbol}; -pub fn load(plugin: &Path) -> Result<()> { +pub fn load(plugin: PathBuf) -> Result<()> { info!("Loading a plugin from '{}'", plugin.display()); - unsafe { - let lib = Library::new(plugin).context("Failed to load plugin")?; - let func: Symbol c_int> = lib - .get(b"plugin_main") - .context("Plugin does not have a 'plugin_main' symbol")?; + let _handle = thread::spawn(move || -> Result<()> { + unsafe { + let lib = Library::new(plugin).context("Failed to load plugin")?; + let func: Symbol c_int> = lib + .get(b"plugin_main") + .context("Plugin does not have a 'plugin_main' symbol")?; - info!("Starting plugin"); - let out = func(); - info!("Plugin finished with: {}", out); - } + info!("Starting plugin"); + let out = func(); + info!("Plugin finished with: {}", out); + } + Ok(()) + }); Ok(()) } diff --git a/src/app/mod.rs b/src/app/mod.rs index e9d4961..cd99a16 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -6,7 +6,7 @@ pub mod status; use std::{collections::HashMap, path::PathBuf, sync::OnceLock}; use anyhow::{Context, Result}; -use cli_log::warn; +use cli_log::{debug, warn}; use directories::ProjectDirs; use keymaps::trie::Node; use tokio::sync::mpsc::{self, Sender}; @@ -93,7 +93,7 @@ impl App { } 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()))?; }