Skip to content

Commit 1b997ad

Browse files
author
Delta-Kronecker
committed
fix: auto race live switch reliability
- Re-copy auth cookie after SETCONF (tor regenerates it on ControlPort change) - Retry verification loop (4 attempts, backoff 500ms→1s→2s→2s) - Check primary ports (9050/8118/9051) free before SETCONF - Better logging for each failure mode
1 parent 578c81f commit 1b997ad

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

scripts/start-tor.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)