-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: restore only active WASM channels at startup #2562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
henrypark133
wants to merge
2
commits into
staging
Choose a base branch
from
fix-reconnect-loop
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+467
−47
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,7 @@ pub async fn setup_wasm_channels( | |
| extension_manager: Option<&Arc<ExtensionManager>>, | ||
| database: Option<&Arc<dyn Database>>, | ||
| registered_channel_names: &[String], | ||
| startup_active_channel_names: Option<&HashSet<String>>, | ||
| ownership_cache: Arc<crate::ownership::OwnershipCache>, | ||
| ) -> Option<WasmChannelSetup> { | ||
| let runtime = match WasmChannelRuntime::new(WasmChannelRuntimeConfig::default()) { | ||
|
|
@@ -93,18 +94,99 @@ pub async fn setup_wasm_channels( | |
| loader = loader.with_secrets_store(Arc::clone(secrets)); | ||
| } | ||
|
|
||
| let results = match loader | ||
| .load_from_dir(&config.channels.wasm_channels_dir) | ||
| .await | ||
| { | ||
| Ok(r) => r, | ||
| Err(e) => { | ||
| tracing::warn!("Failed to scan WASM channels directory: {}", e); | ||
| return None; | ||
| } | ||
| let discovered_channels = | ||
| match crate::channels::wasm::discover_channels(&config.channels.wasm_channels_dir).await { | ||
| Ok(channels) => channels, | ||
| Err(e) => { | ||
| tracing::warn!("Failed to scan WASM channels directory: {}", e); | ||
| return None; | ||
| } | ||
| }; | ||
|
|
||
| let startup_entries: Vec<(String, std::path::PathBuf, Option<std::path::PathBuf>)> = | ||
| discovered_channels | ||
| .into_iter() | ||
| .filter_map(|(name, discovered)| { | ||
| startup_active_channel_names | ||
| .is_none_or(|active_names| active_names.contains(&name)) | ||
| .then_some((name, discovered.wasm_path, discovered.capabilities_path)) | ||
| }) | ||
| .collect(); | ||
|
|
||
| let load_futures = startup_entries.iter().map(|(name, wasm_path, cap_path)| { | ||
| loader.load_from_files(name, wasm_path, cap_path.as_deref()) | ||
| }); | ||
| let load_results = futures::future::join_all(load_futures).await; | ||
|
|
||
| let mut loaded_channels = Vec::new(); | ||
| let startup_load_error_message = if startup_active_channel_names.is_some() { | ||
| "Failed to load persisted-active WASM channel at startup" | ||
| } else { | ||
| "Failed to load WASM channel at startup" | ||
| }; | ||
| for ((name, wasm_path, _), result) in startup_entries.into_iter().zip(load_results) { | ||
| match result { | ||
| Ok(loaded) => loaded_channels.push(loaded), | ||
| Err(err) => { | ||
| tracing::warn!( | ||
| channel = %name, | ||
| path = %wasm_path.display(), | ||
| error = %err, | ||
| "{startup_load_error_message}" | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let wasm_router = Arc::new(WasmChannelRouter::new()); | ||
| let registration_context = StartupChannelRegistrationContext { | ||
| registered_channel_names, | ||
| startup_active_channel_names, | ||
| config, | ||
| secrets_store, | ||
| settings_store: settings_store.as_ref(), | ||
| pairing_store: &pairing_store, | ||
| wasm_router: &wasm_router, | ||
| }; | ||
| let (channels, channel_names) = | ||
| register_startup_loaded_channels(loaded_channels, ®istration_context).await; | ||
|
|
||
| // Always create webhook routes (even with no channels loaded) so that | ||
| // channels hot-added at runtime can receive webhooks without a restart. | ||
| let webhook_routes = { | ||
| Some(create_wasm_channel_router( | ||
| Arc::clone(&wasm_router), | ||
| extension_manager.map(Arc::clone), | ||
| )) | ||
| }; | ||
|
|
||
| Some(WasmChannelSetup { | ||
| channels, | ||
| channel_names, | ||
| webhook_routes, | ||
| wasm_channel_runtime: runtime, | ||
| pairing_store, | ||
| wasm_channel_router: wasm_router, | ||
| }) | ||
| } | ||
|
|
||
| struct StartupChannelRegistrationContext<'a> { | ||
| registered_channel_names: &'a [String], | ||
| startup_active_channel_names: Option<&'a HashSet<String>>, | ||
| config: &'a Config, | ||
| secrets_store: &'a Option<Arc<dyn SecretsStore + Send + Sync>>, | ||
| settings_store: Option<&'a Arc<dyn crate::db::SettingsStore>>, | ||
| pairing_store: &'a Arc<PairingStore>, | ||
| wasm_router: &'a Arc<WasmChannelRouter>, | ||
| } | ||
|
|
||
| async fn register_startup_loaded_channels( | ||
| loaded_channels: Vec<LoadedChannel>, | ||
| context: &StartupChannelRegistrationContext<'_>, | ||
| ) -> ( | ||
| Vec<(String, Box<dyn crate::channels::Channel>)>, | ||
| Vec<String>, | ||
| ) { | ||
| let mut channels: Vec<(String, Box<dyn crate::channels::Channel>)> = Vec::new(); | ||
| let mut channel_names: Vec<String> = Vec::new(); | ||
|
|
||
|
|
@@ -116,7 +198,14 @@ pub async fn setup_wasm_channels( | |
| // - All native/built-in channel names (prevent impersonation) | ||
| // - Trusted approval channels from session::TRUSTED_APPROVAL_CHANNELS | ||
| // - The bootstrap sentinel (universal approval wildcard) | ||
| for loaded in results.loaded { | ||
| for loaded in loaded_channels { | ||
| if !context | ||
| .startup_active_channel_names | ||
| .is_none_or(|active_names| active_names.contains(loaded.name())) | ||
| { | ||
| continue; | ||
| } | ||
|
Comment on lines
+202
to
+207
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| let name_lower = loaded.name().to_ascii_lowercase(); | ||
| if is_reserved_wasm_channel_name(&name_lower) { | ||
| tracing::warn!( | ||
|
|
@@ -128,7 +217,8 @@ pub async fn setup_wasm_channels( | |
| // Also reject any name that collides with an already-registered | ||
| // channel to prevent a WASM module from shadowing a channel that | ||
| // was registered earlier in the startup sequence. | ||
| if registered_channel_names | ||
| if context | ||
| .registered_channel_names | ||
| .iter() | ||
| .any(|n| n.to_ascii_lowercase() == name_lower) | ||
| { | ||
|
|
@@ -141,38 +231,18 @@ pub async fn setup_wasm_channels( | |
|
|
||
| let (name, channel) = register_channel( | ||
| loaded, | ||
| config, | ||
| secrets_store, | ||
| settings_store.as_ref(), | ||
| &pairing_store, | ||
| &wasm_router, | ||
| context.config, | ||
| context.secrets_store, | ||
| context.settings_store, | ||
| context.pairing_store, | ||
| context.wasm_router, | ||
| ) | ||
| .await; | ||
| channel_names.push(name.clone()); | ||
| channels.push((name, channel)); | ||
| } | ||
|
|
||
| for (path, err) in &results.errors { | ||
| tracing::warn!("Failed to load WASM channel {}: {}", path.display(), err); | ||
| } | ||
|
|
||
| // Always create webhook routes (even with no channels loaded) so that | ||
| // channels hot-added at runtime can receive webhooks without a restart. | ||
| let webhook_routes = { | ||
| Some(create_wasm_channel_router( | ||
| Arc::clone(&wasm_router), | ||
| extension_manager.map(Arc::clone), | ||
| )) | ||
| }; | ||
|
|
||
| Some(WasmChannelSetup { | ||
| channels, | ||
| channel_names, | ||
| webhook_routes, | ||
| wasm_channel_runtime: runtime, | ||
| pairing_store, | ||
| wasm_channel_router: wasm_router, | ||
| }) | ||
| (channels, channel_names) | ||
| } | ||
|
|
||
| /// Process a single loaded WASM channel: retrieve secrets, inject config, | ||
|
|
@@ -593,7 +663,7 @@ async fn inject_channel_secrets_into_config( | |
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::collections::HashMap; | ||
| use std::collections::{HashMap, HashSet}; | ||
| use std::sync::Arc; | ||
|
|
||
| use super::reserved_wasm_channel_names; | ||
|
|
@@ -809,6 +879,89 @@ mod tests { | |
| ); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn register_startup_loaded_channels_only_restores_persisted_active_channels() { | ||
| let (config, _temp_dir) = test_config(); | ||
| let wasm_router = Arc::new(WasmChannelRouter::new()); | ||
| let pairing_store = Arc::new(PairingStore::new_noop()); | ||
| let startup_active = HashSet::from(["telegram".to_string()]); | ||
| let context = super::StartupChannelRegistrationContext { | ||
| registered_channel_names: &[], | ||
| startup_active_channel_names: Some(&startup_active), | ||
| config: &config, | ||
| secrets_store: &None, | ||
| settings_store: None, | ||
| pairing_store: &pairing_store, | ||
| wasm_router: &wasm_router, | ||
| }; | ||
|
|
||
| let (channels, channel_names) = super::register_startup_loaded_channels( | ||
| vec![ | ||
| test_loaded_channel("telegram", serde_json::json!({ "owner_id": 12345 })), | ||
| test_loaded_channel("slack", serde_json::json!({ "owner_id": 67890 })), | ||
| ], | ||
| &context, | ||
| ) | ||
| .await; | ||
|
|
||
| assert_eq!(channel_names, vec!["telegram".to_string()]); | ||
| assert_eq!(channels.len(), 1); | ||
| assert!( | ||
| wasm_router | ||
| .get_channel_for_path("/webhook/telegram") | ||
| .await | ||
| .is_some(), | ||
| "persisted-active channel should be registered on the router" | ||
| ); | ||
| assert!( | ||
| wasm_router | ||
| .get_channel_for_path("/webhook/slack") | ||
| .await | ||
| .is_none(), | ||
| "installed-but-inactive channel must not be registered on the router" | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn register_startup_loaded_channels_without_persistence_restores_all_channels() { | ||
| let (config, _temp_dir) = test_config(); | ||
| let wasm_router = Arc::new(WasmChannelRouter::new()); | ||
| let pairing_store = Arc::new(PairingStore::new_noop()); | ||
| let context = super::StartupChannelRegistrationContext { | ||
| registered_channel_names: &[], | ||
| startup_active_channel_names: None, | ||
| config: &config, | ||
| secrets_store: &None, | ||
| settings_store: None, | ||
| pairing_store: &pairing_store, | ||
| wasm_router: &wasm_router, | ||
| }; | ||
|
|
||
| let (channels, channel_names) = super::register_startup_loaded_channels( | ||
| vec![ | ||
| test_loaded_channel("telegram", serde_json::json!({ "owner_id": 12345 })), | ||
| test_loaded_channel("slack", serde_json::json!({ "owner_id": 67890 })), | ||
| ], | ||
| &context, | ||
| ) | ||
| .await; | ||
|
|
||
| assert_eq!(channels.len(), 2); | ||
| assert_eq!(channel_names.len(), 2); | ||
| assert!( | ||
| wasm_router | ||
| .get_channel_for_path("/webhook/telegram") | ||
| .await | ||
| .is_some() | ||
| ); | ||
| assert!( | ||
| wasm_router | ||
| .get_channel_for_path("/webhook/slack") | ||
| .await | ||
| .is_some() | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn register_channel_routes_capabilities_owner_id_to_wasm_channel() { | ||
| let (config, _temp_dir) = test_config(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Medium Severity — Normalized-vs-raw name mismatch in startup filter
startup_active_channel_namescontains normalized names (hyphens → underscores, vianormalize_persisted_wasm_channel_namesinmain.rs), butdiscover_channelsreturns raw filesystem names. The filter here compares a raw discovered name against the normalized set:startup_active_channel_names .is_none_or(|active_names| active_names.contains(&name))If a WASM file is named with hyphens (e.g.
my-channel.wasm), discovery returns"my-channel"but the normalized set contains"my_channel"— the channel won't be loaded at startup despite being persisted-active. The same mismatch recurs inregister_startup_loaded_channelsat the second filter (line 203).Suggested fix: Normalize the discovered name before comparison:
startup_active_channel_names .is_none_or(|active_names| active_names.contains(&name.replace('-', "_")))Or better — apply a shared
canonicalize_channel_name()once at discovery and once at persistence to eliminate this class of mismatch entirely.