@@ -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
0 commit comments