From e353e4c70d43bdd327749ba7bea1d0cfe9a10830 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 16 Nov 2020 15:03:12 -0800 Subject: [PATCH] Add backoff to handle slow android socket setup --- tor/tor.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tor/tor.go b/tor/tor.go index f7c35c9..4a081ef 100644 --- a/tor/tor.go +++ b/tor/tor.go @@ -173,11 +173,25 @@ func Start(ctx context.Context, conf *StartConf) (*Tor, error) { if err == nil { err = tor.startProcess(ctx, conf) } - time.Sleep(time.Second) + + // Connect the controller if err == nil { - err = tor.connectController(ctx, conf) + for attempts := 0; attempts < 10; attempts++ { + err = tor.connectController(ctx, conf) + if err == nil { + break + } else { + // backoff and retry + // max is 1 + 2 + 3 + 4 + 5...+10 = ~55 seconds + // After which point there is probably something else very wrong + // TODO: It might be worth breaking out the error here so that we can distinguish a + // slow socket (on e.g. Android) with a failed setup. + time.Sleep(time.Duration(int(time.Second) * attempts)) + } + } } + // Attempt eager auth w/ no password if err == nil && !conf.DisableEagerAuth { err = tor.Control.Authenticate("")