@@ -51,6 +51,7 @@ struct Server {
5151 config : RefCell < ServerConfig > ,
5252 work_rx : RefCell < Option < crossbeam_channel:: Receiver < RequestWithCompletion > > > ,
5353 work_tx : RefCell < Option < Arc < crossbeam_channel:: Sender < RequestWithCompletion > > > > ,
54+ runtime : RefCell < Option < Arc < tokio:: runtime:: Runtime > > > ,
5455}
5556
5657impl Server {
@@ -62,6 +63,7 @@ impl Server {
6263 config : RefCell :: new ( ServerConfig :: new ( ) ) ,
6364 work_rx : RefCell :: new ( Some ( work_rx) ) ,
6465 work_tx : RefCell :: new ( Some ( Arc :: new ( work_tx) ) ) ,
66+ runtime : RefCell :: new ( None ) ,
6567 }
6668 }
6769
@@ -143,10 +145,13 @@ impl Server {
143145 . ok_or_else ( || MagnusError :: new ( magnus:: exception:: runtime_error ( ) , "Work channel not initialized" ) ) ?
144146 . clone ( ) ;
145147
146- let rt = tokio:: runtime:: Builder :: new_multi_thread ( )
148+ let rt = Arc :: new ( tokio:: runtime:: Builder :: new_multi_thread ( )
147149 . enable_all ( )
148150 . build ( )
149- . map_err ( |e| MagnusError :: new ( magnus:: exception:: runtime_error ( ) , e. to_string ( ) ) ) ?;
151+ . map_err ( |e| MagnusError :: new ( magnus:: exception:: runtime_error ( ) , e. to_string ( ) ) ) ?) ;
152+
153+ // Store the runtime
154+ * self . runtime . borrow_mut ( ) = Some ( rt. clone ( ) ) ;
150155
151156 rt. block_on ( async {
152157 let work_tx = work_tx. clone ( ) ;
@@ -167,7 +172,7 @@ impl Server {
167172 let incoming = UnixListenerStream :: new ( listener) ;
168173 warp:: serve ( any_route)
169174 . run_incoming ( incoming)
170- . await
175+ . await ;
171176 } else {
172177 let addr: SocketAddr = config. bind_address . parse ( )
173178 . expect ( "invalid address format" ) ;
@@ -183,31 +188,31 @@ impl Server {
183188 Ok :: < ( ) , MagnusError > ( ( ) )
184189 } ) ?;
185190
186- // Keep the runtime alive
187- std:: thread:: spawn ( move || {
188- rt. block_on ( async {
189- loop {
190- tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) . await ;
191- }
192- } ) ;
193- } ) ;
194-
195191 Ok ( ( ) )
196192 }
197193
198194 pub fn stop ( & self ) -> Result < ( ) , MagnusError > {
199- let rt = tokio :: runtime:: Runtime :: new ( )
200- . map_err ( |e| MagnusError :: new ( magnus :: exception :: runtime_error ( ) , e . to_string ( ) ) ) ? ;
201-
202- rt . block_on ( async {
203- let mut handle = self . server_handle . lock ( ) . await ;
204- if let Some ( task ) = handle . take ( ) {
205- task . abort ( ) ;
206- }
207- } ) ;
195+ // Use the stored runtime instead of creating a new one
196+ if let Some ( rt ) = self . runtime . borrow ( ) . as_ref ( ) {
197+ rt . block_on ( async {
198+ let mut handle = self . server_handle . lock ( ) . await ;
199+ if let Some ( task ) = handle . take ( ) {
200+ task . abort ( ) ;
201+ }
202+ } ) ;
203+ }
208204
209- // Drop the channel to signal workers to shut down
205+ // Drop the channel and runtime
210206 self . work_tx . borrow_mut ( ) . take ( ) ;
207+ self . runtime . borrow_mut ( ) . take ( ) ;
208+
209+ let bind_address = self . config . borrow ( ) . bind_address . clone ( ) ;
210+ if bind_address. starts_with ( "unix:" ) {
211+ let path = bind_address. trim_start_matches ( "unix:" ) ;
212+ std:: fs:: remove_file ( path) . unwrap_or_else ( |e| {
213+ println ! ( "Failed to remove socket file: {:?}" , e) ;
214+ } ) ;
215+ }
211216
212217 Ok ( ( ) )
213218 }
0 commit comments