diff options
| author | Aram Drevekenin <aram@poor.dev> | 2025-10-21 14:36:30 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-21 14:36:30 +0200 |
| commit | 889bd06f447f9d50ee46b8efc7d9970d3693cb96 (patch) | |
| tree | 171fa657faccf4b4ce7124847977626826a2a750 /zellij-server/src/plugins | |
| parent | Give plugins the ability to read pane contents (#4465) (diff) | |
| download | sl0ppij-889bd06f447f9d50ee46b8efc7d9970d3693cb96.tar.gz sl0ppij-889bd06f447f9d50ee46b8efc7d9970d3693cb96.tar.xz sl0ppij-889bd06f447f9d50ee46b8efc7d9970d3693cb96.zip | |
Migrate from wasmtime to wasmi (#4449)
* initial implementation
* fix stdio and host folder mounting
* some memory usage optimizations
* fix plugin tests
* remove unused plugin cache
* fix docker-compose for e2e tests when running locally
* rustfmt
* initial pinned executor implementation
* EXPERIMENT: add jemalloc
* initial dynamic pinned threadpool
* cleanups and experiments
* keep state on execution threads
* reintroduce memory cache
* add wasmi engine to executor threads
* load plugins for new clients on the relevant executor thread
* only show loading indication of loading for more than 0.4 secs
* fix anonymous plugin loading without client id
* refactor lots of stuff
* update snapshots
* tests for pinned executor
* remove jemalloc
* fix stderr from plugins
* cleanups
* rustfmt
* cleanups
* fix endless loading for additinal attachments
* more cleanups
* remove singlepass feature
* fix displaying plugin permission message
* render optimizations
* always render for tests
* rustfmt
* optimize release build for wasmi
* fixes for multiple client plugin loading
* fix frame rendering for multiple clients
* rustfmt
* fix loading background plugins and e2e event ordering change
* rustfmt
* account for races in the pin_floating_panes e2e test
* docs(changelog): add PR
Diffstat (limited to 'zellij-server/src/plugins')
| -rw-r--r-- | zellij-server/src/plugins/mod.rs | 9 | ||||
| -rw-r--r-- | zellij-server/src/plugins/pinned_executor.rs | 1363 | ||||
| -rw-r--r-- | zellij-server/src/plugins/pipes.rs | 5 | ||||
| -rw-r--r-- | zellij-server/src/plugins/plugin_loader.rs | 989 | ||||
| -rw-r--r-- | zellij-server/src/plugins/plugin_map.rs | 88 | ||||
| -rw-r--r-- | zellij-server/src/plugins/plugin_worker.rs | 2 | ||||
| -rw-r--r-- | zellij-server/src/plugins/unit/plugin_tests.rs | 30 | ||||
| -rw-r--r-- | zellij-server/src/plugins/wasm_bridge.rs | 1110 | ||||
| -rw-r--r-- | zellij-server/src/plugins/zellij_exports.rs | 10 |
9 files changed, 2377 insertions, 1229 deletions
diff --git a/zellij-server/src/plugins/mod.rs b/zellij-server/src/plugins/mod.rs index 14fbf345..126ec356 100644 --- a/zellij-server/src/plugins/mod.rs +++ b/zellij-server/src/plugins/mod.rs @@ -1,3 +1,4 @@ +mod pinned_executor; mod pipes; mod plugin_loader; mod plugin_map; @@ -6,13 +7,15 @@ mod wasm_bridge; mod watch_filesystem; mod zellij_exports; use log::info; + +pub use pinned_executor::PinnedExecutor; use std::{ collections::{BTreeMap, HashMap, HashSet}, fs, path::PathBuf, time::Duration, }; -use wasmtime::Engine; +use wasmi::Engine; use crate::panes::PaneId; use crate::screen::ScreenInstruction; @@ -313,7 +316,6 @@ pub(crate) fn plugin_thread_main( cwd.clone(), skip_cache, Some(client_id), - None, ) { Ok((plugin_id, client_id)) => { drop(bus.senders.send_to_screen(ScreenInstruction::AddPlugin( @@ -389,7 +391,6 @@ pub(crate) fn plugin_thread_main( None, skip_cache, None, - None, ) { Ok((plugin_id, _client_id)) => { let should_be_open_in_place = false; @@ -501,7 +502,6 @@ pub(crate) fn plugin_thread_main( cwd, skip_cache, Some(client_id), - None, )?; plugin_ids .entry(run_plugin_or_alias.clone()) @@ -1124,7 +1124,6 @@ fn load_background_plugin( cwd.clone(), skip_cache, Some(client_id), - None, ) { Ok((plugin_id, client_id)) => { let should_float = None; diff --git a/zellij-server/src/plugins/pinned_executor.rs b/zellij-server/src/plugins/pinned_executor.rs new file mode 100644 index 00000000..e02df637 --- /dev/null +++ b/zellij-server/src/plugins/pinned_executor.rs @@ -0,0 +1,1363 @@ +use crate::plugins::plugin_map::PluginMap; +use crate::plugins::wasm_bridge::PluginCache; +use crate::ClientId; +use crate::ThreadSenders; +use std::collections::{HashMap, HashSet}; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::mpsc::{channel, Sender}; +use std::sync::{Arc, Mutex}; +use std::thread; +use wasmi::Engine; +use zellij_utils::input::layout::Layout; + +/// A dynamic thread pool that pins jobs to specific threads based on plugin_id +/// Starts with 1 thread and expands when threads are busy, shrinks when plugins unload +pub struct PinnedExecutor { + // Sparse vector - Some(thread) for active threads, None for removed threads + execution_threads: Arc<Mutex<Vec<Option<ExecutionThread>>>>, + + // Maps plugin_id -> thread_index (permanent assignment) + plugin_assignments: Arc<Mutex<HashMap<u32, usize>>>, + + // Maps thread_index -> set of plugin_ids assigned to it + thread_plugins: Arc<Mutex<HashMap<usize, HashSet<u32>>>>, + + // Next thread index to use when spawning (monotonically increasing) + next_thread_idx: AtomicUsize, + + // Maximum threads allowed + max_threads: usize, + + // state to send to plugins (to be kept on execution threads) + senders: ThreadSenders, + plugin_map: Arc<Mutex<PluginMap>>, + connected_clients: Arc<Mutex<Vec<ClientId>>>, + default_layout: Box<Layout>, + plugin_cache: PluginCache, + engine: Engine, +} + +struct ExecutionThread { + sender: Sender<Job>, + jobs_in_flight: Arc<AtomicUsize>, // Busy state tracking +} + +enum Job { + Work( + Box< + dyn FnOnce( + ThreadSenders, + Arc<Mutex<PluginMap>>, + Arc<Mutex<Vec<ClientId>>>, + Box<Layout>, + PluginCache, + Engine, + ) + Send + + 'static, + >, + ), + Shutdown, // Signal to exit the worker loop +} + +impl PinnedExecutor { + /// Creates a new pinned executor with the specified maximum number of threads + /// Starts with exactly 1 thread + pub fn new( + max_threads: usize, + senders: &ThreadSenders, + plugin_map: &Arc<Mutex<PluginMap>>, + connected_clients: &Arc<Mutex<Vec<ClientId>>>, + default_layout: &Box<Layout>, + plugin_cache: &PluginCache, + engine: &Engine, + ) -> Self { + let max_threads = max_threads.max(1); // At least 1 + + let thread_0 = Self::spawn_thread( + 0, + senders.clone(), + plugin_map.clone(), + connected_clients.clone(), + default_layout.clone(), + plugin_cache.clone(), + engine.clone(), + ); + + PinnedExecutor { + execution_threads: Arc::new(Mutex::new(vec![Some(thread_0)])), + plugin_assignments: Arc::new(Mutex::new(HashMap::new())), + thread_plugins: Arc::new(Mutex::new(HashMap::new())), + next_thread_idx: AtomicUsize::new(1), // Next will be index 1 + max_threads, + senders: senders.clone(), + plugin_map: plugin_map.clone(), + connected_clients: connected_clients.clone(), + default_layout: default_layout.clone(), + plugin_cache: plugin_cache.clone(), + engine: engine.clone(), + } + } + + fn spawn_thread( + thread_idx: usize, + senders: ThreadSenders, + plugin_map: Arc<Mutex<PluginMap>>, + connected_clients: Arc<Mutex<Vec<ClientId>>>, + default_layout: Box<Layout>, + plugin_cache: PluginCache, + engine: Engine, + ) -> ExecutionThread { + let (sender, receiver) = channel::<Job>(); + let jobs_in_flight = Arc::new(AtomicUsize::new(0)); + let jobs_in_flight_clone = jobs_in_flight.clone(); + + let thread_handle = thread::Builder::new() + .name(format!("plugin-exec-{}", thread_idx)) + .spawn({ + move || { + let senders = senders; + let plugin_map = plugin_map; + let connected_clients = connected_clients; + let default_layout = default_layout; + let plugin_cache = plugin_cache; + let engine = engine; + while let Ok(job) = receiver.recv() { + match job { + Job::Work(work) => { + work( + senders.clone(), + plugin_map.clone(), + connected_clients.clone(), + default_layout.clone(), + plugin_cache.clone(), + engine.clone(), + ); + jobs_in_flight_clone.fetch_sub(1, Ordering::SeqCst); + }, + Job::Shutdown => break, + } + } + } + }); + if let Err(e) = thread_handle { + log::error!("Failed to spawn plugin execution thread: {}", e); + } + + ExecutionThread { + sender, + jobs_in_flight, + } + } + + /// Register a plugin and assign it to a thread + /// Called from wasm_bridge when loading a plugin + pub fn register_plugin(&self, plugin_id: u32) -> usize { + let mut assignments = self.plugin_assignments.lock().unwrap(); + + // If already assigned (shouldn't happen, but defensive) + if let Some(&thread_idx) = assignments.get(&plugin_id) { + return thread_idx; + } + + let mut thread_plugins = self.thread_plugins.lock().unwrap(); + let threads = self.execution_threads.lock().unwrap(); + + // Find a non-busy thread with assigned plugins (prefer reusing threads) + let mut best_thread: Option<(usize, usize)> = None; // (index, load) + + for (idx, thread_opt) in threads.iter().enumerate() { + if let Some(thread) = thread_opt { + let is_busy = thread.jobs_in_flight.load(Ordering::SeqCst) > 0; + if !is_busy { + let load = thread_plugins.get(&idx).map(|s| s.len()).unwrap_or(0); + if best_thread.is_none() || best_thread.map(|b| load < b.1).unwrap_or(false) { + best_thread = Some((idx, load)); + } + } + } + } + + let thread_idx = if let Some((idx, _)) = best_thread { + // Found a non-busy thread + idx + } else { + // All threads are busy - need to expand + if threads.len() < self.max_threads { + // Spawn a new thread + let new_idx = self.next_thread_idx.fetch_add(1, Ordering::SeqCst); + drop(threads); // Release lock before spawning + self.add_thread(new_idx); + new_idx + } else { + // At max capacity, assign to least-loaded thread + threads + .iter() + .enumerate() + .filter_map(|(idx, t)| t.as_ref().map(|_| idx)) + .min_by_key(|&idx| thread_plugins.get(&idx).map(|s| s.len()).unwrap_or(0)) + .unwrap_or_else(|| { + log::error!("Failed to find free thread to run the plugin!"); + 0 // this is a misconfiguration, but we don't want to crash the app + // if it happens + }) + } + }; + + // Update mappings + assignments.insert(plugin_id, thread_idx); + thread_plugins + .entry(thread_idx) + .or_insert_with(HashSet::new) + .insert(plugin_id); + + thread_idx + } + + fn add_thread(&self, thread_idx: usize) { + let mut threads = self.execution_threads.lock().unwrap(); + let new_thread = Self::spawn_thread( + thread_idx, + self.senders.clone(), + self.plugin_map.clone(), + self.connected_clients.clone(), + self.default_layout.clone(), + self.plugin_cache.clone(), + self.engine.clone(), + ); + + // Extend vector if needed + while threads.len() <= thread_idx { + threads.push(None); + } + threads[thread_idx] = Some(new_thread); + } + + /// Execute job pinned to plugin's assigned thread + pub fn execute_for_plugin<F>(&self, plugin_id: u32, f: F) + where + F: FnOnce( + ThreadSenders, + Arc<Mutex<PluginMap>>, + Arc<Mutex<Vec<ClientId>>>, + Box<Layout>, + PluginCache, + Engine, + ) + Send + + 'static, + { + // Look up assigned thread + let thread_idx = { + let assignments = self.plugin_assignments.lock().unwrap(); + assignments.get(&plugin_id).copied() + }; + let Some(thread_idx) = thread_idx else { + log::error!("Failed to find thread for plugin with id: {}", plugin_id); + return; + }; + + // Get thread and mark as busy + let threads = self.execution_threads.lock().unwrap(); + let thread = threads[thread_idx].as_ref(); + let Some(thread) = thread else { + log::error!("Failed to find thread for plugin with id: {}", plugin_id); + return; + }; + + // Increment busy counter BEFORE sending work + thread.jobs_in_flight.fetch_add(1, Ordering::SeqCst); + + // Send work + let job = Job::Work(Box::new(f)); + if let Err(_) = thread.sender.send(job) { + // Thread died unexpectedly - this is a critical error + thread.jobs_in_flight.fetch_sub(1, Ordering::SeqCst); + log::error!("Plugin executor thread {} has died", thread_idx); + } + } + + /// Load a plugin: register it and execute the load work on its assigned thread + /// This combines registration + execution for plugin loading + pub fn execute_plugin_load<F>(&self, plugin_id: u32, f: F) + where + F: FnOnce( + ThreadSenders, + Arc<Mutex<PluginMap>>, + Arc<Mutex<Vec<ClientId>>>, + Box<Layout>, + PluginCache, + Engine, + ) + Send + + 'static, + { + // Register plugin and assign to a thread + self.register_plugin(plugin_id); + + // Execute the load work on the assigned thread + self.execute_for_plugin(plugin_id, f); + } + + /// Unload a plugin: execute cleanup work, then unregister and potentially shrink pool + /// This combines cleanup execution + unregistration for plugin unloading + /// Requires Arc<Self> so we can clone it into the closure for unregistration + pub fn execute_plugin_unload( + self: &Arc<Self>, + plugin_id: u32, + f: impl FnOnce( + ThreadSenders, + Arc<Mutex<PluginMap>>, + Arc<Mutex<Vec<ClientId>>>, + Box<Layout>, + PluginCache, + Engine, + ) + Send + + 'static, + ) { + let executor = self.clone(); + self.execute_for_plugin( + plugin_id, + move |senders, plugin_map, connected_clients, default_layout, plugin_cache, engine| { + // Execute the cleanup work + f( + senders, + plugin_map, + connected_clients, + default_layout, + plugin_cache, + engine, + ); + + // Unregister plugin and potentially shrink the pool + executor.unregister_plugin(plugin_id); + }, + ); + } + + /// Unregister a plugin and potentially shrink the pool + /// Called from wasm_bridge after plugin cleanup is complete + pub fn unregister_plugin(&self, plugin_id: u32) { + let mut assignments = self.plugin_assignments.lock().unwrap(); + let mut thread_plugins = self.thread_plugins.lock().unwrap(); + + if let Some(thread_idx) = assignments.remove(&plugin_id) { + if let Some(plugins) = thread_plugins.get_mut(&thread_idx) { + plugins.remove(&plugin_id); + } + } + + drop(assignments); + drop(thread_plugins); + + // Try to shrink the pool + self.try_shrink_pool(); + } + + fn try_shrink_pool(&self) { + let mut threads = self.execution_threads.lock().unwrap(); + let thread_plugins = self.thread_plugins.lock().unwrap(); + + // Find threads with no assigned plugins (except thread 0, always keep it) + let threads_to_remove: Vec<usize> = threads + .iter() + .enumerate() + .skip(1) // Never remove thread 0 + .filter_map(|(idx, thread_opt)| { + if thread_opt.is_some() { + let has_plugins = thread_plugins + .get(&idx) + .map(|s| !s.is_empty()) + .unwrap_or(false); + if !has_plugins { + Some(idx) + } else { + None + } + } else { + None + } + }) + .collect(); + + // Shutdown and remove idle threads + for idx in threads_to_remove { + if let Some(thread) = threads[idx].take() { + let _ = thread.sender.send(Job::Shutdown); + } + } + } + + #[cfg(test)] + pub fn thread_count(&self) -> usize { + self.execution_threads + .lock() + .unwrap() + .iter() + .filter(|t| t.is_some()) + .count() + } +} + +impl Drop for PinnedExecutor { + fn drop(&mut self) { + let mut threads = self.execution_threads.lock().unwrap(); + + // Send shutdown to all threads + for thread_opt in threads.iter_mut() { + if let Some(thread) = thread_opt { + let _ = thread.sender.send(Job::Shutdown); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::mpsc::{channel, Sender}; + use std::sync::{Arc, Barrier, Mutex}; + use std::thread; + use std::time::Duration; + + // Test fixtures + fn create_test_dependencies() -> ( + ThreadSenders, + Arc<Mutex<PluginMap>>, + Arc<Mutex<Vec<ClientId>>>, + Box<Layout>, + PluginCache, + Engine, + ) { + use std::path::PathBuf; + use wasmi::Module; + use zellij_utils::channels::{self, SenderWithContext}; + + let (send_to_pty, _receive_pty) = channels::bounded(1); + let (send_to_screen, _receive_screen) = channels::bounded(1); + let (send_to_plugin, _receive_plugin) = channels::bounded(1); + let (send_to_server, _receive_server) = channels::bounded(1); + let (send_to_pty_writer, _receive_pty_writer) = channels::bounded(1); + let (send_to_background_jobs, _receive_background_jobs) = channels::bounded(1); + + let to_pty = SenderWithContext::new(send_to_pty); + let to_screen = SenderWithContext::new(send_to_screen); + let to_plugin = SenderWithContext::new(send_to_plugin); + let to_server = SenderWithContext::new(send_to_server); + let to_pty_writer = SenderWithContext::new(send_to_pty_writer); + let to_background_jobs = SenderWithContext::new(send_to_background_jobs); + + let senders = ThreadSenders { + to_pty: Some(to_pty), + to_screen: Some(to_screen), + to_plugin: Some(to_plugin), + to_server: Some(to_server), + to_pty_writer: Some(to_pty_writer), + to_background_jobs: Some(to_background_jobs), + should_silently_fail: false, + }; + + let plugin_map = Arc::new(Mutex::new(PluginMap::default())); + let connected_clients = Arc::new(Mutex::new(vec![])); + + let layout = Box::new(Layout::default()); + + let plugin_cache = Arc::new(Mutex::new( + std::collections::HashMap::<PathBuf, Module>::new(), + )); + + let engine = Engine::default(); + + ( + senders, + plugin_map, + connected_clients, + layout, + plugin_cache, + engine, + ) + } + + fn create_test_executor(max_threads: usize) -> Arc<PinnedExecutor> { + let (senders, plugin_map, clients, layout, cache, engine) = create_test_dependencies(); + Arc::new(PinnedExecutor::new( + max_threads, + &senders, + &plugin_map, + &clients, + &layout, + &cache, + &engine, + )) + } + + // Helper to create a job that signals completion via channel + fn make_signaling_job( + tx: Sender<()>, + ) -> impl FnOnce( + ThreadSenders, + Arc<Mutex<PluginMap>>, + Arc<Mutex<Vec<ClientId>>>, + Box<Layout>, + PluginCache, + Engine, + ) + Send + + 'static { + move |_senders, _plugin_map, _clients, _layout, _cache, _engine| { + tx.send(()).unwrap(); + } + } + + // Helper to verify thread assignment by capturing thread name in job + fn get_thread_name_for_plugin(executor: &Arc<PinnedExecutor>, plugin_id: u32) -> String { + let (tx, rx) = channel(); + executor.execute_for_plugin(plugin_id, move |_s, _p, _c, _l, _ca, _e| { + let name = thread::current().name().unwrap().to_string(); + tx.send(name).unwrap(); + }); + rx.recv_timeout(Duration::from_secs(5)) + .expect("Thread name should be received") + } + + #[test] + fn test_new_creates_one_thread() { + let executor = create_test_executor(4); + assert_eq!(executor.thread_count(), 1); + } + + #[test] + fn test_new_respects_min_threads() { + let executor = create_test_executor(0); + assert_eq!( + executor.thread_count(), + 1, + "Executor should enforce minimum of 1 thread" + ); + } + + #[test] + fn test_first_plugin_assigned_to_thread_zero() { + let executor = create_test_executor(4); + let thread_idx = executor.register_plugin(1); + assert_eq!(thread_idx, 0); + } + + #[test] + fn test_multiple_plugins_share_thread_when_idle() { + let executor = create_test_executor(4); + let thread_idx1 = executor.register_plugin(1); + let thread_idx2 = executor.register_plugin(2); + assert_eq!(thread_idx1, 0); + assert_eq!( + thread_idx2, 0, + "Second plugin should share thread 0 when idle" + ); + } + + #[test] + fn test_new_thread_spawns_when_all_busy() { + let executor = create_test_executor(3); + + // Register plugin 1, gets thread 0 + let thread_idx1 = executor.register_plugin(1); + assert_eq!(thread_idx1, 0); + + // Make thread 0 busy with a barrier + let barrier = Arc::new(Barrier::new(2)); + let barrier_clone = barrier.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + barrier_clone.wait(); + }); + + // Give the job a moment to start executing and block + thread::sleep(Duration::from_millis(50)); + + // Register plugin 2 while thread 0 is busy + let thread_idx2 = executor.register_plugin(2); + assert_eq!( + thread_idx2, 1, + "Plugin 2 should get new thread 1 when thread 0 is busy" + ); + + // Verify thread count + assert_eq!(executor.thread_count(), 2); + + // Release barrier + barrier.wait(); + } + + #[test] + fn test_respects_max_threads_limit() { + let executor = create_test_executor(2); + + // Register plugin 1, gets thread 0 + executor.register_plugin(1); + + // Make thread 0 busy + let barrier1 = Arc::new(Barrier::new(2)); + let barrier1_clone = barrier1.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + barrier1_clone.wait(); + }); + thread::sleep(Duration::from_millis(50)); + + // Register plugin 2, gets thread 1 + executor.register_plugin(2); + + // Make thread 1 busy + let barrier2 = Arc::new(Barrier::new(2)); + let barrier2_clone = barrier2.clone(); + executor.execute_for_plugin(2, move |_s, _p, _c, _l, _ca, _e| { + barrier2_clone.wait(); + }); + thread::sleep(Duration::from_millis(50)); + + // Register plugin 3 when all threads busy + let thread_idx3 = executor.register_plugin(3); + assert!( + thread_idx3 == 0 || thread_idx3 == 1, + "Plugin 3 should be assigned to existing thread" + ); + assert_eq!(executor.thread_count(), 2, "Should not exceed max_threads"); + + // Release barriers + barrier1.wait(); + barrier2.wait(); + } + + #[test] + fn test_duplicate_registration_returns_same_thread() { + let executor = create_test_executor(4); + let thread_idx1 = executor.register_plugin(1); + let thread_idx2 = executor.register_plugin(1); + assert_eq!( + thread_idx1, thread_idx2, + "Duplicate registration should return same thread" + ); + } + + #[test] + fn test_load_balancing_prefers_least_loaded() { + let executor = create_test_executor(3); + + // Register plugins 1, 2, 3 to thread 0 (when idle) + executor.register_plugin(1); + executor.register_plugin(2); + executor.register_plugin(3); + + // Make thread 0 busy + let barrier = Arc::new(Barrier::new(2)); + let barrier_clone = barrier.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + barrier_clone.wait(); + }); + thread::sleep(Duration::from_millis(50)); + + // Register plugin 4 while thread 0 is busy (spawns thread 1) + let thread_idx4 = executor.register_plugin(4); + assert_eq!(thread_idx4, 1); + + // Release barrier + barrier.wait(); + thread::sleep(Duration::from_millis(50)); + + // Register plugin 5 when both threads idle + // Thread 0 has 3 plugins, thread 1 has 1 plugin + let thread_idx5 = executor.register_plugin(5); + assert_eq!( + thread_idx5, 1, + "Plugin 5 should be assigned to less loaded thread 1" + ); + } + + #[test] + fn test_execute_for_plugin_runs_on_correct_thread() { + let executor = create_test_executor(3); + + // Register plugin 1 to thread 0 + executor.register_plugin(1); + + // Make thread 0 busy to force plugin 2 to thread 1 + let barrier = Arc::new(Barrier::new(2)); + let barrier_clone = barrier.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + barrier_clone.wait(); + }); + thread::sleep(Duration::from_millis(50)); + + // Register plugin 2 to thread 1 + executor.register_plugin(2); + + // Release barrier + barrier.wait(); + thread::sleep(Duration::from_millis(50)); + + // Get thread names for both plugins + let thread_name1 = get_thread_name_for_plugin(&executor, 1); + let thread_name2 = get_thread_name_for_plugin(&executor, 2); + + assert_eq!(thread_name1, "plugin-exec-0"); + assert_eq!(thread_name2, "plugin-exec-1"); + } + + #[test] + fn test_execute_for_plugin_unregistered() { + let executor = create_test_executor(4); + let (tx, rx) = channel(); + + // Execute job for unregistered plugin + executor.execute_for_plugin(999, make_signaling_job(tx)); + + // Try to receive with timeout - should timeout + let result = rx.recv_timeout(Duration::from_millis(100)); + assert!( + result.is_err(), + "Job for unregistered plugin should not execute" + ); + } + + #[test] + fn test_job_execution_order_per_thread() { + let executor = create_test_executor(4); + executor.register_plugin(1); + + let order = Arc::new(Mutex::new(Vec::new())); + let (tx, rx) = channel(); + + // Execute 3 jobs for plugin 1 + for i in 1..=3 { + let order_clone = order.clone(); + let tx_clone = tx.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + order_clone.lock().unwrap().push(i); + tx_clone.send(()).unwrap(); + }); + } + + // Wait for all 3 jobs to complete + for _ in 0..3 { + rx.recv_timeout(Duration::from_secs(5)) + .expect("Job should complete"); + } + + assert_eq!( + *order.lock().unwrap(), + vec![1, 2, 3], + "Jobs should execute in order" + ); + } + + #[test] + fn test_jobs_complete_successfully() { + let executor = create_test_executor(4); + executor.register_plugin(1); + + let (tx, rx) = channel(); + executor.execute_for_plugin(1, make_signaling_job(tx)); + + let result = rx.recv_timeout(Duration::from_secs(5)); + assert!(result.is_ok(), "Job should complete successfully"); + } + + #[test] + fn test_concurrent_jobs_on_different_threads() { + let executor = create_test_executor(3); + + // Register plugin 1 to thread 0 + executor.register_plugin(1); + + // Make thread 0 busy to force plugin 2 to thread 1 + let barrier = Arc::new(Barrier::new(2)); + let barrier_clone = barrier.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + barrier_clone.wait(); + }); + thread::sleep(Duration::from_millis(50)); + + // Register plugin 2 to thread 1 + executor.register_plugin(2); + + // Release barrier + barrier.wait(); + thread::sleep(Duration::from_millis(50)); + + // Execute jobs on both threads concurrently + let sync_barrier = Arc::new(Barrier::new(3)); // 2 jobs + test thread + let (tx1, rx1) = channel(); + let (tx2, rx2) = channel(); + + let barrier1 = sync_barrier.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + barrier1.wait(); + tx1.send(()).unwrap(); + }); + + let barrier2 = sync_barrier.clone(); + executor.execute_for_plugin(2, move |_s, _p, _c, _l, _ca, _e| { + barrier2.wait(); + tx2.send(()).unwrap(); + }); + + // Release both jobs simultaneously + sync_barrier.wait(); + + // Both should complete + assert!(rx1.recv_timeout(Duration::from_secs(5)).is_ok()); + assert!(rx2.recv_timeout(Duration::from_secs(5)).is_ok()); + } + + #[test] + fn test_execute_plugin_load_registers_and_executes() { + let executor = create_test_executor(4); + + let (tx, rx) = channel(); + executor.execute_plugin_load(1, make_signaling_job(tx)); + + // Wait for load to complete + rx.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + // Verify plugin is registered + let thread_idx = executor.register_plugin(1); + assert_eq!(thread_idx, 0, "Plugin should already be registered"); + } + + #[test] + fn test_execute_plugin_unload_runs_cleanup_before_unregister() { + let executor = create_test_executor(4); + + // Load plugin + let (tx_load, rx_load) = channel(); + executor.execute_plugin_load(1, make_signaling_job(tx_load)); + rx_load + .recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + // Unload plugin with cleanup + let counter = Arc::new(AtomicUsize::new(0)); + let counter_clone = counter.clone(); + let (tx_unload, rx_unload) = channel(); + + executor.execute_plugin_unload(1, move |_s, _p, _c, _l, _ca, _e| { + counter_clone.fetch_add(1, Ordering::SeqCst); + tx_unload.send(()).unwrap(); + }); + + // Wait for unload to complete + rx_unload + .recv_timeout(Duration::from_secs(5)) + .expect("Unload should complete"); + + // Verify cleanup ran + assert_eq!(counter.load(Ordering::SeqCst), 1, "Cleanup should have run"); + + // Give unregister a moment to complete + thread::sleep(Duration::from_millis(100)); + + // Plugin should be unregistered - registering again should assign new thread + let thread_idx = executor.register_plugin(1); + assert_eq!(thread_idx, 0, "Plugin should be re-registered to thread 0"); + } + + #[test] + fn test_unload_sequence_is_correct() { + let executor = create_test_executor(4); + + // Load plugin + let (tx_load, rx_load) = channel(); + executor.execute_plugin_load(1, make_signaling_job(tx_load)); + rx_load + .recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + // Unload with sequence tracking + let sequence = Arc::new(Mutex::new(Vec::new())); + let sequence_clone = sequence.clone(); + let (tx_unload, rx_unload) = channel(); + + executor.execute_plugin_unload(1, move |_s, _p, _c, _l, _ca, _e| { + sequence_clone.lock().unwrap().push("cleanup"); + tx_unload.send(()).unwrap(); + }); + + // Wait for unload to complete + rx_unload + .recv_timeout(Duration::from_secs(5)) + .expect("Unload should complete"); + sequence.lock().unwrap().push("after"); + + assert_eq!(*sequence.lock().unwrap(), vec!["cleanup", "after"]); + } + + #[test] + fn test_shrink_removes_idle_threads() { + let executor = create_test_executor(4); + + // Load plugin 1 to thread 0 + let (tx1, rx1) = channel(); + executor.execute_plugin_load(1, make_signaling_job(tx1)); + rx1.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + // Make thread 0 busy to force plugin 2 to thread 1 + let barrier = Arc::new(Barrier::new(2)); + let barrier_clone = barrier.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + barrier_clone.wait(); + }); + thread::sleep(Duration::from_millis(50)); + + // Load plugin 2 to thread 1 + let (tx2, rx2) = channel(); + executor.execute_plugin_load(2, make_signaling_job(tx2)); + rx2.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + // Release barrier + barrier.wait(); + thread::sleep(Duration::from_millis(50)); + + let thread_count_before = executor.thread_count(); + assert!(thread_count_before >= 2, "Should have at least 2 threads"); + + // Unload plugin 2 + let (tx_unload2, rx_unload2) = channel(); + executor.execute_plugin_unload(2, make_signaling_job(tx_unload2)); + rx_unload2 + .recv_timeout(Duration::from_secs(5)) + .expect("Unload should complete"); + + // Give shrinking a moment to complete + thread::sleep(Duration::from_millis(100)); + + // Thread count should decrease after unloading + let thread_count_after = executor.thread_count(); + assert!( + thread_count_after < thread_count_before, + "Idle threads should be removed" + ); + assert!(thread_count_after >= 1, "Thread 0 should remain"); + } + + #[test] + fn test_thread_zero_never_removed() { + let executor = create_test_executor(4); + + // Load a plugin and then unload it + let (tx_load, rx_load) = channel(); + executor.execute_plugin_load(1, make_signaling_job(tx_load)); + rx_load + .recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + let (tx_unload, rx_unload) = channel(); + executor.execute_plugin_unload(1, make_signaling_job(tx_unload)); + rx_unload + .recv_timeout(Duration::from_secs(5)) + .expect("Unload should complete"); + + // Give shrinking a moment + thread::sleep(Duration::from_millis(100)); + + // Thread 0 should remain + assert!( + executor.thread_count() >= 1, + "Thread 0 should never be removed" + ); + } + + #[test] + fn test_active_threads_not_removed() { + let executor = create_test_executor(4); + + // Load plugin 1 to thread 0 + let (tx1, rx1) = channel(); + executor.execute_plugin_load(1, make_signaling_job(tx1)); + rx1.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + // Force plugin 2 to thread 1 by making thread 0 busy + let barrier = Arc::new(Barrier::new(2)); + let barrier_clone = barrier.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + barrier_clone.wait(); + }); + thread::sleep(Duration::from_millis(50)); + + let (tx2, rx2) = channel(); + executor.execute_plugin_load(2, make_signaling_job(tx2)); + rx2.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + barrier.wait(); + thread::sleep(Duration::from_millis(100)); + + let thread_count_with_both = executor.thread_count(); + assert!( + thread_count_with_both >= 2, + "Should have at least 2 threads with 2 plugins" + ); + + // Unload plugin 2 + let (tx_unload, rx_unload) = channel(); + executor.execute_plugin_unload(2, make_signaling_job(tx_unload)); + rx_unload + .recv_timeout(Duration::from_secs(5)) + .expect("Unload should complete"); + + thread::sleep(Duration::from_millis(100)); + + // Plugin 1's thread should still work (verify active threads not affected) + let (tx_test, rx_test) = channel(); + executor.execute_for_plugin(1, make_signaling_job(tx_test)); + assert!( + rx_test.recv_timeout(Duration::from_secs(5)).is_ok(), + "Plugin 1's thread should still work" + ); + + // Thread count should decrease after unloading + let thread_count_after = executor.thread_count(); + assert!( + thread_count_after < thread_count_with_both, + "Idle thread should be removed" + ); + assert!(thread_count_after >= 1, "Active threads should remain"); + } + + #[test] + fn test_shrink_does_not_affect_remaining_threads() { + let executor = create_test_executor(4); + + // Load plugin 1 to thread 0 + let (tx1, rx1) = channel(); + executor.execute_plugin_load(1, make_signaling_job(tx1)); + rx1.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + // Force plugin 2 to thread 1 + let barrier = Arc::new(Barrier::new(2)); + let barrier_clone = barrier.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + barrier_clone.wait(); + }); + thread::sleep(Duration::from_millis(50)); + + let (tx2, rx2) = channel(); + executor.execute_plugin_load(2, make_signaling_job(tx2)); + rx2.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + barrier.wait(); + thread::sleep(Duration::from_millis(50)); + + // Unload plugin 2 (shrinks pool) + let (tx_unload, rx_unload) = channel(); + executor.execute_plugin_unload(2, make_signaling_job(tx_unload)); + rx_unload + .recv_timeout(Duration::from_secs(5)) + .expect("Unload should complete"); + + thread::sleep(Duration::from_millis(100)); + + // Execute job for plugin 1 + let (tx_test, rx_test) = channel(); + executor.execute_for_plugin(1, make_signaling_job(tx_test)); + + assert!( + rx_test.recv_timeout(Duration::from_secs(5)).is_ok(), + "Plugin 1's thread should still work" + ); + } + + #[test] + fn test_drop_cleans_up_gracefully() { + let executor = create_test_executor(4); + + // Load multiple plugins on different threads + let (tx1, rx1) = channel(); + executor.execute_plugin_load(1, make_signaling_job(tx1)); + rx1.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + let barrier = Arc::new(Barrier::new(2)); + let barrier_clone = barrier.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + barrier_clone.wait(); + }); + thread::sleep(Duration::from_millis(50)); + + let (tx2, rx2) = channel(); + executor.execute_plugin_load(2, make_signaling_job(tx2)); + rx2.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + barrier.wait(); + + // Drop executor + drop(executor); + + // Test completes without panic + } + + #[test] + fn test_drop_with_jobs_in_flight() { + let executor = create_test_executor(4); + executor.register_plugin(1); + + let barrier = Arc::new(Barrier::new(2)); + let barrier_clone = barrier.clone(); + executor.execute_for_plugin(1, move |_s, _p, _c, _l, _ca, _e| { + barrier_clone.wait(); + }); + + // Drop executor while job is blocked + drop(executor); + + // Release barrier (job may or may not complete, but shouldn't panic) + barrier.wait(); + + // Test completes without panic + } + + #[test] + fn test_concurrent_plugin_registrations() { + let executor = create_test_executor(4); + + let handles: Vec<_> = (1..=10) + .map(|i| { + let exec = executor.clone(); + thread::spawn(move || exec.register_plugin(i)) + }) + .collect(); + + for handle in handles { + let thread_idx = handle.join().expect("Thread should not panic"); + assert!(thread_idx < 4, "Thread index should be valid"); + } + } + + #[test] + fn test_unregister_nonexistent_plugin() { + let executor = create_test_executor(4); + + // Unregister non-existent plugin + executor.unregister_plugin(999); + + // Executor should still work + let (tx, rx) = channel(); + executor.execute_plugin_load(1, make_signaling_job(tx)); + assert!(rx.recv_timeout(Duration::from_secs(5)).is_ok()); + } + + #[test] + fn test_max_threads_one() { + let executor = create_test_executor(1); + + // Register multiple plugins + let thread_idx1 = executor.register_plugin(1); + let thread_idx2 = executor.register_plugin(2); + let thread_idx3 = executor.register_plugin(3); + + assert_eq!(thread_idx1, 0); + assert_eq!(thread_idx2, 0); + assert_eq!(thread_idx3, 0); + assert_eq!(executor.thread_count(), 1); + } + + #[test] + fn test_rapid_load_unload_cycles() { + let executor = create_test_executor(4); + + // Load plugin 1 + let (tx1, rx1) = channel(); + executor.execute_plugin_load(1, make_signaling_job(tx1)); + rx1.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + + // Unload plugin 1 + let (tx2, rx2) = channel(); + executor.execute_plugin_unload(1, make_signaling_job(tx2)); + rx2.recv_timeout(Duration::from_secs(5)) + .expect("Unload should complete"); + + thread::sleep(Duration::from_millis(100)); + + // Load plugin 1 again + let (tx3, rx3) = channel(); + executor.execute_plugin_load(1, make_signaling_job(tx3)); + rx3.recv_timeout(Duration::from_secs(5)) + .expect("Second load should complete"); + + // Execute job for plugin 1 + let (tx4, rx4) = channel(); + executor.execute_for_plugin(1, make_signaling_job(tx4)); + assert!( + rx4.recv_timeout(Duration::from_secs(5)).is_ok(), + "Executor should handle cycles correctly" + ); + } + + #[test] + fn test_many_plugins_limited_threads() { + let executor = create_test_executor(4); + let (tx, rx) = channel(); + + // Load 20 plugins + for i in 1..=20 { + let tx_clone = tx.clone(); + executor.execute_plugin_load(i, make_signaling_job(tx_clone)); + } + + // Collect 20 completion signals + for _ in 1..=20 { + rx.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + } + + assert!( + executor.thread_count() <= 4, + "Should not exceed max_threads" + ); + } + + #[test] + fn test_full_lifecycle() { + let executor = create_test_executor(4); + let (tx, rx) = channel(); + + // Load 5 plugins + for i in 1..=5 { + let tx_clone = tx.clone(); + executor.execute_plugin_load(i, make_signaling_job(tx_clone)); + } + + // Wait for all loads + for _ in 0..5 { + rx.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + } + + // Execute 2 jobs per plugin (10 total) + for i in 1..=5 { + for _ in 0..2 { + let tx_clone = tx.clone(); + executor.execute_for_plugin(i, make_signaling_job(tx_clone)); + } + } + + // Wait for all jobs + for _ in 0..10 { + rx.recv_timeout(Duration::from_secs(5)) + .expect("Job should complete"); + } + + let thread_count_before = executor.thread_count(); + + // Unload 3 plugins + for i in 1..=3 { + let tx_clone = tx.clone(); + executor.execute_plugin_unload(i, make_signaling_job(tx_clone)); + } + + // Wait for unloads + for _ in 0..3 { + rx.recv_timeout(Duration::from_secs(5)) + .expect("Unload should complete"); + } + + thread::sleep(Duration::from_millis(100)); + + // Thread count should decrease or stay the same + let thread_count_after = executor.thread_count(); + assert!( + thread_count_after <= thread_count_before, + "Thread count should decrease after unloads" + ); + + // Execute jobs for remaining plugins + for i in 4..=5 { + let tx_clone = tx.clone(); + executor.execute_for_plugin(i, make_signaling_job(tx_clone)); + } + + for _ in 0..2 { + rx.recv_timeout(Duration::from_secs(5)) + .expect("Job should complete"); + } + + // Drop executor + drop(executor); + } + + #[test] + fn test_realistic_plugin_churn() { + let executor = create_test_executor(4); + let (tx, rx) = channel(); + + // Load plugins 1, 2, 3 + for i in 1..=3 { + let tx_clone = tx.clone(); + executor.execute_plugin_load(i, make_signaling_job(tx_clone)); + } + for _ in 0..3 { + rx.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + } + + // Execute jobs for each + for i in 1..=3 { + let tx_clone = tx.clone(); + executor.execute_for_plugin(i, make_signaling_job(tx_clone)); + } + for _ in 0..3 { + rx.recv_timeout(Duration::from_secs(5)) + .expect("Job should complete"); + } + + // Unload plugin 2 + let tx_clone = tx.clone(); + executor.execute_plugin_unload(2, make_signaling_job(tx_clone)); + rx.recv_timeout(Duration::from_secs(5)) + .expect("Unload should complete"); + + thread::sleep(Duration::from_millis(100)); + + // Load plugins 4, 5 + for i in 4..=5 { + let tx_clone = tx.clone(); + executor.execute_plugin_load(i, make_signaling_job(tx_clone)); + } + for _ in 0..2 { + rx.recv_timeout(Duration::from_secs(5)) + .expect("Load should complete"); + } + + // Execute jobs for plugins 1, 3, 4, 5 + for i in &[1, 3, 4, 5] { + let tx_clone = tx.clone(); + executor.execute_for_plugin(*i, make_signaling_job(tx_clone)); + } + for _ in 0..4 { + rx.recv_timeout(Duration::from_secs(5)) + .expect("Job should complete"); + } + + // Unload plugins 1, 3 + for i in &[1, 3] { + let tx_clone = tx.clone(); + executor.execute_plugin_unload(*i, make_signaling_job(tx_clone)); + } + for _ in 0..2 { + rx.recv_timeout(Duration::from_secs(5)) + .expect("Unload should complete"); + } + + thread::sleep(Duration::from_millis(100)); + + // Verify thread count reflects active plugins (4 and 5) + assert!( + executor.thread_count() >= 1, + "Should have at least thread 0" + ); + + drop(executor); + } +} diff --git a/zellij-server/src/plugins/pipes.rs b/zellij-server/src/plugins/pipes.rs index 840af17b..4e516c44 100644 --- a/zellij-server/src/plugins/pipes.rs +++ b/zellij-server/src/plugins/pipes.rs @@ -171,7 +171,10 @@ pub fn apply_pipe_message_to_plugin( .and_then(|render| { render.call(&mut running_plugin.store, (rows as i32, columns as i32)) }) - .and_then(|_| wasi_read_string(running_plugin.store.data())) + .map_err(|e| anyhow!(e)) + .and_then(|_| { + wasi_read_string(running_plugin.store.data()).map_err(|e| anyhow!(e)) + }) .with_context(err_context)?; let pipes_to_block_or_unblock = pipes_to_block_or_unblock(running_plugin, Some(&pipe_message.source)); diff --git a/zellij-server/src/plugins/plugin_loader.rs b/zellij-server/src/plugins/plugin_loader.rs index 4d8a4542..08c85464 100644 --- a/zellij-server/src/plugins/plugin_loader.rs +++ b/zellij-server/src/plugins/plugin_loader.rs @@ -2,10 +2,9 @@ use crate::plugins::plugin_map::{ PluginEnv, PluginMap, RunningPlugin, VecDequeInputStream, WriteOutputStream, }; use crate::plugins::plugin_worker::{plugin_worker, RunningWorker}; +use crate::plugins::wasm_bridge::{LoadingContext, PluginCache}; use crate::plugins::zellij_exports::{wasi_write_object, zellij_exports}; use crate::plugins::PluginId; -use highway::{HighwayHash, PortableHash}; -use log::info; use prost::Message; use std::{ collections::{HashMap, HashSet, VecDeque}, @@ -13,19 +12,20 @@ use std::{ path::PathBuf, sync::{Arc, Mutex}, }; -use url::Url; -use wasmtime::{Engine, Instance, Linker, Module, Store}; -use wasmtime_wasi::{preview1::WasiP1Ctx, DirPerms, FilePerms, WasiCtxBuilder}; -use zellij_utils::consts::ZELLIJ_PLUGIN_ARTIFACT_DIR; +use wasmi::{Engine, Instance, Linker, Module, Store, StoreLimits}; +use wasmi_wasi::sync::WasiCtxBuilder; +use wasmi_wasi::wasi_common::pipe::{ReadPipe, WritePipe}; +use wasmi_wasi::Dir; +use wasmi_wasi::WasiCtx; use crate::{ - logging_pipe::LoggingPipe, screen::ScreenInstruction, thread_bus::ThreadSenders, + logging_pipe::LoggingPipe, thread_bus::ThreadSenders, ui::loading_indication::LoadingIndication, ClientId, }; use zellij_utils::plugin_api::action::ProtobufPluginConfiguration; use zellij_utils::{ - consts::{ZELLIJ_CACHE_DIR, ZELLIJ_SESSION_CACHE_DIR, ZELLIJ_TMP_DIR}, + consts::ZELLIJ_TMP_DIR, data::{InputMode, PluginCapabilities}, errors::prelude::*, input::command::TerminalAction, @@ -36,597 +36,138 @@ use zellij_utils::{ pane_size::Size, }; -macro_rules! display_loading_stage { - ($loading_stage:ident, $loading_indication:expr, $senders:expr, $plugin_id:expr) => {{ - $loading_indication.$loading_stage(); - drop( - $senders.send_to_screen(ScreenInstruction::UpdatePluginLoadingStage( - $plugin_id, - $loading_indication.clone(), - )), - ); - }}; +fn create_plugin_fs_entries(plugin_own_data_dir: &PathBuf, plugin_own_cache_dir: &PathBuf) { + // Create filesystem entries mounted into WASM. + // We create them here to get expressive error messages in case they fail. + if let Err(e) = fs::create_dir_all(&plugin_own_data_dir) { + log::error!("Failed to create plugin data dir: {}", e); + }; + if let Err(e) = fs::create_dir_all(&plugin_own_cache_dir) { + log::error!("Failed to create plugin cache dir: {}", e); + } + if let Err(e) = fs::create_dir_all(ZELLIJ_TMP_DIR.as_path()) { + log::error!("Failed to create plugin tmp dir: {}", e); + } } pub struct PluginLoader<'a> { - plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>, - plugin_path: PathBuf, - loading_indication: &'a mut LoadingIndication, - senders: ThreadSenders, + skip_cache: bool, plugin_id: PluginId, client_id: ClientId, - engine: Engine, - plugin: PluginConfig, - plugin_dir: &'a PathBuf, - tab_index: Option<usize>, + plugin_cwd: PathBuf, plugin_own_data_dir: PathBuf, plugin_own_cache_dir: PathBuf, - size: Size, - wasm_blob_on_hd: Option<(Vec<u8>, PathBuf)>, + plugin_config: PluginConfig, + tab_index: Option<usize>, path_to_default_shell: PathBuf, - plugin_cwd: PathBuf, capabilities: PluginCapabilities, client_attributes: ClientAttributes, default_shell: Option<TerminalAction>, - default_layout: Box<Layout>, layout_dir: Option<PathBuf>, default_mode: InputMode, keybinds: Keybinds, + plugin_dir: PathBuf, + size: Size, + loading_indication: LoadingIndication, + senders: ThreadSenders, + engine: Engine, + default_layout: Box<Layout>, + plugin_cache: PluginCache, + plugin_map: &'a mut PluginMap, // we receive a mutable reference rather than the Arc so that it + // will be held for the lifetime of this struct and thus loading + // plugins for all connected clients will be one transaction + connected_clients: Option<Arc<Mutex<Vec<ClientId>>>>, } impl<'a> PluginLoader<'a> { - pub fn reload_plugin_from_memory( - plugin_id: PluginId, - plugin_dir: PathBuf, - plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>, - senders: ThreadSenders, - engine: Engine, - plugin_map: Arc<Mutex<PluginMap>>, - connected_clients: Arc<Mutex<Vec<ClientId>>>, - loading_indication: &mut LoadingIndication, - path_to_default_shell: PathBuf, - plugin_cwd: PathBuf, - capabilities: PluginCapabilities, - client_attributes: ClientAttributes, - default_shell: Option<TerminalAction>, - default_layout: Box<Layout>, - layout_dir: Option<PathBuf>, - ) -> Result<()> { - let err_context = || format!("failed to reload plugin {plugin_id} from memory"); - let mut connected_clients: Vec<ClientId> = - connected_clients.lock().unwrap().iter().copied().collect(); - if connected_clients.is_empty() { - return Err(anyhow!("No connected clients, cannot reload plugin")); - } - let first_client_id = connected_clients.remove(0); - - let mut plugin_loader = PluginLoader::new_from_existing_plugin_attributes( - &plugin_cache, - &plugin_map, - loading_indication, - &senders, - plugin_id, - first_client_id, - engine, - &plugin_dir, - path_to_default_shell, - Some(plugin_cwd), - capabilities, - client_attributes, - default_shell, - default_layout, - layout_dir, - )?; - plugin_loader - .load_module_from_memory() - .and_then(|module| plugin_loader.create_plugin_environment(module)) - .and_then(|(store, instance)| { - plugin_loader.load_plugin_instance(store, &instance, &plugin_map) - }) - .and_then(|_| { - plugin_loader.clone_instance_for_other_clients(&connected_clients, &plugin_map) - }) - .with_context(err_context)?; - display_loading_stage!(end, loading_indication, senders, plugin_id); - Ok(()) - } - - pub fn start_plugin( - plugin_id: PluginId, - client_id: ClientId, - plugin: &PluginConfig, - tab_index: Option<usize>, - plugin_dir: PathBuf, - plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>, - senders: ThreadSenders, - engine: Engine, - plugin_map: Arc<Mutex<PluginMap>>, - size: Size, - connected_clients: Arc<Mutex<Vec<ClientId>>>, - loading_indication: &mut LoadingIndication, - path_to_default_shell: PathBuf, - plugin_cwd: PathBuf, - capabilities: PluginCapabilities, - client_attributes: ClientAttributes, - default_shell: Option<TerminalAction>, - default_layout: Box<Layout>, + pub fn new( skip_cache: bool, - layout_dir: Option<PathBuf>, - default_mode: InputMode, - keybinds: Keybinds, - ) -> Result<()> { - let err_context = || format!("failed to start plugin {plugin_id} for client {client_id}"); - let mut plugin_loader = PluginLoader::new( - &plugin_cache, - loading_indication, - &senders, - plugin_id, - client_id, - engine, - plugin.clone(), - &plugin_dir, - tab_index, - size, - path_to_default_shell, - plugin_cwd, - capabilities, - client_attributes, - default_shell, - default_layout, - layout_dir, - default_mode, - keybinds, - )?; - if skip_cache { - plugin_loader - .compile_module() - .and_then(|module| plugin_loader.create_plugin_environment(module)) - .and_then(|(store, instance)| { - plugin_loader.load_plugin_instance(store, &instance, &plugin_map) - }) - .and_then(|_| { - plugin_loader.clone_instance_for_other_clients( - &connected_clients.lock().unwrap(), - &plugin_map, - ) - }) - .with_context(err_context)?; - } else { - plugin_loader - .load_module_from_memory() - .or_else(|_e| plugin_loader.load_module_from_hd_cache()) - .or_else(|_e| plugin_loader.compile_module()) - .and_then(|module| plugin_loader.create_plugin_environment(module)) - .and_then(|(store, instance)| { - plugin_loader.load_plugin_instance(store, &instance, &plugin_map) - }) - .and_then(|_| { - plugin_loader.clone_instance_for_other_clients( - &connected_clients.lock().unwrap(), - &plugin_map, - ) - }) - .with_context(err_context)?; - }; - display_loading_stage!(end, loading_indication, senders, plugin_id); - Ok(()) - } - - pub fn add_client( - client_id: ClientId, - plugin_dir: PathBuf, - plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>, + loading_context: LoadingContext, senders: ThreadSenders, engine: Engine, - plugin_map: Arc<Mutex<PluginMap>>, - connected_clients: Arc<Mutex<Vec<ClientId>>>, - loading_indication: &mut LoadingIndication, - path_to_default_shell: PathBuf, - plugin_cwd: PathBuf, - capabilities: PluginCapabilities, - client_attributes: ClientAttributes, - default_shell: Option<TerminalAction>, default_layout: Box<Layout>, - layout_dir: Option<PathBuf>, - default_mode: InputMode, - keybinds: Keybinds, - ) -> Result<()> { - let mut new_plugins = HashSet::new(); - for plugin_id in plugin_map.lock().unwrap().plugin_ids() { - new_plugins.insert((plugin_id, client_id)); - } - for (plugin_id, existing_client_id) in new_plugins { - let mut plugin_loader = PluginLoader::new_from_different_client_id( - &plugin_cache, - &plugin_map, - loading_indication, - &senders, - plugin_id, - existing_client_id, - engine.clone(), - &plugin_dir, - path_to_default_shell.clone(), - plugin_cwd.clone(), - capabilities.clone(), - client_attributes.clone(), - default_shell.clone(), - default_layout.clone(), - layout_dir.clone(), - default_mode, - keybinds.clone(), - )?; - plugin_loader - .load_module_from_memory() - .and_then(|module| plugin_loader.create_plugin_environment(module)) - .and_then(|(store, instance)| { - plugin_loader.load_plugin_instance(store, &instance, &plugin_map) - })? - } - connected_clients.lock().unwrap().push(client_id); - Ok(()) - } - - pub fn reload_plugin( - plugin_id: PluginId, - plugin_dir: PathBuf, - plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>, - senders: ThreadSenders, - engine: Engine, - plugin_map: Arc<Mutex<PluginMap>>, + plugin_cache: PluginCache, + plugin_map: &'a mut PluginMap, connected_clients: Arc<Mutex<Vec<ClientId>>>, - loading_indication: &mut LoadingIndication, - path_to_default_shell: PathBuf, - capabilities: PluginCapabilities, - client_attributes: ClientAttributes, - default_shell: Option<TerminalAction>, - default_layout: Box<Layout>, - layout_dir: Option<PathBuf>, - ) -> Result<()> { - let err_context = || format!("failed to reload plugin id {plugin_id}"); - - let mut connected_clients: Vec<ClientId> = - connected_clients.lock().unwrap().iter().copied().collect(); - if connected_clients.is_empty() { - return Err(anyhow!("No connected clients, cannot reload plugin")); - } - let first_client_id = connected_clients.remove(0); + ) -> Self { + let loading_indication = LoadingIndication::new("".into()); + create_plugin_fs_entries( + &loading_context.plugin_own_data_dir, + &loading_context.plugin_own_cache_dir, + ); + Self { + plugin_id: loading_context.plugin_id, + client_id: loading_context.client_id, + plugin_cwd: loading_context.plugin_cwd, + plugin_own_data_dir: loading_context.plugin_own_data_dir, + plugin_own_cache_dir: loading_context.plugin_own_cache_dir, + plugin_config: loading_context.plugin_config, + tab_index: loading_context.tab_index, + path_to_default_shell: loading_context.path_to_default_shell, + capabilities: loading_context.capabilities, + client_attributes: loading_context.client_attributes, + default_shell: loading_context.default_shell, + layout_dir: loading_context.layout_dir, + default_mode: loading_context.default_mode, + keybinds: loading_context.keybinds, + plugin_dir: loading_context.plugin_dir, + size: loading_context.size, - let mut plugin_loader = PluginLoader::new_from_existing_plugin_attributes( - &plugin_cache, - &plugin_map, - loading_indication, - &senders, - plugin_id, - first_client_id, - engine, - &plugin_dir, - path_to_default_shell, - None, - capabilities, - client_attributes, - default_shell, - default_layout, - layout_dir, - )?; - plugin_loader - .compile_module() - .and_then(|module| plugin_loader.create_plugin_environment(module)) - .and_then(|(store, instance)| { - plugin_loader.load_plugin_instance(store, &instance, &plugin_map) - }) - .and_then(|_| { - plugin_loader.clone_instance_for_other_clients(&connected_clients, &plugin_map) - }) - .with_context(err_context)?; - display_loading_stage!(end, loading_indication, senders, plugin_id); - Ok(()) - } - pub fn new( - plugin_cache: &Arc<Mutex<HashMap<PathBuf, Module>>>, - loading_indication: &'a mut LoadingIndication, - senders: &ThreadSenders, - plugin_id: PluginId, - client_id: ClientId, - engine: Engine, - plugin: PluginConfig, - plugin_dir: &'a PathBuf, - tab_index: Option<usize>, - size: Size, - path_to_default_shell: PathBuf, - plugin_cwd: PathBuf, - capabilities: PluginCapabilities, - client_attributes: ClientAttributes, - default_shell: Option<TerminalAction>, - default_layout: Box<Layout>, - layout_dir: Option<PathBuf>, - default_mode: InputMode, - keybinds: Keybinds, - ) -> Result<Self> { - let plugin_own_data_dir = ZELLIJ_SESSION_CACHE_DIR - .join(Url::from(&plugin.location).to_string()) - .join(format!("{}-{}", plugin_id, client_id)); - let plugin_own_cache_dir = ZELLIJ_SESSION_CACHE_DIR - .join(Url::from(&plugin.location).to_string()) - .join(format!("plugin_cache")); - create_plugin_fs_entries(&plugin_own_data_dir, &plugin_own_cache_dir)?; - let plugin_path = plugin.path.clone(); - Ok(PluginLoader { - plugin_cache: plugin_cache.clone(), - plugin_path, - loading_indication, - senders: senders.clone(), - plugin_id, - client_id, - engine, - plugin, - plugin_dir, - tab_index, - plugin_own_data_dir, - plugin_own_cache_dir, - size, - wasm_blob_on_hd: None, - path_to_default_shell, - plugin_cwd, - capabilities, - client_attributes, - default_shell, - default_layout, - layout_dir, - default_mode, - keybinds, - }) - } - pub fn new_from_existing_plugin_attributes( - plugin_cache: &Arc<Mutex<HashMap<PathBuf, Module>>>, - plugin_map: &Arc<Mutex<PluginMap>>, - loading_indication: &'a mut LoadingIndication, - senders: &ThreadSenders, - plugin_id: PluginId, - client_id: ClientId, - engine: Engine, - plugin_dir: &'a PathBuf, - path_to_default_shell: PathBuf, - cwd: Option<PathBuf>, - capabilities: PluginCapabilities, - client_attributes: ClientAttributes, - default_shell: Option<TerminalAction>, - default_layout: Box<Layout>, - layout_dir: Option<PathBuf>, - ) -> Result<Self> { - let err_context = || "Failed to find existing plugin"; - let (running_plugin, _subscriptions, _workers) = { - let mut plugin_map = plugin_map.lock().unwrap(); - plugin_map - .remove_single_plugin(plugin_id, client_id) - .with_context(err_context)? - }; - let running_plugin = running_plugin.lock().unwrap(); - let tab_index = running_plugin.store.data().tab_index; - let size = Size { - rows: running_plugin.rows, - cols: running_plugin.columns, - }; - let keybinds = running_plugin.store.data().keybinds.clone(); - let default_mode = running_plugin.store.data().default_mode; - let plugin_config = running_plugin.store.data().plugin.clone(); - // prefer the explicitly given cwd, otherwise copy it from the running plugin - // (when reloading a plugin, we want to copy it, when starting a new plugin instance from - // meomory, we want to reset it) - let plugin_cwd = cwd.unwrap_or_else(|| running_plugin.store.data().plugin_cwd.clone()); - loading_indication.set_name(running_plugin.store.data().name()); - PluginLoader::new( - plugin_cache, - loading_indication, + skip_cache, senders, - plugin_id, - client_id, engine, - plugin_config, - plugin_dir, - tab_index, - size, - path_to_default_shell, - plugin_cwd, - capabilities, - client_attributes, - default_shell, default_layout, - layout_dir, - default_mode, - keybinds, - ) - } - pub fn new_from_different_client_id( - plugin_cache: &Arc<Mutex<HashMap<PathBuf, Module>>>, - plugin_map: &Arc<Mutex<PluginMap>>, - loading_indication: &'a mut LoadingIndication, - senders: &ThreadSenders, - plugin_id: PluginId, - client_id: ClientId, - engine: Engine, - plugin_dir: &'a PathBuf, - path_to_default_shell: PathBuf, - plugin_cwd: PathBuf, - capabilities: PluginCapabilities, - client_attributes: ClientAttributes, - default_shell: Option<TerminalAction>, - default_layout: Box<Layout>, - layout_dir: Option<PathBuf>, - default_mode: InputMode, - keybinds: Keybinds, - ) -> Result<Self> { - let err_context = || "Failed to find existing plugin"; - let running_plugin = { - let plugin_map = plugin_map.lock().unwrap(); - plugin_map - .get_running_plugin(plugin_id, None) - .with_context(err_context)? - .clone() - }; - let running_plugin = running_plugin.lock().unwrap(); - let tab_index = running_plugin.store.data().tab_index; - let size = Size { - rows: running_plugin.rows, - cols: running_plugin.columns, - }; - let plugin_config = running_plugin.store.data().plugin.clone(); - loading_indication.set_name(running_plugin.store.data().name()); - PluginLoader::new( plugin_cache, + plugin_map, + connected_clients: Some(connected_clients), loading_indication, - senders, - plugin_id, - client_id, - engine, - plugin_config, - plugin_dir, - tab_index, - size, - path_to_default_shell, - plugin_cwd, - capabilities, - client_attributes, - default_shell, - default_layout, - layout_dir, - default_mode, - keybinds, - ) + } } - pub fn load_module_from_memory(&mut self) -> Result<Module> { - display_loading_stage!( - indicate_loading_plugin_from_memory, - self.loading_indication, - self.senders, - self.plugin_id - ); - let module = self - .plugin_cache - .lock() - .unwrap() - .remove(&self.plugin_path) - .ok_or(anyhow!("Plugin is not stored in memory"))?; - display_loading_stage!( - indicate_loading_plugin_from_memory_success, - self.loading_indication, - self.senders, - self.plugin_id - ); - Ok(module) + pub fn without_connected_clients(mut self) -> Self { + self.connected_clients = None; + self } - pub fn load_module_from_hd_cache(&mut self) -> Result<Module> { - display_loading_stage!( - indicate_loading_plugin_from_memory_notfound, - self.loading_indication, - self.senders, - self.plugin_id - ); - display_loading_stage!( - indicate_loading_plugin_from_hd_cache, - self.loading_indication, - self.senders, - self.plugin_id - ); - let (_wasm_bytes, cached_path) = self.plugin_bytes_and_cache_path()?; - let timer = std::time::Instant::now(); - let file_in_cache = std::fs::read(&cached_path)?; - let module = unsafe { Module::deserialize(&self.engine, file_in_cache)? }; - log::info!( - "Loaded plugin '{}' from cache folder at '{}' in {:?}", - self.plugin_path.display(), - ZELLIJ_CACHE_DIR.display(), - timer.elapsed(), - ); - display_loading_stage!( - indicate_loading_plugin_from_hd_cache_success, - self.loading_indication, - self.senders, - self.plugin_id - ); - Ok(module) + pub fn start_plugin(&mut self) -> Result<()> { + let module = if self.skip_cache { + self.interpret_module()? + } else { + self.load_module_from_memory() + .or_else(|_e| self.interpret_module())? + }; + let (store, instance) = self.create_plugin_environment(module)?; + self.load_plugin_instance(store, &instance)?; + self.clone_instance_for_other_clients()?; + Ok(()) } - pub fn compile_module(&mut self) -> Result<Module> { + fn interpret_module(&mut self) -> Result<Module> { self.loading_indication.override_previous_error(); - display_loading_stage!( - indicate_loading_plugin_from_hd_cache_notfound, - self.loading_indication, - self.senders, - self.plugin_id - ); - display_loading_stage!( - indicate_compiling_plugin, - self.loading_indication, - self.senders, - self.plugin_id - ); - let (wasm_bytes, cached_path) = self.plugin_bytes_and_cache_path()?; + let wasm_bytes = self.plugin_config.resolve_wasm_bytes(&self.plugin_dir)?; let timer = std::time::Instant::now(); - let err_context = || "failed to recover cache dir"; - let module = fs::create_dir_all(ZELLIJ_PLUGIN_ARTIFACT_DIR.as_path()) - .map_err(anyError::new) - .and_then(|_| { - // compile module - Module::new(&self.engine, &wasm_bytes) - }) - .and_then(|m| { - // serialize module to HD cache for faster loading in the future - fs::write(&cached_path, m.serialize()?).map_err(anyError::new)?; - log::info!( - "Compiled plugin '{}' in {:?}", - self.plugin_path.display(), - timer.elapsed() - ); - Ok(m) - }) - .with_context(err_context)?; + let module = Module::new(&self.engine, &wasm_bytes)?; + log::info!( + "Loaded plugin '{}' in {:?}", + self.plugin_config.path.display(), + timer.elapsed() + ); Ok(module) } - pub fn create_plugin_environment( - &mut self, - module: Module, - ) -> Result<(Store<PluginEnv>, Instance)> { - let (store, instance) = self.create_plugin_instance_env(&module)?; - // Only do an insert when everything went well! - let cloned_plugin = self.plugin.clone(); - self.plugin_cache - .lock() - .unwrap() - .insert(cloned_plugin.path, module); - Ok((store, instance)) - } - pub fn create_plugin_instance_and_wasi_env_for_worker( - &mut self, - ) -> Result<(Store<PluginEnv>, Instance)> { - let err_context = || { - format!( - "Failed to create instance and plugin env for worker {}", - self.plugin_id - ) - }; + fn load_module_from_memory(&mut self) -> Result<Module> { let module = self .plugin_cache .lock() .unwrap() - .get(&self.plugin.path) - .with_context(err_context)? - .clone(); - let (store, instance) = self.create_plugin_instance_env(&module)?; - Ok((store, instance)) + .remove(&self.plugin_config.path) // TODO: do we still bring it back later? + // maybe we can forgo this dance? + .ok_or(anyhow!("Plugin is not stored in memory"))?; + Ok(module) } - pub fn load_plugin_instance( + fn load_plugin_instance( &mut self, mut store: Store<PluginEnv>, instance: &Instance, - plugin_map: &Arc<Mutex<PluginMap>>, ) -> Result<()> { let err_context = || format!("failed to load plugin from instance {instance:#?}"); let main_user_instance = instance.clone(); - display_loading_stage!( - indicate_starting_plugin, - self.loading_indication, - self.senders, - self.plugin_id - ); let start_function = instance .get_typed_func::<(), ()>(&mut store, "_start") .with_context(err_context)?; @@ -641,7 +182,6 @@ impl<'a> PluginLoader<'a> { if function_name.ends_with("_worker") { let (mut store, instance) = self.create_plugin_instance_and_wasi_env_for_worker()?; - let start_function_for_worker = instance .get_typed_func::<(), ()>(&mut store, "_start") .with_context(err_context)?; @@ -662,7 +202,7 @@ impl<'a> PluginLoader<'a> { self.size.rows, self.size.cols, ))); - plugin_map.lock().unwrap().insert( + self.plugin_map.insert( self.plugin_id, self.client_id, plugin.clone(), @@ -675,7 +215,7 @@ impl<'a> PluginLoader<'a> { .with_context(err_context)?; let protobuf_plugin_configuration: ProtobufPluginConfiguration = self - .plugin + .plugin_config .userspace_configuration .clone() .try_into() @@ -687,143 +227,124 @@ impl<'a> PluginLoader<'a> { .call(&mut plugin.lock().unwrap().store, ()) .with_context(err_context)?; - display_loading_stage!( - indicate_starting_plugin_success, - self.loading_indication, - self.senders, - self.plugin_id - ); - display_loading_stage!( - indicate_writing_plugin_to_cache, - self.loading_indication, - self.senders, - self.plugin_id - ); - display_loading_stage!( - indicate_writing_plugin_to_cache_success, - self.loading_indication, - self.senders, - self.plugin_id - ); - Ok(()) } - pub fn clone_instance_for_other_clients( - &mut self, - connected_clients: &[ClientId], - plugin_map: &Arc<Mutex<PluginMap>>, - ) -> Result<()> { - if !connected_clients.is_empty() { - display_loading_stage!( - indicate_cloning_plugin_for_other_clients, - self.loading_indication, - self.senders, + pub fn create_plugin_environment( + &self, + module: Module, + ) -> Result<(Store<PluginEnv>, Instance)> { + let err_context = || { + format!( + "Failed to create instance, plugin env and subscriptions for plugin {}", self.plugin_id - ); + ) + }; + let stdin_pipe = Arc::new(Mutex::new(VecDeque::new())); + let stdout_pipe = Arc::new(Mutex::new(VecDeque::new())); + + let wasi_ctx = PluginLoader::create_wasi_ctx( + &self.plugin_cwd, + &self.plugin_own_data_dir, + &self.plugin_own_cache_dir, + &ZELLIJ_TMP_DIR, + &self.plugin_config.location.to_string(), + self.plugin_id, + stdin_pipe.clone(), + stdout_pipe.clone(), + )?; + let plugin_path = self.plugin_config.path.clone(); + let plugin_env = PluginEnv { + plugin_id: self.plugin_id, + client_id: self.client_id, + plugin: self.plugin_config.clone(), // TODO: change field name in PluginEnv to plugin_config + permissions: Arc::new(Mutex::new(None)), + senders: self.senders.clone(), + wasi_ctx, + plugin_own_data_dir: self.plugin_own_data_dir.clone(), + plugin_own_cache_dir: self.plugin_own_cache_dir.clone(), + tab_index: self.tab_index, + path_to_default_shell: self.path_to_default_shell.clone(), + capabilities: self.capabilities.clone(), + client_attributes: self.client_attributes.clone(), + default_shell: self.default_shell.clone(), + default_layout: self.default_layout.clone(), + plugin_cwd: self.plugin_cwd.clone(), + input_pipes_to_unblock: Arc::new(Mutex::new(HashSet::new())), + input_pipes_to_block: Arc::new(Mutex::new(HashSet::new())), + layout_dir: self.layout_dir.clone(), + default_mode: self.default_mode.clone(), + subscriptions: Arc::new(Mutex::new(HashSet::new())), + keybinds: self.keybinds.clone(), + intercepting_key_presses: false, + stdin_pipe, + stdout_pipe, + store_limits: create_optimized_store_limits(), + }; + let mut store = Store::new(&self.engine, plugin_env); + + // Apply optimized resource limits for memory efficiency + store.limiter(|plugin_env| &mut plugin_env.store_limits); + + let mut linker = Linker::new(&self.engine); + wasmi_wasi::add_to_linker(&mut linker, |plugin_env: &mut PluginEnv| { + &mut plugin_env.wasi_ctx + })?; + zellij_exports(&mut linker); + + let instance = linker + .instantiate_and_start(&mut store, &module) + .with_context(err_context)?; + + if let Some(func) = instance.get_func(&mut store, "_initialize") { + if let Ok(typed_func) = func.typed::<(), ()>(&store) { + let _ = typed_func.call(&mut store, ()); + } + } + + self.plugin_cache + .lock() + .unwrap() + .insert(plugin_path.clone(), module); + Ok((store, instance)) + } + pub fn clone_instance_for_other_clients(&mut self) -> Result<()> { + let Some(connected_clients) = self.connected_clients.as_ref() else { + return Ok(()); + }; + let connected_clients: Vec<ClientId> = + connected_clients.lock().unwrap().iter().copied().collect(); + if !connected_clients.is_empty() { + self.connected_clients = None; // so we don't have infinite loops for client_id in connected_clients { - if client_id == &self.client_id { + if client_id == self.client_id { // don't reload the plugin once more for ourselves continue; } - let mut loading_indication = LoadingIndication::new("".into()); - let mut plugin_loader_for_client = PluginLoader::new_from_different_client_id( - &self.plugin_cache.clone(), - &plugin_map, - &mut loading_indication, - &self.senders.clone(), - self.plugin_id, - *client_id, - self.engine.clone(), - &self.plugin_dir, - self.path_to_default_shell.clone(), - self.plugin_cwd.clone(), - self.capabilities.clone(), - self.client_attributes.clone(), - self.default_shell.clone(), - self.default_layout.clone(), - self.layout_dir.clone(), - self.default_mode, - self.keybinds.clone(), - )?; - plugin_loader_for_client - .load_module_from_memory() - .and_then(|module| plugin_loader_for_client.create_plugin_environment(module)) - .and_then(|(store, instance)| { - plugin_loader_for_client.load_plugin_instance(store, &instance, plugin_map) - })? + self.client_id = client_id; + self.start_plugin()?; } - display_loading_stage!( - indicate_cloning_plugin_for_other_clients_success, - self.loading_indication, - self.senders, - self.plugin_id - ); } Ok(()) } - fn plugin_bytes_and_cache_path(&mut self) -> Result<(Vec<u8>, PathBuf)> { - match self.wasm_blob_on_hd.as_ref() { - Some((wasm_bytes, cached_path)) => Ok((wasm_bytes.clone(), cached_path.clone())), - None => { - if self.plugin._allow_exec_host_cmd { - info!( - "Plugin({:?}) is able to run any host command, this may lead to some security issues!", - self.plugin.path - ); - } - // The plugins blob as stored on the filesystem - let wasm_bytes = self.plugin.resolve_wasm_bytes(&self.plugin_dir)?; - let hash: String = PortableHash::default() - .hash256(&wasm_bytes) - .iter() - .map(ToString::to_string) - .collect(); - let cached_path = ZELLIJ_PLUGIN_ARTIFACT_DIR.join(&hash); - self.wasm_blob_on_hd = Some((wasm_bytes.clone(), cached_path.clone())); - Ok((wasm_bytes, cached_path)) - }, - } - } - pub fn create_wasi_ctx( - host_dir: &PathBuf, - data_dir: &PathBuf, - cache_dir: &PathBuf, - tmp_dir: &PathBuf, - plugin_url: &String, - plugin_id: PluginId, - stdin_pipe: Arc<Mutex<VecDeque<u8>>>, - stdout_pipe: Arc<Mutex<VecDeque<u8>>>, - ) -> Result<WasiP1Ctx> { - let err_context = || format!("Failed to create wasi_ctx"); - let dirs = vec![ - ("/host".to_owned(), host_dir.clone()), - ("/data".to_owned(), data_dir.clone()), - ("/cache".to_owned(), cache_dir.clone()), - ("/tmp".to_owned(), tmp_dir.clone()), - ]; - let dirs = dirs.into_iter().filter(|(_dir_name, dir)| { - // note that this does not protect against TOCTOU errors - // eg. if one or more of these folders existed at the time of check but was deleted - // before we mounted in in the wasi environment, we'll crash - // when we move to a new wasi environment, we should address this with locking if - // there's no built-in solution - dir.try_exists().ok().unwrap_or(false) - }); - let mut wasi_ctx_builder = WasiCtxBuilder::new(); - wasi_ctx_builder.env("CLICOLOR_FORCE", "1"); - for (guest_path, host_path) in dirs { - wasi_ctx_builder - .preopened_dir(host_path, guest_path, DirPerms::all(), FilePerms::all()) - .with_context(err_context)?; - } - wasi_ctx_builder - .stdin(VecDequeInputStream(stdin_pipe.clone())) - .stdout(WriteOutputStream(stdout_pipe.clone())) - .stderr(WriteOutputStream(Arc::new(Mutex::new(LoggingPipe::new( - plugin_url, plugin_id, - ))))); - let wasi_ctx = wasi_ctx_builder.build_p1(); - Ok(wasi_ctx) + pub fn create_plugin_instance_and_wasi_env_for_worker( + &self, + ) -> Result<(Store<PluginEnv>, Instance)> { + let plugin_id = self.plugin_id; + let err_context = || { + format!( + "Failed to create instance and plugin env for worker {}", + plugin_id + ) + }; + let module = self + .plugin_cache + .lock() + .unwrap() + .get(&self.plugin_config.path) + .with_context(err_context)? + .clone(); + let (store, instance) = self.create_plugin_instance_env(&module)?; + Ok((store, instance)) } fn create_plugin_instance_env(&self, module: &Module) -> Result<(Store<PluginEnv>, Instance)> { let err_context = || { @@ -840,16 +361,16 @@ impl<'a> PluginLoader<'a> { &self.plugin_own_data_dir, &self.plugin_own_cache_dir, &ZELLIJ_TMP_DIR, - &self.plugin.location.to_string(), + &self.plugin_config.location.to_string(), self.plugin_id, stdin_pipe.clone(), stdout_pipe.clone(), )?; - let plugin = self.plugin.clone(); + let plugin_config = self.plugin_config.clone(); let plugin_env = PluginEnv { plugin_id: self.plugin_id, client_id: self.client_id, - plugin, + plugin: plugin_config, permissions: Arc::new(Mutex::new(None)), senders: self.senders.clone(), wasi_ctx, @@ -871,43 +392,97 @@ impl<'a> PluginLoader<'a> { intercepting_key_presses: false, stdin_pipe, stdout_pipe, + store_limits: create_optimized_store_limits(), }; let mut store = Store::new(&self.engine, plugin_env); + // Apply optimized resource limits for memory efficiency + store.limiter(|plugin_env| &mut plugin_env.store_limits); + let mut linker = Linker::new(&self.engine); - wasmtime_wasi::preview1::add_to_linker_sync(&mut linker, |plugin_env: &mut PluginEnv| { + wasmi_wasi::add_to_linker(&mut linker, |plugin_env: &mut PluginEnv| { &mut plugin_env.wasi_ctx - }) - .unwrap(); + })?; zellij_exports(&mut linker); let instance = linker - .instantiate(&mut store, module) + .instantiate_and_start(&mut store, module) .with_context(err_context)?; if let Some(func) = instance.get_func(&mut store, "_initialize") { - func.typed::<(), ()>(&store)?.call(&mut store, ())?; + if let Ok(typed_func) = func.typed::<(), ()>(&store) { + let _ = typed_func.call(&mut store, ()); + } } Ok((store, instance)) } + pub fn create_wasi_ctx( + host_dir: &PathBuf, + data_dir: &PathBuf, + cache_dir: &PathBuf, + tmp_dir: &PathBuf, + plugin_url: &String, + plugin_id: PluginId, + stdin_pipe: Arc<Mutex<VecDeque<u8>>>, + stdout_pipe: Arc<Mutex<VecDeque<u8>>>, + ) -> Result<WasiCtx> { + let _err_context = || format!("Failed to create wasi_ctx"); + let dirs = vec![ + ("/host".to_owned(), host_dir.clone()), + ("/data".to_owned(), data_dir.clone()), + ("/cache".to_owned(), cache_dir.clone()), + ("/tmp".to_owned(), tmp_dir.clone()), + ]; + let dirs = dirs.into_iter().filter(|(_dir_name, dir)| { + // note that this does not protect against TOCTOU errors + // eg. if one or more of these folders existed at the time of check but was deleted + // before we mounted in in the wasi environment, we'll crash + // when we move to a new wasi environment, we should address this with locking if + // there's no built-in solution + dir.try_exists().ok().unwrap_or(false) + }); + + let mut builder = WasiCtxBuilder::new(); + builder.inherit_env()?; + + // Mount directories using the builder + for (guest_path, host_path) in dirs { + match std::fs::File::open(&host_path) { + Ok(dir_file) => { + let dir = Dir::from_std_file(dir_file); + builder.preopened_dir(dir, guest_path)?; + }, + Err(e) => { + log::warn!("Failed to mount directory {:?}: {}", host_path, e); + }, + } + } + + let ctx = builder.build(); + + // Set up custom stdin/stdout/stderr + ctx.set_stdin(Box::new(ReadPipe::new(VecDequeInputStream( + stdin_pipe.clone(), + )))); + ctx.set_stdout(Box::new(WritePipe::new(WriteOutputStream( + stdout_pipe.clone(), + )))); + ctx.set_stderr(Box::new(WritePipe::new(WriteOutputStream(Arc::new( + Mutex::new(LoggingPipe::new(plugin_url, plugin_id)), + ))))); + + Ok(ctx) + } } -fn create_plugin_fs_entries( - plugin_own_data_dir: &PathBuf, - plugin_own_cache_dir: &PathBuf, -) -> Result<()> { - let err_context = || "failed to create plugin fs entries"; - // Create filesystem entries mounted into WASM. - // We create them here to get expressive error messages in case they fail. - fs::create_dir_all(&plugin_own_data_dir) - .with_context(|| format!("failed to create datadir in {plugin_own_data_dir:?}")) - .with_context(err_context)?; - fs::create_dir_all(&plugin_own_cache_dir) - .with_context(|| format!("failed to create cache dir in {plugin_own_cache_dir:?}")) - .with_context(err_context)?; - fs::create_dir_all(ZELLIJ_TMP_DIR.as_path()) - .with_context(|| format!("failed to create tmpdir at {:?}", &ZELLIJ_TMP_DIR.as_path())) - .with_context(err_context)?; - Ok(()) +fn create_optimized_store_limits() -> StoreLimits { + use wasmi::StoreLimitsBuilder; + StoreLimitsBuilder::new() + .instances(1) // One instance per plugin + .memories(4) // Max 4 linear memories per plugin + .memory_size(16 * 1024 * 1024) // 16MB per memory maximum + .tables(16) // Small table element limit + .trap_on_grow_failure(true) // Fail fast on resource exhaustion + .build() } diff --git a/zellij-server/src/plugins/plugin_map.rs b/zellij-server/src/plugins/plugin_map.rs index a92c291a..7520114f 100644 --- a/zellij-server/src/plugins/plugin_map.rs +++ b/zellij-server/src/plugins/plugin_map.rs @@ -1,18 +1,13 @@ use crate::plugins::plugin_worker::MessageToWorker; use crate::plugins::PluginId; -use bytes::Bytes; use std::io::Write; use std::{ collections::{BTreeMap, HashMap, HashSet, VecDeque}, path::PathBuf, sync::{Arc, Mutex}, }; -use wasmtime::{Instance, Store}; -use wasmtime_wasi::preview1::WasiP1Ctx; -use wasmtime_wasi::{ - HostInputStream, HostOutputStream, StdinStream, StdoutStream, StreamError, StreamResult, - Subscribe, -}; +use wasmi::{Instance, Store, StoreLimits}; +use wasmi_wasi::WasiCtx; use crate::{thread_bus::ThreadSenders, ClientId}; @@ -70,17 +65,6 @@ impl PluginMap { } removed } - pub fn remove_single_plugin( - &mut self, - plugin_id: PluginId, - client_id: ClientId, - ) -> Option<( - Arc<Mutex<RunningPlugin>>, - Arc<Mutex<Subscriptions>>, - HashMap<String, Sender<MessageToWorker>>, - )> { - self.plugin_assets.remove(&(plugin_id, client_id)) - } pub fn plugin_ids(&self) -> Vec<PluginId> { let mut unique_plugins: HashSet<PluginId> = self .plugin_assets @@ -293,7 +277,7 @@ pub struct PluginEnv { pub plugin: PluginConfig, pub permissions: Arc<Mutex<Option<HashSet<PermissionType>>>>, pub senders: ThreadSenders, - pub wasi_ctx: WasiP1Ctx, + pub wasi_ctx: WasiCtx, pub tab_index: Option<usize>, pub client_id: ClientId, #[allow(dead_code)] @@ -314,34 +298,23 @@ pub struct PluginEnv { pub stdout_pipe: Arc<Mutex<VecDeque<u8>>>, pub keybinds: Keybinds, pub intercepting_key_presses: bool, + pub store_limits: StoreLimits, } #[derive(Clone)] pub struct VecDequeInputStream(pub Arc<Mutex<VecDeque<u8>>>); -impl StdinStream for VecDequeInputStream { - fn stream(&self) -> Box<dyn wasmtime_wasi::HostInputStream> { - Box::new(self.clone()) - } - - fn isatty(&self) -> bool { - false - } -} - -impl HostInputStream for VecDequeInputStream { - fn read(&mut self, size: usize) -> StreamResult<Bytes> { +impl std::io::Read for VecDequeInputStream { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { let mut inner = self.0.lock().unwrap(); - let len = std::cmp::min(size, inner.len()); - Ok(Bytes::from_iter(inner.drain(0..len))) + let len = std::cmp::min(buf.len(), inner.len()); + for (i, byte) in inner.drain(0..len).enumerate() { + buf[i] = byte; + } + Ok(len) } } -#[async_trait::async_trait] -impl Subscribe for VecDequeInputStream { - async fn ready(&mut self) {} -} - pub struct WriteOutputStream<T>(pub Arc<Mutex<T>>); impl<T> Clone for WriteOutputStream<T> { @@ -350,43 +323,18 @@ impl<T> Clone for WriteOutputStream<T> { } } -impl<T: Write + Send + 'static> StdoutStream for WriteOutputStream<T> { - fn stream(&self) -> Box<dyn HostOutputStream> { - Box::new((*self).clone()) - } - - fn isatty(&self) -> bool { - false - } -} - -impl<T: Write + Send + 'static> HostOutputStream for WriteOutputStream<T> { - fn write(&mut self, bytes: Bytes) -> StreamResult<()> { - self.0 - .lock() - .unwrap() - .write_all(&*bytes) - .map_err(|e| StreamError::LastOperationFailed(e.into())) - } - - fn flush(&mut self) -> StreamResult<()> { - self.0 - .lock() - .unwrap() - .flush() - .map_err(|e| StreamError::LastOperationFailed(e.into())) +impl<T: Write + Send + 'static> std::io::Write for WriteOutputStream<T> { + fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { + let mut inner = self.0.lock().unwrap(); + inner.write(buf) } - fn check_write(&mut self) -> StreamResult<usize> { - Ok(usize::MAX) + fn flush(&mut self) -> std::io::Result<()> { + let mut inner = self.0.lock().unwrap(); + inner.flush() } } -#[async_trait::async_trait] -impl<T: Send + 'static> Subscribe for WriteOutputStream<T> { - async fn ready(&mut self) {} -} - impl PluginEnv { // Get the name (path) of the containing plugin pub fn name(&self) -> String { diff --git a/zellij-server/src/plugins/plugin_worker.rs b/zellij-server/src/plugins/plugin_worker.rs index 30085a3a..6e20a499 100644 --- a/zellij-server/src/plugins/plugin_worker.rs +++ b/zellij-server/src/plugins/plugin_worker.rs @@ -1,6 +1,6 @@ use crate::plugins::plugin_map::PluginEnv; use crate::plugins::zellij_exports::wasi_write_object; -use wasmtime::{Instance, Store}; +use wasmi::{Instance, Store}; use async_channel::{unbounded, Receiver, Sender}; use async_std::task; diff --git a/zellij-server/src/plugins/unit/plugin_tests.rs b/zellij-server/src/plugins/unit/plugin_tests.rs index da725443..9631da91 100644 --- a/zellij-server/src/plugins/unit/plugin_tests.rs +++ b/zellij-server/src/plugins/unit/plugin_tests.rs @@ -6,7 +6,7 @@ use lazy_static::lazy_static; use std::collections::BTreeMap; use std::path::PathBuf; use tempfile::tempdir; -use wasmtime::Engine; +use wasmi::Engine; use zellij_utils::data::{ BareKey, Event, InputMode, KeyWithModifier, PermissionStatus, PermissionType, PluginCapabilities, @@ -333,7 +333,10 @@ fn create_plugin_thread( None, ) .should_silently_fail(); - let engine = Engine::new(wasmtime::Config::new().strategy(wasmtime::Strategy::Winch)).unwrap(); + let mut config = wasmi::Config::default(); + config.set_max_stack_height(1024 * 1024); + config.set_max_recursion_depth(1000); + let engine = Engine::new(&config); let data_dir = PathBuf::from(tempdir().unwrap().path()); let default_shell = PathBuf::from("."); let plugin_capabilities = PluginCapabilities::default(); @@ -427,7 +430,10 @@ fn create_plugin_thread_with_server_receiver( None, ) .should_silently_fail(); - let engine = Engine::new(wasmtime::Config::new().strategy(wasmtime::Strategy::Winch)).unwrap(); + let mut config = wasmi::Config::default(); + config.set_max_stack_height(1024 * 1024); + config.set_max_recursion_depth(1000); + let engine = Engine::new(&config); let data_dir = PathBuf::from(tempdir().unwrap().path()); let default_shell = PathBuf::from("."); let plugin_capabilities = PluginCapabilities::default(); @@ -517,7 +523,10 @@ fn create_plugin_thread_with_pty_receiver( None, ) .should_silently_fail(); - let engine = Engine::new(wasmtime::Config::new().strategy(wasmtime::Strategy::Winch)).unwrap(); + let mut config = wasmi::Config::default(); + config.set_max_stack_height(1024 * 1024); + config.set_max_recursion_depth(1000); + let engine = Engine::new(&config); let data_dir = PathBuf::from(tempdir().unwrap().path()); let default_shell = PathBuf::from("."); let plugin_capabilities = PluginCapabilities::default(); @@ -602,7 +611,10 @@ fn create_plugin_thread_with_background_jobs_receiver( None, ) .should_silently_fail(); - let engine = Engine::new(wasmtime::Config::new().strategy(wasmtime::Strategy::Winch)).unwrap(); + let mut config = wasmi::Config::default(); + config.set_max_stack_height(1024 * 1024); + config.set_max_recursion_depth(1000); + let engine = Engine::new(&config); let data_dir = PathBuf::from(tempdir().unwrap().path()); let default_shell = PathBuf::from("."); let plugin_capabilities = PluginCapabilities::default(); @@ -8666,7 +8678,7 @@ pub fn reload_plugin_plugin_command() { received_screen_instructions, ScreenInstruction::RequestStateUpdateForPlugins, // happens on successful plugin (re)load screen_receiver, - 3, + 2, &PermissionType::ChangeApplicationState, cache_path, plugin_thread_sender, @@ -8709,7 +8721,7 @@ pub fn reload_plugin_plugin_command() { } }) .count(); - assert_eq!(request_state_update_requests, 3); + assert_eq!(request_state_update_requests, 2); } #[test] @@ -8740,7 +8752,7 @@ pub fn load_new_plugin_plugin_command() { received_screen_instructions, ScreenInstruction::RequestStateUpdateForPlugins, // happens on successful plugin (re)load screen_receiver, - 3, + 2, &PermissionType::ChangeApplicationState, cache_path, plugin_thread_sender, @@ -8783,7 +8795,7 @@ pub fn load_new_plugin_plugin_command() { } }) .count(); - assert_eq!(request_state_update_requests, 3); + assert_eq!(request_state_update_requests, 2); } #[test] diff --git a/zellij-server/src/plugins/wasm_bridge.rs b/zellij-server/src/plugins/wasm_bridge.rs index ca9837a7..647e72ec 100644 --- a/zellij-server/src/plugins/wasm_bridge.rs +++ b/zellij-server/src/plugins/wasm_bridge.rs @@ -1,4 +1,4 @@ -use super::{PluginId, PluginInstruction}; +use super::{PinnedExecutor, PluginId, PluginInstruction}; use crate::plugins::pipes::{ apply_pipe_message_to_plugin, pipes_to_block_or_unblock, PendingPipes, PipeStateChange, }; @@ -9,18 +9,20 @@ use crate::plugins::plugin_worker::MessageToWorker; use crate::plugins::watch_filesystem::watch_filesystem; use crate::plugins::zellij_exports::{wasi_read_string, wasi_write_object}; use async_channel::Sender; -use async_std::task::{self, JoinHandle}; use highway::{HighwayHash, PortableHash}; use log::info; use notify_debouncer_full::{notify::RecommendedWatcher, Debouncer, FileIdMap}; +use once_cell::sync::OnceCell; use std::{ collections::{BTreeMap, HashMap, HashSet}, path::PathBuf, str::FromStr, sync::{Arc, Mutex}, }; -use wasmtime::{Engine, Module}; -use zellij_utils::consts::{ZELLIJ_CACHE_DIR, ZELLIJ_TMP_DIR}; +use tokio::runtime::Runtime; +use url::Url; +use wasmi::{Engine, Module}; +use zellij_utils::consts::{ZELLIJ_CACHE_DIR, ZELLIJ_SESSION_CACHE_DIR, ZELLIJ_TMP_DIR}; use zellij_utils::data::{ FloatingPaneCoordinates, InputMode, PaneContents, PaneRenderReport, PermissionStatus, PermissionType, PipeMessage, PipeSource, @@ -49,6 +51,20 @@ use zellij_utils::{ pane_size::Size, }; +// Global tokio runtime for async I/O operations (plugin downloads, timers) +static TOKIO_RUNTIME: OnceCell<Runtime> = OnceCell::new(); + +pub fn get_tokio_runtime() -> &'static Runtime { + TOKIO_RUNTIME.get_or_init(|| { + tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) // Small pool for I/O only + .thread_name("tokio-plugin-io") + .enable_all() + .build() + .expect("Failed to create tokio runtime") + }) +} + #[derive(Debug, Clone)] pub enum EventOrPipeMessage { Event(Event), @@ -79,13 +95,85 @@ impl PluginRenderAsset { } } +#[derive(Debug, Clone)] +pub struct LoadingContext { + pub plugin_id: PluginId, + pub client_id: ClientId, + pub plugin_cwd: PathBuf, + pub plugin_own_data_dir: PathBuf, + pub plugin_own_cache_dir: PathBuf, + pub plugin_config: PluginConfig, + pub tab_index: Option<usize>, + pub path_to_default_shell: PathBuf, + pub capabilities: PluginCapabilities, + pub client_attributes: ClientAttributes, + pub default_shell: Option<TerminalAction>, + pub layout_dir: Option<PathBuf>, + pub default_mode: InputMode, + pub keybinds: Keybinds, + pub plugin_dir: PathBuf, + pub size: Size, +} + +impl LoadingContext { + pub fn new( + wasm_bridge: &WasmBridge, + cwd: Option<PathBuf>, + plugin_config: PluginConfig, + plugin_id: PluginId, + client_id: ClientId, + tab_index: Option<usize>, + size: Size, + ) -> Self { + let plugin_own_data_dir = ZELLIJ_SESSION_CACHE_DIR + .join(Url::from(&plugin_config.location).to_string()) + .join(format!("{}-{}", plugin_id, client_id)); + let plugin_own_cache_dir = ZELLIJ_SESSION_CACHE_DIR + .join(Url::from(&plugin_config.location).to_string()) + .join(format!("plugin_cache")); + let default_mode = wasm_bridge + .base_modes + .get(&client_id) + .copied() + .unwrap_or(wasm_bridge.default_mode); + let keybinds = wasm_bridge + .keybinds + .get(&client_id) + .cloned() + .unwrap_or_else(|| wasm_bridge.default_keybinds.clone()); + + LoadingContext { + client_id, + plugin_id, + path_to_default_shell: wasm_bridge.path_to_default_shell.clone(), + plugin_cwd: cwd.unwrap_or_else(|| wasm_bridge.zellij_cwd.clone()), + capabilities: wasm_bridge.capabilities.clone(), + client_attributes: wasm_bridge.client_attributes.clone(), + default_shell: wasm_bridge.default_shell.clone(), + layout_dir: wasm_bridge.layout_dir.clone(), + keybinds, + default_mode, + plugin_own_data_dir, + plugin_own_cache_dir, + plugin_config, + tab_index, + plugin_dir: wasm_bridge.plugin_dir.clone(), + size, + } + } + pub fn update_plugin_path(&mut self, new_path: PathBuf) { + self.plugin_config.path = new_path; + } +} + +pub type PluginCache = Arc<Mutex<HashMap<PathBuf, Module>>>; + pub struct WasmBridge { connected_clients: Arc<Mutex<Vec<ClientId>>>, senders: ThreadSenders, - engine: Engine, plugin_dir: PathBuf, - plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>, plugin_map: Arc<Mutex<PluginMap>>, + plugin_executor: Arc<PinnedExecutor>, next_plugin_id: PluginId, plugin_ids_waiting_for_permission_request: HashSet<PluginId>, cached_events_for_pending_plugins: HashMap<PluginId, Vec<EventOrPipeMessage>>, @@ -94,7 +182,7 @@ pub struct WasmBridge { // worker_name, // message, // payload> - loading_plugins: HashMap<(PluginId, RunPlugin), JoinHandle<()>>, // plugin_id to join-handle + loading_plugins: HashSet<(PluginId, RunPlugin)>, // tracks loading plugins without handles pending_plugin_reloads: HashSet<RunPlugin>, path_to_default_shell: PathBuf, watcher: Option<Debouncer<RecommendedWatcher, FileIdMap>>, @@ -102,7 +190,6 @@ pub struct WasmBridge { capabilities: PluginCapabilities, client_attributes: ClientAttributes, default_shell: Option<TerminalAction>, - default_layout: Box<Layout>, cached_plugin_map: HashMap<RunPluginLocation, HashMap<PluginUserConfiguration, Vec<(PluginId, ClientId)>>>, pending_pipes: PendingPipes, @@ -136,13 +223,22 @@ impl WasmBridge { Arc::new(Mutex::new(HashMap::new())); let watcher = None; let downloader = Downloader::new(ZELLIJ_CACHE_DIR.to_path_buf()); + let max_threads = num_cpus::get().max(4).min(16); + let plugin_executor = Arc::new(PinnedExecutor::new( + max_threads, + &senders, + &plugin_map, + &connected_clients, + &default_layout, + &plugin_cache, + &engine, + )); WasmBridge { connected_clients, senders, - engine, plugin_dir, - plugin_cache, plugin_map, + plugin_executor, path_to_default_shell, watcher, next_plugin_id: 0, @@ -150,13 +246,12 @@ impl WasmBridge { plugin_ids_waiting_for_permission_request: HashSet::new(), cached_resizes_for_pending_plugins: HashMap::new(), cached_worker_messages: HashMap::new(), - loading_plugins: HashMap::new(), + loading_plugins: HashSet::new(), pending_plugin_reloads: HashSet::new(), zellij_cwd, capabilities, client_attributes, default_shell, - default_layout, cached_plugin_map: HashMap::new(), pending_pipes: Default::default(), layout_dir, @@ -176,13 +271,23 @@ impl WasmBridge { cwd: Option<PathBuf>, skip_cache: bool, client_id: Option<ClientId>, - cli_client_id: Option<ClientId>, ) -> Result<(PluginId, ClientId)> { - // returns the plugin id let err_context = move || format!("failed to load plugin"); let client_id = client_id + .and_then(|client_id| { + // first attempt to use a connected client (because this might be a cli_client that + // should not get plugins) and only if none is connected, load a "dummy" plugin for + // the cli client + let connected_clients = self.connected_clients.lock().unwrap(); + if connected_clients.contains(&client_id) { + Some(client_id) + } else { + None + } + }) .or_else(|| { + // if no client id was provided, try to use the first connected client self.connected_clients .lock() .unwrap() @@ -190,6 +295,9 @@ impl WasmBridge { .next() .copied() }) + .or(client_id) // if we got here, this is likely a cli client with no other clients + // connected, or loading a background plugin on app start, we use the provided client id as a dummy to load the + // plugin anyway .with_context(|| { "Plugins must have a client id, none was provided and none are connected" })?; @@ -198,7 +306,7 @@ impl WasmBridge { match run { Some(run) => { - let mut plugin = PluginConfig::from_run_plugin(run) + let plugin = PluginConfig::from_run_plugin(run) .with_context(|| format!("failed to resolve plugin {run:?}")) .with_context(err_context)?; let plugin_name = run.location.to_string(); @@ -207,33 +315,29 @@ impl WasmBridge { .insert(plugin_id, vec![]); self.cached_resizes_for_pending_plugins .insert(plugin_id, (size.rows, size.cols)); + self.loading_plugins.insert((plugin_id, run.clone())); + + // Clone for threaded contexts + let plugin_executor = self.plugin_executor.clone(); + let senders = self.senders.clone(); + let zellij_cwd = cwd.unwrap_or_else(|| self.zellij_cwd.clone()); + + // Check if we need to download (async I/O required) + let needs_download = matches!(plugin.location, RunPluginLocation::Remote(_)); + + let mut loading_context = LoadingContext::new( + &self, + Some(zellij_cwd.clone()), + plugin.clone(), // TODO: rename to plugin_config + plugin_id, + client_id, + tab_index, + size, + ); - let load_plugin_task = task::spawn({ - let plugin_dir = self.plugin_dir.clone(); - let plugin_cache = self.plugin_cache.clone(); - let senders = self.senders.clone(); - let engine = self.engine.clone(); - let plugin_map = self.plugin_map.clone(); - let connected_clients = self.connected_clients.clone(); - let path_to_default_shell = self.path_to_default_shell.clone(); - let zellij_cwd = cwd.unwrap_or_else(|| self.zellij_cwd.clone()); - let capabilities = self.capabilities.clone(); - let client_attributes = self.client_attributes.clone(); - let default_shell = self.default_shell.clone(); - let default_layout = self.default_layout.clone(); - let layout_dir = self.layout_dir.clone(); + if needs_download { let downloader = self.downloader.clone(); - let default_mode = self - .base_modes - .get(&client_id) - .copied() - .unwrap_or(self.default_mode); - let keybinds = self - .keybinds - .get(&client_id) - .cloned() - .unwrap_or_else(|| self.default_keybinds.clone()); - async move { + get_tokio_runtime().spawn(async move { let _ = senders.send_to_background_jobs( BackgroundJob::AnimatePluginLoading(plugin_id), ); @@ -246,64 +350,119 @@ impl WasmBridge { .map(ToString::to_string) .collect(); - // if the url is already in cache, we'll use that version, otherwise - // we'll download it, place it in cache and then use it match downloader.download(url, Some(&file_name)).await { - Ok(_) => plugin.path = ZELLIJ_CACHE_DIR.join(&file_name), + Ok(_) => loading_context + .update_plugin_path(ZELLIJ_CACHE_DIR.join(&file_name)), + Err(e) => { + handle_plugin_loading_failure( + &senders, + plugin_id, + &mut loading_indication, + e, + Some(client_id), + ); + return; + }, + } + } + + plugin_executor.execute_plugin_load( + plugin_id, + move |senders: ThreadSenders, + plugin_map: Arc<Mutex<PluginMap>>, + connected_clients: Arc<Mutex<Vec<ClientId>>>, + default_layout: Box<Layout>, + plugin_cache: PluginCache, + engine| { + let mut plugin_map = plugin_map.lock().unwrap(); + match PluginLoader::new( + skip_cache, + loading_context, + senders.clone(), + engine.clone(), + default_layout.clone(), + plugin_cache.clone(), + &mut plugin_map, + connected_clients.clone(), + ) + .start_plugin() + { + Ok(_) => { + let plugin_list = plugin_map.list_plugins(); + handle_plugin_successful_loading( + &senders, + plugin_id, + plugin_list, + ); + }, + Err(e) => handle_plugin_loading_failure( + &senders, + plugin_id, + &mut loading_indication, + e, + Some(client_id), + ), + } + + let _ = + senders.send_to_plugin(PluginInstruction::ApplyCachedEvents { + plugin_ids: vec![plugin_id], + done_receiving_permissions: false, + }); + }, + ); + }); + } else { + let _ = senders + .send_to_background_jobs(BackgroundJob::AnimatePluginLoading(plugin_id)); + let mut loading_indication = LoadingIndication::new(plugin_name.clone()); + + self.plugin_executor.execute_plugin_load( + plugin_id, + move |senders, + plugin_map, + connected_clients, + default_layout, + plugin_cache: PluginCache, + engine: Engine| { + let mut plugin_map = plugin_map.lock().unwrap(); + match PluginLoader::new( + skip_cache, + loading_context, + senders.clone(), + engine.clone(), + default_layout.clone(), + plugin_cache.clone(), + &mut plugin_map, + connected_clients.clone(), + ) + .start_plugin() + { + Ok(_) => { + let plugin_list = plugin_map.list_plugins(); + handle_plugin_successful_loading( + &senders, + plugin_id, + plugin_list, + ); + }, Err(e) => handle_plugin_loading_failure( &senders, plugin_id, &mut loading_indication, e, - cli_client_id, + Some(client_id), ), } - } - match PluginLoader::start_plugin( - plugin_id, - client_id, - &plugin, - tab_index, - plugin_dir, - plugin_cache, - senders.clone(), - engine, - plugin_map.clone(), - size, - connected_clients.clone(), - &mut loading_indication, - path_to_default_shell, - zellij_cwd.clone(), - capabilities, - client_attributes, - default_shell, - default_layout, - skip_cache, - layout_dir, - default_mode, - keybinds, - ) { - Ok(_) => { - let plugin_list = plugin_map.lock().unwrap().list_plugins(); - handle_plugin_successful_loading(&senders, plugin_id, plugin_list); - }, - Err(e) => handle_plugin_loading_failure( - &senders, - plugin_id, - &mut loading_indication, - e, - cli_client_id, - ), - } - let _ = senders.send_to_plugin(PluginInstruction::ApplyCachedEvents { - plugin_ids: vec![plugin_id], - done_receiving_permissions: false, - }); - } - }); - self.loading_plugins - .insert((plugin_id, run.clone()), load_plugin_task); + let _ = senders.send_to_plugin(PluginInstruction::ApplyCachedEvents { + plugin_ids: vec![plugin_id], + done_receiving_permissions: false, + }); + }, + ); + } + self.next_plugin_id += 1; }, None => { @@ -322,55 +481,80 @@ impl WasmBridge { } pub fn unload_plugin(&mut self, pid: PluginId) -> Result<()> { info!("Bye from plugin {}", &pid); - let mut plugin_map = self.plugin_map.lock().unwrap(); - for ((plugin_id, client_id), (running_plugin, subscriptions, workers)) in - plugin_map.remove_plugins(pid) + + // Remove from plugin_map on main thread + let plugins_to_cleanup: Vec<_> = { + let mut plugin_map = self.plugin_map.lock().unwrap(); + plugin_map.remove_plugins(pid).into_iter().collect() + }; + + // Schedule cleanup on each plugin's pinned thread + for ((plugin_id, client_id), (running_plugin, subscriptions, workers)) in plugins_to_cleanup { + // Clear key intercepts if needed (on main thread is OK) + if running_plugin.lock().unwrap().intercepting_key_presses() { + let _ = self + .senders + .send_to_screen(ScreenInstruction::ClearKeyPressesIntercepts(client_id)); + } + + // Send worker exit messages for (_worker_name, worker_sender) in workers { drop(worker_sender.send(MessageToWorker::Exit)); } - { - // if the plugin was intercepting key presses and for some reason did not clear - // this state, we make sure to do it ourselves so that the user will not get stuck - if running_plugin.lock().unwrap().intercepting_key_presses() { - let _ = self - .senders - .send_to_screen(ScreenInstruction::ClearKeyPressesIntercepts(client_id)); - } - } - let subscriptions = subscriptions.lock().unwrap(); - if subscriptions.contains(&EventType::BeforeClose) { - let mut running_plugin = running_plugin.lock().unwrap(); - match apply_before_close_event_to_plugin( - pid, - client_id, - &mut running_plugin, - self.senders.clone(), - ) { - Ok(()) => {}, - Err(e) => { - log::error!("{:?}", e); - // https://stackoverflow.com/questions/66450942/in-rust-is-there-a-way-to-make-literal-newlines-in-r-using-windows-c - let stringified_error = format!("{:?}", e).replace("\n", "\n\r"); + self.plugin_executor.execute_plugin_unload( + plugin_id, + move |senders, + _plugin_map, + _connected_clients, + _default_layout, + _plugin_cache, + _engine| { + let subscriptions_guard = subscriptions.lock().unwrap(); + let needs_before_close = subscriptions_guard.contains(&EventType::BeforeClose); + drop(subscriptions_guard); // Release lock before calling plugin - handle_plugin_crash(plugin_id, stringified_error, self.senders.clone()); - }, - } - let cache_dir = running_plugin.store.data().plugin_own_data_dir.clone(); - if let Err(e) = std::fs::remove_dir_all(cache_dir) { - log::error!("Failed to remove cache dir for plugin: {:?}", e); - } - } else { - // this is duplicated because of locking/unlocking order between running_plugin and - // subscriptions - let running_plugin = running_plugin.lock().unwrap(); - let cache_dir = running_plugin.store.data().plugin_own_data_dir.clone(); - if let Err(e) = std::fs::remove_dir_all(cache_dir) { - log::error!("Failed to remove cache dir for plugin: {:?}", e); - } - } + if needs_before_close { + let mut rp = running_plugin.lock().unwrap(); + match apply_before_close_event_to_plugin( + plugin_id, + client_id, + &mut rp, + senders.clone(), + ) { + Ok(()) => {}, + Err(e) => { + log::error!("{:?}", e); + let stringified_error = format!("{:?}", e).replace("\n", "\n\r"); + handle_plugin_crash(plugin_id, stringified_error, senders.clone()); + }, + } + let cache_dir = rp.store.data().plugin_own_data_dir.clone(); + drop(rp); // Release lock before filesystem operation + if let Err(e) = std::fs::remove_dir_all(&cache_dir) { + log::error!("Failed to remove cache dir for plugin: {:?}", e); + } + } else { + let cache_dir = running_plugin + .lock() + .unwrap() + .store + .data() + .plugin_own_data_dir + .clone(); + if let Err(e) = std::fs::remove_dir_all(&cache_dir) { + log::error!("Failed to remove cache dir for plugin: {:?}", e); + } + } + + drop(running_plugin); + drop(subscriptions); + }, + ); } + + // Main thread cleanup self.cached_plugin_map.clear(); let mut pipes_to_unblock = self.pending_pipes.unload_plugin(&pid); for pipe_name in pipes_to_unblock.drain(..) { @@ -379,10 +563,11 @@ impl WasmBridge { .send_to_server(ServerInstruction::UnblockCliPipeInput(pipe_name)) .context("failed to unblock input pipe"); } - let plugin_list = plugin_map.list_plugins(); + let plugin_list = self.plugin_map.lock().unwrap().list_plugins(); let _ = self .senders .send_to_background_jobs(BackgroundJob::ReportPluginList(plugin_list)); + Ok(()) } pub fn reload_plugin_with_id(&mut self, plugin_id: u32) -> Result<()> { @@ -399,58 +584,78 @@ impl WasmBridge { let mut loading_indication = LoadingIndication::new(run_plugin.location.to_string()); self.start_plugin_loading_indication(&[plugin_id], &loading_indication); - let load_plugin_task = task::spawn({ - let plugin_dir = self.plugin_dir.clone(); - let plugin_cache = self.plugin_cache.clone(); - let senders = self.senders.clone(); - let engine = self.engine.clone(); - let plugin_map = self.plugin_map.clone(); - let connected_clients = self.connected_clients.clone(); - let path_to_default_shell = self.path_to_default_shell.clone(); - let capabilities = self.capabilities.clone(); - let client_attributes = self.client_attributes.clone(); - let default_shell = self.default_shell.clone(); - let default_layout = self.default_layout.clone(); - let layout_dir = self.layout_dir.clone(); - async move { - match PluginLoader::reload_plugin( - plugin_id, - plugin_dir.clone(), - plugin_cache.clone(), + self.loading_plugins.insert((plugin_id, run_plugin.clone())); + + let plugin_executor = self.plugin_executor.clone(); + + let Some(first_client_id) = self.get_first_client_id() else { + log::error!("No connected clients, cannot reload plugin."); + return Ok(()); + }; + let Some(plugin_config) = self.plugin_config_of_plugin_id(plugin_id) else { + log::error!("Could not find running plugin with id: {}", plugin_id); + return Ok(()); + }; + let tab_index = self.tab_index_of_plugin_id(plugin_id); + let Some(size) = self.size_of_plugin_id(plugin_id) else { + log::error!( + "Could not find size of running plugin with id: {}", + plugin_id + ); + return Ok(()); + }; + let size = Size { + rows: size.0, + cols: size.1, + }; + + let cwd = self.cwd_of_plugin_id(plugin_id); + + let loading_context = LoadingContext::new( + &self, + cwd, + plugin_config, + plugin_id, + first_client_id, + tab_index, + size, + ); + + plugin_executor.execute_for_plugin( + plugin_id, + move |senders, plugin_map, connected_clients, default_layout, plugin_cache, engine| { + let skip_cache = true; // we want to explicitly reload the plugin + let mut plugin_map = plugin_map.lock().unwrap(); + match PluginLoader::new( + skip_cache, + loading_context, senders.clone(), engine.clone(), - plugin_map.clone(), - connected_clients.clone(), - &mut loading_indication, - path_to_default_shell.clone(), - capabilities.clone(), - client_attributes.clone(), - default_shell.clone(), default_layout.clone(), - layout_dir.clone(), - ) { + plugin_cache.clone(), + &mut plugin_map, + connected_clients.clone(), + ) + .start_plugin() + { Ok(_) => { - let plugin_list = plugin_map.lock().unwrap().list_plugins(); + let plugin_list = plugin_map.list_plugins(); handle_plugin_successful_loading(&senders, plugin_id, plugin_list); }, - Err(e) => { - handle_plugin_loading_failure( - &senders, - plugin_id, - &mut loading_indication, - &e, - None, - ); - }, + Err(e) => handle_plugin_loading_failure( + &senders, + plugin_id, + &mut loading_indication, + e, + Some(first_client_id), + ), } let _ = senders.send_to_plugin(PluginInstruction::ApplyCachedEvents { plugin_ids: vec![plugin_id], done_receiving_permissions: false, }); - } - }); - self.loading_plugins - .insert((plugin_id, run_plugin.clone()), load_plugin_task); + }, + ); Ok(()) } pub fn reload_plugin(&mut self, run_plugin: &RunPlugin) -> Result<()> { @@ -462,146 +667,110 @@ impl WasmBridge { let plugin_ids = self .all_plugin_ids_for_plugin_location(&run_plugin.location, &run_plugin.configuration)?; for plugin_id in &plugin_ids { - let (rows, columns) = self.size_of_plugin_id(*plugin_id).unwrap_or((0, 0)); - self.cached_events_for_pending_plugins - .insert(*plugin_id, vec![]); - self.cached_resizes_for_pending_plugins - .insert(*plugin_id, (rows, columns)); + self.reload_plugin_with_id(*plugin_id)?; } - - let first_plugin_id = *plugin_ids.get(0).unwrap(); // this is safe becaise the above - // methods always returns at least 1 id - let mut loading_indication = LoadingIndication::new(run_plugin.location.to_string()); - self.start_plugin_loading_indication(&plugin_ids, &loading_indication); - let load_plugin_task = task::spawn({ - let plugin_dir = self.plugin_dir.clone(); - let plugin_cache = self.plugin_cache.clone(); - let senders = self.senders.clone(); - let engine = self.engine.clone(); - let plugin_map = self.plugin_map.clone(); - let connected_clients = self.connected_clients.clone(); - let path_to_default_shell = self.path_to_default_shell.clone(); - let zellij_cwd = self.zellij_cwd.clone(); - let capabilities = self.capabilities.clone(); - let client_attributes = self.client_attributes.clone(); - let default_shell = self.default_shell.clone(); - let default_layout = self.default_layout.clone(); - let layout_dir = self.layout_dir.clone(); - async move { - match PluginLoader::reload_plugin( - first_plugin_id, - plugin_dir.clone(), - plugin_cache.clone(), - senders.clone(), - engine.clone(), - plugin_map.clone(), - connected_clients.clone(), - &mut loading_indication, - path_to_default_shell.clone(), - capabilities.clone(), - client_attributes.clone(), - default_shell.clone(), - default_layout.clone(), - layout_dir.clone(), - ) { - Ok(_) => { - let plugin_list = plugin_map.lock().unwrap().list_plugins(); - handle_plugin_successful_loading(&senders, first_plugin_id, plugin_list); - for plugin_id in &plugin_ids { - if plugin_id == &first_plugin_id { - // no need to reload the plugin we just reloaded - continue; - } - let mut loading_indication = LoadingIndication::new("".into()); - match PluginLoader::reload_plugin_from_memory( - *plugin_id, - plugin_dir.clone(), - plugin_cache.clone(), - senders.clone(), - engine.clone(), - plugin_map.clone(), - connected_clients.clone(), - &mut loading_indication, - path_to_default_shell.clone(), - zellij_cwd.clone(), - capabilities.clone(), - client_attributes.clone(), - default_shell.clone(), - default_layout.clone(), - layout_dir.clone(), - ) { - Ok(_) => { - let plugin_list = plugin_map.lock().unwrap().list_plugins(); - handle_plugin_successful_loading( - &senders, - *plugin_id, - plugin_list, - ); - }, - Err(e) => handle_plugin_loading_failure( - &senders, - *plugin_id, - &mut loading_indication, - e, - None, - ), - } - } - }, - Err(e) => { - for plugin_id in &plugin_ids { - handle_plugin_loading_failure( - &senders, - *plugin_id, - &mut loading_indication, - &e, - None, - ); - } - }, - } - let _ = senders.send_to_plugin(PluginInstruction::ApplyCachedEvents { - plugin_ids, - done_receiving_permissions: false, - }); - } - }); - self.loading_plugins - .insert((first_plugin_id, run_plugin.clone()), load_plugin_task); Ok(()) } pub fn add_client(&mut self, client_id: ClientId) -> Result<()> { - let mut loading_indication = LoadingIndication::new("".into()); - match PluginLoader::add_client( - client_id, - self.plugin_dir.clone(), - self.plugin_cache.clone(), - self.senders.clone(), - self.engine.clone(), - self.plugin_map.clone(), - self.connected_clients.clone(), - &mut loading_indication, - self.path_to_default_shell.clone(), - self.zellij_cwd.clone(), - self.capabilities.clone(), - self.client_attributes.clone(), - self.default_shell.clone(), - self.default_layout.clone(), - self.layout_dir.clone(), - self.default_mode, - self.keybinds - .get(&client_id) - .cloned() - .unwrap_or_else(|| self.default_keybinds.clone()), - ) { - Ok(_) => { - let _ = self - .senders - .send_to_screen(ScreenInstruction::RequestStateUpdateForPlugins); - Ok(()) - }, - Err(e) => Err(e), + if self.client_is_connected(&client_id) { + return Ok(()); } + + let mut new_plugins = HashSet::new(); + for plugin_id in self.plugin_map.lock().unwrap().plugin_ids() { + new_plugins.insert(plugin_id); + } + for plugin_id in new_plugins { + let Some(run_plugin) = self.run_plugin_of_plugin_id(plugin_id).map(|r| r.clone()) + else { + log::error!("Failed to find plugin with id: {}", plugin_id); + return Ok(()); + }; + + let (rows, columns) = self.size_of_plugin_id(plugin_id).unwrap_or((0, 0)); + self.cached_events_for_pending_plugins + .insert(plugin_id, vec![]); + self.cached_resizes_for_pending_plugins + .insert(plugin_id, (rows, columns)); + + let loading_indication = LoadingIndication::new(run_plugin.location.to_string()); + self.start_plugin_loading_indication(&[plugin_id], &loading_indication); + self.loading_plugins.insert((plugin_id, run_plugin.clone())); + + let plugin_executor = self.plugin_executor.clone(); + + let Some(plugin_config) = self.plugin_config_of_plugin_id(plugin_id) else { + log::error!("Could not find running plugin with id: {}", plugin_id); + return Ok(()); + }; + let tab_index = self.tab_index_of_plugin_id(plugin_id); + let Some(size) = self.size_of_plugin_id(plugin_id) else { + log::error!( + "Could not find size of running plugin with id: {}", + plugin_id + ); + return Ok(()); + }; + let size = Size { + rows: size.0, + cols: size.1, + }; + + let cwd = self.cwd_of_plugin_id(plugin_id); + + let loading_context = LoadingContext::new( + &self, + cwd, + plugin_config, + plugin_id, + client_id, + tab_index, + size, + ); + + plugin_executor.execute_for_plugin( + plugin_id, + move |senders, + plugin_map, + connected_clients, + default_layout, + plugin_cache, + engine| { + let skip_cache = false; + let mut plugin_map = plugin_map.lock().unwrap(); + match PluginLoader::new( + skip_cache, + loading_context, + senders.clone(), + engine.clone(), + default_layout.clone(), + plugin_cache.clone(), + &mut plugin_map, + connected_clients.clone(), + ) + .without_connected_clients() + .start_plugin() + { + Ok(_) => { + let _ = senders + .send_to_screen(ScreenInstruction::RequestStateUpdateForPlugins); + let _ = senders.send_to_background_jobs( + BackgroundJob::StopPluginLoadingAnimation(plugin_id), + ); + let _ = senders.send_to_plugin(PluginInstruction::ApplyCachedEvents { + plugin_ids: vec![plugin_id], + done_receiving_permissions: false, + }); + }, + Err(e) => { + log::error!("Failed to load plugin for new client: {}", e); + }, + } + }, + ) + } + self.connected_clients.lock().unwrap().push(client_id); + Ok(()) } pub fn resize_plugin( &mut self, @@ -631,11 +800,17 @@ impl WasmBridge { .lock() .unwrap() .next_event_id(AtomicEvent::Resize); - task::spawn({ - let senders = self.senders.clone(); + // Execute directly on pinned thread (no async I/O needed for resize/render) + self.plugin_executor.execute_for_plugin(plugin_id, { + // let senders = self.senders.clone(); let running_plugin = running_plugin.clone(); let _s = shutdown_sender.clone(); - async move { + move |senders, + _plugin_map, + _connected_clients, + _default_layout, + _plugin_cache, + _engine| { let mut running_plugin = running_plugin.lock().unwrap(); let _s = _s; // guard to allow the task to complete before cleanup/shutdown if running_plugin.apply_event_id(AtomicEvent::Resize, event_id) { @@ -660,7 +835,11 @@ impl WasmBridge { (new_rows as i32, new_columns as i32), ) }) - .and_then(|_| wasi_read_string(running_plugin.store.data())) + .map_err(|e| anyhow!(e)) + .and_then(|_| { + wasi_read_string(running_plugin.store.data()) + .map_err(|e| anyhow!(e)) + }) .with_context(err_context); match rendered_bytes { Ok(rendered_bytes) => { @@ -714,29 +893,38 @@ impl WasmBridge { .contains_key(&plugin_id) }) .collect(); - task::spawn({ - let mut updates = updates.clone(); - let senders = self.senders.clone(); - let s = shutdown_sender.clone(); - async move { - let _s = s; - for (pid, cid, event) in updates.drain(..) { - for (plugin_id, client_id, running_plugin, subscriptions) in &plugins_to_update + + // Execute each plugin update on its respective pinned thread + let plugin_executor = self.plugin_executor.clone(); + // let senders = self.senders.clone(); + for (pid, cid, event) in updates.clone().into_iter() { + for (plugin_id, client_id, running_plugin, subscriptions) in &plugins_to_update { + let subs = subscriptions.lock().unwrap().clone(); + // FIXME: This is very janky... Maybe I should write my own macro for Event -> EventType? + if let Ok(event_type) = EventType::from_str(&event.to_string()) { + if (subs.contains(&event_type) + || event_type == EventType::PermissionRequestResult) + && Self::message_is_directed_at_plugin(pid, cid, plugin_id, client_id) { - let subs = subscriptions.lock().unwrap().clone(); - // FIXME: This is very janky... Maybe I should write my own macro for Event -> EventType? - if let Ok(event_type) = EventType::from_str(&event.to_string()) { - if (subs.contains(&event_type) - || event_type == EventType::PermissionRequestResult) - && Self::message_is_directed_at_plugin( - pid, cid, plugin_id, client_id, - ) - { + // Execute directly on pinned thread (no async I/O needed for event processing) + plugin_executor.execute_for_plugin(*plugin_id, { + let plugin_id = *plugin_id; + let client_id = *client_id; + let running_plugin = running_plugin.clone(); + let event = event.clone(); + let _s = shutdown_sender.clone(); + move |senders, + _plugin_map, + _connected_clients, + _default_layout, + _plugin_cache, + _engine| { + let _s = _s; // guard to allow the task to complete before cleanup/shutdown let mut running_plugin = running_plugin.lock().unwrap(); let mut plugin_render_assets = vec![]; match apply_event_to_plugin( - *plugin_id, - *client_id, + plugin_id, + client_id, &mut running_plugin, &event, &mut plugin_render_assets, @@ -755,18 +943,19 @@ impl WasmBridge { format!("{:?}", e).replace("\n", "\n\r"); handle_plugin_crash( - *plugin_id, + plugin_id, stringified_error, senders.clone(), ); }, } } - } + }); } } } - }); + } + // loop once more to update the cached events for the pending plugins (probably currently // being loaded, we'll send them these events when they load) for (pid, _cid, event) in updates.drain(..) { @@ -818,85 +1007,95 @@ impl WasmBridge { .iter() .cloned() .collect(); - task::spawn({ - let senders = self.senders.clone(); - async move { - match new_host_dir.try_exists() { - Ok(false) => { - log::error!( - "Failed to change folder to {},: folder does not exist", - new_host_dir.display() - ); - let _ = senders.send_to_plugin(PluginInstruction::Update(vec![( - Some(plugin_id_to_update), - Some(client_id_to_update), - Event::FailedToChangeHostFolder(Some(format!( - "Folder {} does not exist", + + // Execute directly on pinned thread (no async I/O needed for directory check/change) + self.plugin_executor + .execute_for_plugin(plugin_id_to_update, { + move |senders, + _plugin_map, + _connected_clients, + _default_layout, + _plugin_cache, + _engine| { + match new_host_dir.try_exists() { + Ok(false) => { + log::error!( + "Failed to change folder to {},: folder does not exist", new_host_dir.display() - ))), - )])); - return; - }, - Err(e) => { - log::error!( - "Failed to change folder to {},: {}", - new_host_dir.display(), - e - ); - let _ = senders.send_to_plugin(PluginInstruction::Update(vec![( - Some(plugin_id_to_update), - Some(client_id_to_update), - Event::FailedToChangeHostFolder(Some(e.to_string())), - )])); - return; - }, - _ => {}, - } - for (plugin_id, client_id, running_plugin, _subscriptions) in &plugins_to_change { - if plugin_id == &plugin_id_to_update && client_id == &client_id_to_update { - let mut running_plugin = running_plugin.lock().unwrap(); - let plugin_env = running_plugin.store.data_mut(); - let stdin_pipe = plugin_env.stdin_pipe.clone(); - let stdout_pipe = plugin_env.stdout_pipe.clone(); - let wasi_ctx = PluginLoader::create_wasi_ctx( - &new_host_dir, - &plugin_env.plugin_own_data_dir, - &plugin_env.plugin_own_cache_dir, - &ZELLIJ_TMP_DIR, - &plugin_env.plugin.location.to_string(), - plugin_env.plugin_id, - stdin_pipe.clone(), - stdout_pipe.clone(), - ); - match wasi_ctx { - Ok(wasi_ctx) => { - drop(std::mem::replace(&mut plugin_env.wasi_ctx, wasi_ctx)); - plugin_env.plugin_cwd = new_host_dir.clone(); + ); + let _ = senders.send_to_plugin(PluginInstruction::Update(vec![( + Some(plugin_id_to_update), + Some(client_id_to_update), + Event::FailedToChangeHostFolder(Some(format!( + "Folder {} does not exist", + new_host_dir.display() + ))), + )])); + return; + }, + Err(e) => { + log::error!( + "Failed to change folder to {},: {}", + new_host_dir.display(), + e + ); + let _ = senders.send_to_plugin(PluginInstruction::Update(vec![( + Some(plugin_id_to_update), + Some(client_id_to_update), + Event::FailedToChangeHostFolder(Some(e.to_string())), + )])); + return; + }, + _ => {}, + } + for (plugin_id, client_id, running_plugin, _subscriptions) in &plugins_to_change + { + if plugin_id == &plugin_id_to_update && client_id == &client_id_to_update { + let mut running_plugin = running_plugin.lock().unwrap(); + let plugin_env = running_plugin.store.data_mut(); + let stdin_pipe = plugin_env.stdin_pipe.clone(); + let stdout_pipe = plugin_env.stdout_pipe.clone(); + let wasi_ctx = PluginLoader::create_wasi_ctx( + &new_host_dir, + &plugin_env.plugin_own_data_dir, + &plugin_env.plugin_own_cache_dir, + &ZELLIJ_TMP_DIR, + &plugin_env.plugin.location.to_string(), + plugin_env.plugin_id, + stdin_pipe.clone(), + stdout_pipe.clone(), + ); + match wasi_ctx { + Ok(wasi_ctx) => { + drop(std::mem::replace(&mut plugin_env.wasi_ctx, wasi_ctx)); + plugin_env.plugin_cwd = new_host_dir.clone(); - let _ = senders.send_to_plugin(PluginInstruction::Update(vec![( - Some(*plugin_id), - Some(*client_id), - Event::HostFolderChanged(new_host_dir.clone()), - )])); - }, - Err(e) => { - let _ = senders.send_to_plugin(PluginInstruction::Update(vec![( - Some(*plugin_id), - Some(*client_id), - Event::FailedToChangeHostFolder(Some(e.to_string())), - )])); - log::error!("Failed to create wasi ctx: {}", e); - }, + let _ = + senders.send_to_plugin(PluginInstruction::Update(vec![( + Some(*plugin_id), + Some(*client_id), + Event::HostFolderChanged(new_host_dir.clone()), + )])); + }, + Err(e) => { + let _ = + senders.send_to_plugin(PluginInstruction::Update(vec![( + Some(*plugin_id), + Some(*client_id), + Event::FailedToChangeHostFolder(Some(e.to_string())), + )])); + log::error!("Failed to create wasi ctx: {}", e); + }, + } } } } - } - }); + }); Ok(()) } pub fn pipe_messages( &mut self, - mut messages: Vec<(Option<PluginId>, Option<ClientId>, PipeMessage)>, + messages: Vec<(Option<PluginId>, Option<ClientId>, PipeMessage)>, shutdown_sender: Sender<()>, ) -> Result<()> { let plugins_to_update: Vec<( @@ -917,7 +1116,10 @@ impl WasmBridge { .contains_key(&plugin_id) }) .collect(); - for (message_pid, message_cid, pipe_message) in messages.drain(..) { + + // Execute each pipe message on its respective plugin's pinned thread + let plugin_executor = self.plugin_executor.clone(); + for (message_pid, message_cid, pipe_message) in messages.clone().into_iter() { for (plugin_id, client_id, running_plugin, _subscriptions) in &plugins_to_update { if Self::message_is_directed_at_plugin( message_pid, @@ -929,14 +1131,20 @@ impl WasmBridge { self.pending_pipes .mark_being_processed(pipe_id, plugin_id, client_id); } - task::spawn({ - let senders = self.senders.clone(); + // Execute directly on pinned thread (no async I/O needed for pipe message processing) + plugin_executor.execute_for_plugin(*plugin_id, { + // let senders = senders.clone(); let running_plugin = running_plugin.clone(); let pipe_message = pipe_message.clone(); let plugin_id = *plugin_id; let client_id = *client_id; let _s = shutdown_sender.clone(); - async move { + move |senders, + _plugin_map, + _connected_clients, + _default_layout, + _plugin_cache, + _engine| { let mut running_plugin = running_plugin.lock().unwrap(); let mut plugin_render_assets = vec![]; let _s = _s; // guard to allow the task to complete before cleanup/shutdown @@ -1021,7 +1229,7 @@ impl WasmBridge { applied_plugin_paths.insert(run_plugin.clone()); } self.loading_plugins - .retain(|(p_id, _run_plugin), _| p_id != &plugin_id); + .retain(|(p_id, _run_plugin)| p_id != &plugin_id); self.clear_plugin_map_cache(); } for run_plugin in applied_plugin_paths.drain() { @@ -1103,9 +1311,8 @@ impl WasmBridge { } pub fn cleanup(&mut self) { - for (_plugin_id, loading_plugin_task) in self.loading_plugins.drain() { - drop(loading_plugin_task.cancel()); - } + self.loading_plugins.clear(); + let plugin_ids = self.plugin_map.lock().unwrap().plugin_ids(); for plugin_id in &plugin_ids { drop(self.unload_plugin(*plugin_id)); @@ -1117,8 +1324,8 @@ impl WasmBridge { pub fn run_plugin_of_loading_plugin_id(&self, plugin_id: PluginId) -> Option<&RunPlugin> { self.loading_plugins .iter() - .find(|((p_id, _run_plugin), _)| p_id == &plugin_id) - .map(|((_p_id, run_plugin), _)| run_plugin) + .find(|(p_id, _run_plugin)| p_id == &plugin_id) + .map(|(_p_id, run_plugin)| run_plugin) } pub fn run_plugin_of_plugin_id(&self, plugin_id: PluginId) -> Option<RunPlugin> { self.plugin_map @@ -1134,16 +1341,16 @@ impl WasmBridge { default_mode: Option<InputMode>, default_shell: Option<TerminalAction>, ) -> Result<()> { - let plugins_to_reconfigure: Vec<Arc<Mutex<RunningPlugin>>> = self + let plugins_to_reconfigure: Vec<(PluginId, Arc<Mutex<RunningPlugin>>)> = self .plugin_map .lock() .unwrap() .running_plugins() .iter() .cloned() - .filter_map(|(_plugin_id, c_id, running_plugin)| { + .filter_map(|(plugin_id, c_id, running_plugin)| { if c_id == client_id { - Some(running_plugin.clone()) + Some((plugin_id, running_plugin.clone())) } else { None } @@ -1156,12 +1363,17 @@ impl WasmBridge { self.keybinds.insert(client_id, keybinds.clone()); } self.default_shell = default_shell.clone(); - for running_plugin in plugins_to_reconfigure { - task::spawn({ + for (plugin_id, running_plugin) in plugins_to_reconfigure { + self.plugin_executor.execute_for_plugin(plugin_id, { let running_plugin = running_plugin.clone(); let keybinds = keybinds.clone(); let default_shell = default_shell.clone(); - async move { + move |_senders, + _plugin_map, + _connected_clients, + _default_layout, + _plugin_cache, + _engine| { let mut running_plugin = running_plugin.lock().unwrap(); if let Some(keybinds) = keybinds { running_plugin.update_keybinds(keybinds); @@ -1198,14 +1410,18 @@ impl WasmBridge { .unwrap() .get_running_plugin_and_subscriptions(plugin_id, *client_id) { - task::spawn({ - let senders = self.senders.clone(); + let subs = subscriptions.lock().unwrap().clone(); + self.plugin_executor.execute_for_plugin(plugin_id, { let running_plugin = running_plugin.clone(); let client_id = *client_id; let _s = shutdown_sender.clone(); let events_or_pipe_messages = events_or_pipe_messages.clone(); - async move { - let subs = subscriptions.lock().unwrap().clone(); + move |senders, + _plugin_map, + _connected_clients, + _default_layout, + _plugin_cache, + _engine| { let _s = _s; // guard to allow the task to complete before cleanup/shutdown for event_or_pipe_message in events_or_pipe_messages { match event_or_pipe_message { @@ -1311,7 +1527,7 @@ impl WasmBridge { fn plugin_is_currently_being_loaded(&self, plugin_location: &RunPluginLocation) -> bool { self.loading_plugins .iter() - .find(|((_plugin_id, run_plugin), _)| &run_plugin.location == plugin_location) + .find(|(_plugin_id, run_plugin)| &run_plugin.location == plugin_location) .is_some() } fn plugin_id_of_loading_plugin( @@ -1321,7 +1537,7 @@ impl WasmBridge { ) -> Option<PluginId> { self.loading_plugins .iter() - .find_map(|((plugin_id, run_plugin), _)| { + .find_map(|(plugin_id, run_plugin)| { if &run_plugin.location == plugin_location && &run_plugin.configuration == plugin_configuration { @@ -1375,6 +1591,36 @@ impl WasmBridge { (r.rows, r.columns) }) } + fn cwd_of_plugin_id(&self, plugin_id: PluginId) -> Option<PathBuf> { + self.plugin_map + .lock() + .unwrap() + .get_running_plugin(plugin_id, None) + .map(|r| { + let r = r.lock().unwrap(); + r.store.data().plugin_cwd.clone() + }) + } + fn plugin_config_of_plugin_id(&self, plugin_id: PluginId) -> Option<PluginConfig> { + self.plugin_map + .lock() + .unwrap() + .get_running_plugin(plugin_id, None) + .map(|r| { + let r = r.lock().unwrap(); + r.store.data().plugin.clone() + }) + } + fn tab_index_of_plugin_id(&self, plugin_id: PluginId) -> Option<usize> { + self.plugin_map + .lock() + .unwrap() + .get_running_plugin(plugin_id, None) + .and_then(|r| { + let r = r.lock().unwrap(); + r.store.data().tab_index + }) + } fn start_plugin_loading_indication( &self, plugin_ids: &[PluginId], @@ -1517,7 +1763,6 @@ impl WasmBridge { size, cwd.clone(), skip_cache, - None, cli_client_id, ) { Ok((plugin_id, client_id)) => { @@ -1628,7 +1873,7 @@ fn handle_plugin_loading_failure( plugin_id: PluginId, loading_indication: &mut LoadingIndication, error: impl std::fmt::Debug, - cli_client_id: Option<ClientId>, + client_id: Option<ClientId>, ) { log::error!("{:?}", error); let _ = senders.send_to_background_jobs(BackgroundJob::StopPluginLoadingAnimation(plugin_id)); @@ -1637,10 +1882,10 @@ fn handle_plugin_loading_failure( plugin_id, loading_indication.clone(), )); - if let Some(cli_client_id) = cli_client_id { + if let Some(client_id) = client_id { let _ = senders.send_to_server(ServerInstruction::LogError( vec![format!("{:?}", error)], - cli_client_id, + client_id, )); } } @@ -1735,7 +1980,10 @@ pub fn apply_event_to_plugin( .and_then(|render| { render.call(&mut running_plugin.store, (rows as i32, columns as i32)) }) - .and_then(|_| wasi_read_string(running_plugin.store.data())) + .map_err(|e| anyhow!(e)) + .and_then(|_| { + wasi_read_string(running_plugin.store.data()).map_err(|e| anyhow!(e)) + }) .with_context(err_context)?; let pipes_to_block_or_unblock = pipes_to_block_or_unblock(running_plugin, None); let plugin_render_asset = PluginRenderAsset::new( diff --git a/zellij-server/src/plugins/zellij_exports.rs b/zellij-server/src/plugins/zellij_exports.rs index 7c287b0d..2b4ac7e1 100644 --- a/zellij-server/src/plugins/zellij_exports.rs +++ b/zellij-server/src/plugins/zellij_exports.rs @@ -1,11 +1,10 @@ use super::PluginInstruction; use crate::background_jobs::BackgroundJob; use crate::plugins::plugin_map::PluginEnv; -use crate::plugins::wasm_bridge::handle_plugin_crash; +use crate::plugins::wasm_bridge::{get_tokio_runtime, handle_plugin_crash}; use crate::pty::{ClientTabIndexOrPaneId, NewPanePlacement, PtyInstruction}; use crate::route::route_action; use crate::ServerInstruction; -use async_std::task; use interprocess::local_socket::LocalSocketStream; use log::warn; use serde::Serialize; @@ -18,7 +17,7 @@ use std::{ thread, time::{Duration, Instant}, }; -use wasmtime::{Caller, Linker}; +use wasmi::{Caller, Linker}; use zellij_utils::data::{ CommandType, ConnectToSession, FloatingPaneCoordinates, HttpVerb, KeyWithModifier, LayoutInfo, MessageToPlugin, OriginatingPlugin, PaneScrollbackResponse, PermissionStatus, PermissionType, @@ -1246,9 +1245,10 @@ fn set_timeout(env: &PluginEnv, secs: f64) { let update_target = Some(env.plugin_id); let client_id = env.client_id; let plugin_name = env.name(); - task::spawn(async move { + // Use tokio runtime for async I/O (timer operation) + get_tokio_runtime().spawn(async move { let start_time = Instant::now(); - task::sleep(Duration::from_secs_f64(secs)).await; + tokio::time::sleep(Duration::from_secs_f64(secs)).await; // FIXME: The way that elapsed time is being calculated here is not exact; it doesn't take into account the // time it takes an event to actually reach the plugin after it's sent to the `wasm` thread. let elapsed_time = Instant::now().duration_since(start_time).as_secs_f64(); |
