|
18 | 18 | use std::collections::BTreeMap; |
19 | 19 |
|
20 | 20 | use tokio::sync::{broadcast, mpsc}; |
21 | | -#[cfg(debug_assertions)] |
22 | | -use tracing::debug; |
23 | 21 | use tracing::{info, warn}; |
24 | 22 |
|
25 | 23 | use orion_configuration::config::{ |
@@ -128,19 +126,18 @@ impl ListenersManager { |
128 | 126 | let listener_name = listener.get_name(); |
129 | 127 | let (addr, dev) = listener.get_socket(); |
130 | 128 | 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 | + |
131 | 136 | // spawn the task for this listener address, this will spawn additional task per connection |
132 | 137 | let join_handle = tokio::spawn(async move { |
133 | 138 | let error = listener.start().await; |
134 | 139 | warn!("Listener {listener_name} exited: {error}"); |
135 | 140 | }); |
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 |
144 | 141 | self.listener_handles.insert(listener_name, ListenerInfo::new(join_handle, listener_conf)); |
145 | 142 |
|
146 | 143 | Ok(()) |
@@ -201,10 +198,8 @@ mod tests { |
201 | 198 | man.start_listener(l2, l2_info).unwrap(); |
202 | 199 | assert!(routeb_tx2.send(RouteConfigurationChange::Removed("n/a".into())).is_ok()); |
203 | 200 | 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 |
206 | 202 | assert!(routeb_tx1.send(RouteConfigurationChange::Removed("n/a".into())).is_err()); |
207 | | - // Yield once more just in case more logs can be seen |
208 | 203 | tokio::task::yield_now().await; |
209 | 204 | } |
210 | 205 |
|
|
0 commit comments