@@ -248,7 +248,7 @@ fn adb_forward_port(
248248 if let Some ( ( target_device_serial_no, target_device_name) ) = target_device {
249249 let mut already_forwarded = false ;
250250
251- // clear port forwarding for all devices
251+ // clear port forwarding for all devices other than the target
252252 for device in & devices {
253253 let reverse_list_output =
254254 adb_reverse_list ( env, device. serial_no ( ) ) . map_err ( |error| Error :: CommandFailed {
@@ -257,25 +257,37 @@ fn adb_forward_port(
257257 } ) ?;
258258
259259 // check if the device has the port forwarded
260- if String :: from_utf8_lossy ( & reverse_list_output. stdout ) . contains ( & forward) {
260+ if !reverse_list_has_forward (
261+ & String :: from_utf8_lossy ( & reverse_list_output. stdout ) ,
262+ & forward,
263+ ) {
264+ continue ;
265+ }
266+
267+ if device. serial_no ( ) == target_device_serial_no {
261268 // device matches our target, we can skip forwarding
262- if device. serial_no ( ) == target_device_serial_no {
263- log:: debug!(
264- "device {} already has the forward for {}" ,
265- device. name( ) ,
266- forward
267- ) ;
268- already_forwarded = true ;
269- }
270- break ;
269+ log:: debug!(
270+ "device {} already has the forward for {}" ,
271+ device. name( ) ,
272+ forward
273+ ) ;
274+ already_forwarded = true ;
275+ } else if let Err ( error) = adb_reverse_remove ( env, device. serial_no ( ) , & forward) {
276+ log:: warn!(
277+ "failed to remove the {forward} forward from device {}: {error}" ,
278+ device. name( )
279+ ) ;
271280 }
272281 }
273282
274283 // if there's a known target, we should forward the port to it
275284 if already_forwarded {
276285 log:: info!( "{forward} already forwarded to {target_device_name}" ) ;
277286 } else {
287+ const MAX_ATTEMPTS : u32 = 5 ;
288+ let mut attempt = 0 ;
278289 loop {
290+ attempt += 1 ;
279291 run_adb_reverse ( env, & target_device_serial_no, & forward, & forward) . map_err ( |error| {
280292 Error :: CommandFailed {
281293 command : format ! ( "adb reverse {forward} {forward}" ) ,
@@ -291,7 +303,15 @@ fn adb_forward_port(
291303 }
292304 } ) ?;
293305 // wait and retry until the port has actually been forwarded
294- if String :: from_utf8_lossy ( & reverse_list_output. stdout ) . contains ( & forward) {
306+ if reverse_list_has_forward (
307+ & String :: from_utf8_lossy ( & reverse_list_output. stdout ) ,
308+ & forward,
309+ ) {
310+ break ;
311+ } else if attempt >= MAX_ATTEMPTS {
312+ log:: warn!(
313+ "could not verify that {forward} was forwarded to {target_device_name} after {MAX_ATTEMPTS} attempts; the app might not be able to reach the dev server"
314+ ) ;
295315 break ;
296316 } else {
297317 log:: warn!(
@@ -332,3 +352,47 @@ fn adb_reverse_list(
332352 . stderr_capture ( )
333353 . run ( )
334354}
355+
356+ fn adb_reverse_remove (
357+ env : & cargo_mobile2:: android:: env:: Env ,
358+ device_serial_no : & str ,
359+ remote : & str ,
360+ ) -> std:: io:: Result < std:: process:: Output > {
361+ adb:: adb ( env, [ "-s" , device_serial_no, "reverse" , "--remove" , remote] )
362+ . stdin_file ( os_pipe:: dup_stdin ( ) . unwrap ( ) )
363+ . stdout_capture ( )
364+ . stderr_capture ( )
365+ . run ( )
366+ }
367+
368+ /// Checks whether the output of `adb reverse --list` contains the given `tcp:<port>` forward.
369+ ///
370+ /// Each line has the format `<serial> <remote> <local>`, so we compare the remote column exactly
371+ /// to avoid e.g. `tcp:80` matching `tcp:8080`.
372+ fn reverse_list_has_forward ( output : & str , forward : & str ) -> bool {
373+ output
374+ . lines ( )
375+ . any ( |line| line. split_whitespace ( ) . nth ( 1 ) == Some ( forward) )
376+ }
377+
378+ #[ cfg( test) ]
379+ mod tests {
380+ use super :: reverse_list_has_forward;
381+
382+ #[ test]
383+ fn reverse_list_matches_exact_port ( ) {
384+ let output = "emulator-5554 tcp:8080 tcp:8080\n emulator-5554 tcp:1420 tcp:1420\n " ;
385+ assert ! ( reverse_list_has_forward( output, "tcp:8080" ) ) ;
386+ assert ! ( reverse_list_has_forward( output, "tcp:1420" ) ) ;
387+ assert ! ( !reverse_list_has_forward( output, "tcp:80" ) ) ;
388+ assert ! ( !reverse_list_has_forward( output, "tcp:142" ) ) ;
389+ assert ! ( !reverse_list_has_forward( "" , "tcp:1420" ) ) ;
390+ }
391+
392+ #[ test]
393+ fn reverse_list_ignores_local_column ( ) {
394+ let output = "UsbFfs tcp:5173 tcp:1420\n " ;
395+ assert ! ( reverse_list_has_forward( output, "tcp:5173" ) ) ;
396+ assert ! ( !reverse_list_has_forward( output, "tcp:1420" ) ) ;
397+ }
398+ }
0 commit comments