From 03dafea24e3a897555acbf9bd2190cc52e8682c3 Mon Sep 17 00:00:00 2001 From: Eeshu-Yadav Date: Tue, 2 Sep 2025 02:13:00 +0530 Subject: [PATCH] listeners: fix duplicate listener names by stopping existing listener - Check if listener with same name exists before starting new one - Stop the existing listener to prevent conflicts - Improve logging for duplicate listener detection Signed-off-by: Eeshu-Yadav --- orion-lib/src/listeners/listeners_manager.rs | 21 ++++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/orion-lib/src/listeners/listeners_manager.rs b/orion-lib/src/listeners/listeners_manager.rs index 645a2ef1..835120ad 100644 --- a/orion-lib/src/listeners/listeners_manager.rs +++ b/orion-lib/src/listeners/listeners_manager.rs @@ -18,8 +18,6 @@ use std::collections::BTreeMap; use tokio::sync::{broadcast, mpsc}; -#[cfg(debug_assertions)] -use tracing::debug; use tracing::{info, warn}; use orion_configuration::config::{ @@ -128,19 +126,18 @@ impl ListenersManager { let listener_name = listener.get_name(); let (addr, dev) = listener.get_socket(); info!("Listener {} at {addr} (device bind:{})", listener_name, dev.is_some()); + + // Stop existing listener if one exists with the same name to prevent mixed responses + if self.listener_handles.contains_key(&listener_name) { + info!("Listener {listener_name} already exists, stopping existing listener before starting new one"); + self.stop_listener(&listener_name)?; + } + // spawn the task for this listener address, this will spawn additional task per connection let join_handle = tokio::spawn(async move { let error = listener.start().await; warn!("Listener {listener_name} exited: {error}"); }); - #[cfg(debug_assertions)] - if self.listener_handles.contains_key(&listener_name) { - debug!("Listener {listener_name} already exists, replacing it"); - } - // note: join handle gets overwritten here if it already exists. - // handles are abort on drop so will be aborted, closing the socket - // but the any tasks spawned within this task, which happens on a per-connection basis, - // will survive past this point and only get dropped when their session ends self.listener_handles.insert(listener_name, ListenerInfo::new(join_handle, listener_conf)); Ok(()) @@ -201,10 +198,8 @@ mod tests { man.start_listener(l2, l2_info).unwrap(); assert!(routeb_tx2.send(RouteConfigurationChange::Removed("n/a".into())).is_ok()); tokio::task::yield_now().await; - - // This should fail because the old listener exited already dropping the rx + // The first listener should now be stopped, so this should fail assert!(routeb_tx1.send(RouteConfigurationChange::Removed("n/a".into())).is_err()); - // Yield once more just in case more logs can be seen tokio::task::yield_now().await; }