From ddbd7a68acff54b4e8593a83c1b77d65099c1d32 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Thu, 21 Nov 2019 13:56:48 +0100 Subject: [PATCH] lntest/node: add numActiveNodesMtx Fixes a slight race condition that could happen since Alice and Bob are created in different goroutines, leading to using the same listening ports. --- lntest/node.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lntest/node.go b/lntest/node.go index 26a5e0532..ca1f00784 100644 --- a/lntest/node.go +++ b/lntest/node.go @@ -77,7 +77,8 @@ const ( var ( // numActiveNodes is the number of active nodes within the test network. - numActiveNodes = 0 + numActiveNodes = 0 + numActiveNodesMtx sync.Mutex // logOutput is a flag that can be set to append the output from the // seed nodes to log files. @@ -96,6 +97,9 @@ var ( // support multiple test nodes running at once, the p2p, rpc, rest and // profiling ports are incremented after each initialization. func generateListeningPorts() (int, int, int, int) { + numActiveNodesMtx.Lock() + defer numActiveNodesMtx.Unlock() + p2p := defaultNodePort + (4 * numActiveNodes) rpc := defaultClientPort + (4 * numActiveNodes) rest := defaultRestPort + (4 * numActiveNodes) @@ -295,8 +299,10 @@ func newNode(cfg nodeConfig) (*HarnessNode, error) { cfg.P2PPort, cfg.RPCPort, cfg.RESTPort, cfg.ProfilePort = generateListeningPorts() + numActiveNodesMtx.Lock() nodeNum := numActiveNodes numActiveNodes++ + numActiveNodesMtx.Unlock() return &HarnessNode{ cfg: &cfg,