Skip to content

Commit 4d70017

Browse files
Merge pull request #6520 from oasisprotocol/martin/bugfix/is-node-running
go/oasis-node/cmd/common: Fix isNodeRunning function
2 parents 5da32b2 + 6a3b823 commit 4d70017

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

.changelog/6520.bugfix.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
go/oasis-node/cmd/common: Fix is node running helper
2+
3+
Now the helper tries to dial the node socket to check
4+
whether the node is running.

go/oasis-node/cmd/common/common.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import (
66
"fmt"
77
"io"
88
"io/fs"
9+
"net"
910
"os"
1011
"path/filepath"
1112
"strings"
13+
"syscall"
14+
"time"
1215

1316
"github.com/spf13/cobra"
1417
flag "github.com/spf13/pflag"
@@ -77,11 +80,14 @@ func InternalSocketPath() string {
7780
func IsNodeRunning() (bool, error) {
7881
path := InternalSocketPath()
7982

80-
if _, err := os.Stat(path); err != nil {
81-
if errors.Is(err, fs.ErrNotExist) {
82-
return false, nil
83-
}
84-
return false, fmt.Errorf("stat %s: %w", path, err)
83+
conn, err := net.DialTimeout("unix", path, time.Second)
84+
switch {
85+
case err == nil:
86+
_ = conn.Close()
87+
case errors.Is(err, fs.ErrNotExist) || errors.Is(err, syscall.ECONNREFUSED):
88+
return false, nil
89+
default:
90+
return false, fmt.Errorf("failed to dial internal socket %s: %w", path, err)
8591
}
8692

8793
return true, nil

0 commit comments

Comments
 (0)