Skip to content

Commit f7e39a7

Browse files
authored
Merge pull request #15000 from fjl/node-flock
node: fix instance dir locking and improve error message
2 parents 79cdbcf + 7e57fee commit f7e39a7

10 files changed

Lines changed: 551 additions & 24 deletions

File tree

‎node/errors.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,28 @@
1717
package node
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"reflect"
23+
"syscall"
2224
)
2325

26+
var (
27+
ErrDatadirUsed = errors.New("datadir already used by another process")
28+
ErrNodeStopped = errors.New("node not started")
29+
ErrNodeRunning = errors.New("node already running")
30+
ErrServiceUnknown = errors.New("unknown service")
31+
32+
datadirInUseErrnos = map[uint]bool{11: true, 32: true, 35: true}
33+
)
34+
35+
func convertFileLockError(err error) error {
36+
if errno, ok := err.(syscall.Errno); ok && datadirInUseErrnos[uint(errno)] {
37+
return ErrDatadirUsed
38+
}
39+
return err
40+
}
41+
2442
// DuplicateServiceError is returned during Node startup if a registered service
2543
// constructor returns a service of the same type that was already started.
2644
type DuplicateServiceError struct {

‎node/node.go‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"reflect"
2626
"strings"
2727
"sync"
28-
"syscall"
2928

3029
"github.com/ethereum/go-ethereum/accounts"
3130
"github.com/ethereum/go-ethereum/ethdb"
@@ -34,16 +33,7 @@ import (
3433
"github.com/ethereum/go-ethereum/log"
3534
"github.com/ethereum/go-ethereum/p2p"
3635
"github.com/ethereum/go-ethereum/rpc"
37-
"github.com/syndtr/goleveldb/leveldb/storage"
38-
)
39-
40-
var (
41-
ErrDatadirUsed = errors.New("datadir already used")
42-
ErrNodeStopped = errors.New("node not started")
43-
ErrNodeRunning = errors.New("node already running")
44-
ErrServiceUnknown = errors.New("unknown service")
45-
46-
datadirInUseErrnos = map[uint]bool{11: true, 32: true, 35: true}
36+
"github.com/prometheus/prometheus/util/flock"
4737
)
4838

4939
// Node is a container on which services can be registered.
@@ -52,8 +42,8 @@ type Node struct {
5242
config *Config
5343
accman *accounts.Manager
5444

55-
ephemeralKeystore string // if non-empty, the key directory that will be removed by Stop
56-
instanceDirLock storage.Storage // prevents concurrent use of instance directory
45+
ephemeralKeystore string // if non-empty, the key directory that will be removed by Stop
46+
instanceDirLock flock.Releaser // prevents concurrent use of instance directory
5747

5848
serverConfig p2p.Config
5949
server *p2p.Server // Currently running P2P networking layer
@@ -197,10 +187,7 @@ func (n *Node) Start() error {
197187
running.Protocols = append(running.Protocols, service.Protocols()...)
198188
}
199189
if err := running.Start(); err != nil {
200-
if errno, ok := err.(syscall.Errno); ok && datadirInUseErrnos[uint(errno)] {
201-
return ErrDatadirUsed
202-
}
203-
return err
190+
return convertFileLockError(err)
204191
}
205192
// Start each of the services
206193
started := []reflect.Type{}
@@ -242,14 +229,13 @@ func (n *Node) openDataDir() error {
242229
if err := os.MkdirAll(instdir, 0700); err != nil {
243230
return err
244231
}
245-
// Try to open the instance directory as LevelDB storage. This creates a lock file
246-
// which prevents concurrent use by another instance as well as accidental use of the
247-
// instance directory as a database.
248-
storage, err := storage.OpenFile(instdir, true)
232+
// Lock the instance directory to prevent concurrent use by another instance as well as
233+
// accidental use of the instance directory as a database.
234+
release, _, err := flock.New(filepath.Join(instdir, "LOCK"))
249235
if err != nil {
250-
return err
236+
return convertFileLockError(err)
251237
}
252-
n.instanceDirLock = storage
238+
n.instanceDirLock = release
253239
return nil
254240
}
255241

@@ -509,7 +495,9 @@ func (n *Node) Stop() error {
509495

510496
// Release instance directory lock.
511497
if n.instanceDirLock != nil {
512-
n.instanceDirLock.Close()
498+
if err := n.instanceDirLock.Release(); err != nil {
499+
log.Error("Can't release datadir lock", "err", err)
500+
}
513501
n.instanceDirLock = nil
514502
}
515503

‎vendor/github.com/prometheus/prometheus/LICENSE‎

Lines changed: 201 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/prometheus/prometheus/NOTICE‎

Lines changed: 87 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)