Skip to content

Commit 03dafea

Browse files
committed
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 <eeshuyadav123@gmail.com>
1 parent 59adc36 commit 03dafea

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

orion-lib/src/listeners/listeners_manager.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
use std::collections::BTreeMap;
1919

2020
use tokio::sync::{broadcast, mpsc};
21-
#[cfg(debug_assertions)]
22-
use tracing::debug;
2321
use tracing::{info, warn};
2422

2523
use orion_configuration::config::{
@@ -128,19 +126,18 @@ impl ListenersManager {
128126
let listener_name = listener.get_name();
129127
let (addr, dev) = listener.get_socket();
130128
info!("Listener {} at {addr} (device bind:{})", listener_name, dev.is_some());
129+
130+
// Stop existing listener if one exists with the same name to prevent mixed responses
131+
if self.listener_handles.contains_key(&listener_name) {
132+
info!("Listener {listener_name} already exists, stopping existing listener before starting new one");
133+
self.stop_listener(&listener_name)?;
134+
}
135+
131136
// spawn the task for this listener address, this will spawn additional task per connection
132137
let join_handle = tokio::spawn(async move {
133138
let error = listener.start().await;
134139
warn!("Listener {listener_name} exited: {error}");
135140
});
136-
#[cfg(debug_assertions)]
137-
if self.listener_handles.contains_key(&listener_name) {
138-
debug!("Listener {listener_name} already exists, replacing it");
139-
}
140-
// note: join handle gets overwritten here if it already exists.
141-
// handles are abort on drop so will be aborted, closing the socket
142-
// but the any tasks spawned within this task, which happens on a per-connection basis,
143-
// will survive past this point and only get dropped when their session ends
144141
self.listener_handles.insert(listener_name, ListenerInfo::new(join_handle, listener_conf));
145142

146143
Ok(())
@@ -201,10 +198,8 @@ mod tests {
201198
man.start_listener(l2, l2_info).unwrap();
202199
assert!(routeb_tx2.send(RouteConfigurationChange::Removed("n/a".into())).is_ok());
203200
tokio::task::yield_now().await;
204-
205-
// This should fail because the old listener exited already dropping the rx
201+
// The first listener should now be stopped, so this should fail
206202
assert!(routeb_tx1.send(RouteConfigurationChange::Removed("n/a".into())).is_err());
207-
// Yield once more just in case more logs can be seen
208203
tokio::task::yield_now().await;
209204
}
210205

0 commit comments

Comments
 (0)