From bfb1e07720e241c65f00d4c461b12c5450a65476 Mon Sep 17 00:00:00 2001 From: Maru Newby Date: Tue, 3 Sep 2024 13:09:56 -0700 Subject: [PATCH 1/2] [tmpnet] Avoid calling Stat in advance of Read/Open Checking for a file/dir in advance of attempting to open it is redundant and invites race conditions: https://github.com/ava-labs/avalanchego/actions/runs/10688505984/job/29628507363 Prefer checking for ErrNotExist in the call to read the file/dir. --- tests/fixture/tmpnet/node_process.go | 8 +++----- tests/fixture/tmpnet/subnet.go | 8 ++------ 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/tests/fixture/tmpnet/node_process.go b/tests/fixture/tmpnet/node_process.go index d7d95a697dd..b33fe32f073 100644 --- a/tests/fixture/tmpnet/node_process.go +++ b/tests/fixture/tmpnet/node_process.go @@ -77,14 +77,12 @@ func (p *NodeProcess) setProcessContext(processContext node.NodeProcessContext) func (p *NodeProcess) readState() error { path := p.getProcessContextPath() - if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) { + bytes, err := os.ReadFile(path) + if errors.Is(err, fs.ErrNotExist) { // The absence of the process context file indicates the node is not running p.setProcessContext(node.NodeProcessContext{}) return nil - } - - bytes, err := os.ReadFile(path) - if err != nil { + } else if err != nil { return fmt.Errorf("failed to read node process context: %w", err) } processContext := node.NodeProcessContext{} diff --git a/tests/fixture/tmpnet/subnet.go b/tests/fixture/tmpnet/subnet.go index a9062f2fb5c..79bf2d9a9ed 100644 --- a/tests/fixture/tmpnet/subnet.go +++ b/tests/fixture/tmpnet/subnet.go @@ -339,14 +339,10 @@ func WaitForActiveValidators( // Reads subnets from [network dir]/subnets/[subnet name].json func readSubnets(subnetDir string) ([]*Subnet, error) { - if _, err := os.Stat(subnetDir); os.IsNotExist(err) { + entries, err := os.ReadDir(subnetDir) + if os.IsNotExist(err) { return nil, nil } else if err != nil { - return nil, err - } - - entries, err := os.ReadDir(subnetDir) - if err != nil { return nil, fmt.Errorf("failed to read subnet dir: %w", err) } From ddd296034a5deb3a6228968bd8838ead5261470d Mon Sep 17 00:00:00 2001 From: Maru Newby Date: Tue, 3 Sep 2024 13:26:37 -0700 Subject: [PATCH 2/2] fixup: respond to review feedback --- tests/fixture/tmpnet/subnet.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/fixture/tmpnet/subnet.go b/tests/fixture/tmpnet/subnet.go index 79bf2d9a9ed..1e1d3c749c7 100644 --- a/tests/fixture/tmpnet/subnet.go +++ b/tests/fixture/tmpnet/subnet.go @@ -6,6 +6,7 @@ package tmpnet import ( "context" "encoding/json" + "errors" "fmt" "io" "os" @@ -340,7 +341,7 @@ func WaitForActiveValidators( // Reads subnets from [network dir]/subnets/[subnet name].json func readSubnets(subnetDir string) ([]*Subnet, error) { entries, err := os.ReadDir(subnetDir) - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { return nil, nil } else if err != nil { return nil, fmt.Errorf("failed to read subnet dir: %w", err)