From e1ebdb311a0f2ea7ce6f9e11908b2a678e7aa0c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hampus=20Sj=C3=B6berg?= Date: Thu, 15 Apr 2021 11:44:38 +0200 Subject: [PATCH] mobile: Move lndStarted check to the top of the Start function --- mobile/bindings.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/mobile/bindings.go b/mobile/bindings.go index 4b714d37..58cf2a0e 100644 --- a/mobile/bindings.go +++ b/mobile/bindings.go @@ -31,6 +31,15 @@ var lndStarted int32 // current app directory in order to ensure lnd has the permissions needed to // write to it. func Start(extraArgs string, rpcReady Callback) { + // We only support a single lnd instance at a time (singleton) for now, + // so we make sure to return immediately if it has already been + // started. + if !atomic.CompareAndSwapInt32(&lndStarted, 0, 1) { + err := errors.New("lnd already started") + rpcReady.OnError(err) + return + } + // (Re-)initialize the in-mem gRPC listeners we're going to give to lnd. // This is required each time lnd is started, because when lnd shuts // down, the in-mem listeners are closed. @@ -58,6 +67,7 @@ func Start(extraArgs string, rpcReady Callback) { // Hook interceptor for os signals. shutdownInterceptor, err := signal.Intercept() if err != nil { + atomic.StoreInt32(&lndStarted, 0) _, _ = fmt.Fprintln(os.Stderr, err) rpcReady.OnError(err) return @@ -67,6 +77,7 @@ func Start(extraArgs string, rpcReady Callback) { // line options. This function will also set up logging properly. loadedConfig, err := lnd.LoadConfig(shutdownInterceptor) if err != nil { + atomic.StoreInt32(&lndStarted, 0) _, _ = fmt.Fprintln(os.Stderr, err) rpcReady.OnError(err) return @@ -88,15 +99,6 @@ func Start(extraArgs string, rpcReady Callback) { }, } - // We only support a single lnd instance at a time (singleton) for now, - // so we make sure to return immediately if it has already been - // started. - if !atomic.CompareAndSwapInt32(&lndStarted, 0, 1) { - err := errors.New("lnd already started") - rpcReady.OnError(err) - return - } - // Call the "real" main in a nested manner so the defers will properly // be executed in the case of a graceful shutdown. go func() {