@@ -125,19 +125,19 @@ impl ListenersManager {
125125 let listener_name = listener. get_name ( ) ;
126126 let ( addr, dev) = listener. get_socket ( ) ;
127127 info ! ( "Listener {} at {addr} (device bind:{})" , listener_name, dev. is_some( ) ) ;
128+
129+ // Stop the old listener if it exists before starting a new one
130+ if self . listener_handles . contains_key ( & listener_name) {
131+ self . stop_listener ( & listener_name) ?;
132+ #[ cfg( debug_assertions) ]
133+ debug ! ( "Listener {listener_name} already exists, stopping and replacing it" ) ;
134+ }
135+
128136 // spawn the task for this listener address, this will spawn additional task per connection
129137 let join_handle = tokio:: spawn ( async move {
130138 let error = listener. start ( ) . await ;
131139 warn ! ( "Listener {listener_name} exited: {error}" ) ;
132140 } ) ;
133- #[ cfg( debug_assertions) ]
134- if self . listener_handles . contains_key ( & listener_name) {
135- debug ! ( "Listener {listener_name} already exists, replacing it" ) ;
136- }
137- // note: join handle gets overwritten here if it already exists.
138- // handles are abort on drop so will be aborted, closing the socket
139- // but the any tasks spawned within this task, which happens on a per-connection basis,
140- // will survive past this point and only get dropped when their session ends
141141 self . listener_handles . insert ( listener_name, ListenerInfo :: new ( join_handle, listener_conf) ) ;
142142
143143 Ok ( ( ) )
0 commit comments