@@ -3172,6 +3172,11 @@ private static string FirstLine(string s)
31723172 // port stays put on purpose; the DNS stub re-points at it instead.
31733173 private static bool AutoSwitchWinnerToPrimaryPorts ( AutoRacer w )
31743174 {
3175+ if ( TcpPortBusy ( 9050 ) || TcpPortBusy ( 8118 ) || TcpPortBusy ( 9051 ) )
3176+ {
3177+ AutoEmit ( "[auto] live switch: primary port busy (9050/8118/9051)" ) ;
3178+ return false ;
3179+ }
31753180 try { File . Copy ( AutoRacerCookie ( w ) , ControlCookie , true ) ; }
31763181 catch ( Exception ex )
31773182 {
@@ -3197,16 +3202,28 @@ private static bool AutoSwitchWinnerToPrimaryPorts(AutoRacer w)
31973202 AutoEmit ( "[auto] live switch rejected by tor — falling back to restart" ) ;
31983203 return false ;
31993204 }
3200- Thread . Sleep ( 700 ) ; // let the new control listener come up
3201- int pct ; string tag , summ ;
3202- if ( ! BootstrapPhaseFor ( 9051 , AutoRacerLog ( w ) , ControlCookie ,
3203- out pct , out tag , out summ ) )
3205+ // Tor regenerates the auth cookie when ControlPort changes.
3206+ // Re-copy the NEW cookie before verifying on the new port.
3207+ Thread . Sleep ( 500 ) ;
3208+ try { File . Copy ( AutoRacerCookie ( w ) , ControlCookie , true ) ; }
3209+ catch { }
3210+ for ( int attempt = 1 ; attempt <= 4 ; attempt ++ )
32043211 {
3205- AutoEmit ( "[auto] live switch: control 9051 not answering — falling back" ) ;
3206- return false ;
3212+ int waitMs = attempt == 1 ? 500 : attempt == 2 ? 1000 : 2000 ;
3213+ Thread . Sleep ( waitMs ) ;
3214+ int pct ; string tag , summ ;
3215+ if ( BootstrapPhaseFor ( 9051 , AutoRacerLog ( w ) , ControlCookie ,
3216+ out pct , out tag , out summ ) )
3217+ {
3218+ AutoEmit ( "[auto] live switch OK — winner stays up on 9050/8118 (no restart)" ) ;
3219+ return true ;
3220+ }
3221+ // Re-copy cookie on each retry (tor may still be writing it)
3222+ try { File . Copy ( AutoRacerCookie ( w ) , ControlCookie , true ) ; } catch { }
3223+ AutoEmit ( "[auto] live switch: attempt " + attempt + "/4 — 9051 not ready, retrying..." ) ;
32073224 }
3208- AutoEmit ( "[auto] live switch OK — winner stays up on 9050/8118 (no restart) " ) ;
3209- return true ;
3225+ AutoEmit ( "[auto] live switch: control 9051 not answering after 4 attempts — falling back " ) ;
3226+ return false ;
32103227 }
32113228
32123229 private static void AutoEmit ( string line )
0 commit comments