get localnode address up front on creation to save potential data race (#7111)

the log line here was the culprit for the race. made sense to just
capture this on localnode creation instead and hold onto it for when the
server is started.

ran test with `-race` and `-count=5000` to double check and all looks
good
This commit is contained in:
hexoscott 2023-03-16 03:44:00 +00:00 committed by GitHub
parent 87c958b5c1
commit 3b36d5d57a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -196,12 +196,13 @@ type Server struct {
peerFeed event.Feed
log log.Logger
nodedb *enode.DB
localnode *enode.LocalNode
ntab *discover.UDPv4
DiscV5 *discover.UDPv5
discmix *enode.FairMix
dialsched *dialScheduler
nodedb *enode.DB
localnode *enode.LocalNode
localnodeAddress string
ntab *discover.UDPv4
DiscV5 *discover.UDPv5
discmix *enode.FairMix
dialsched *dialScheduler
// Channels into the run loop.
quitCtx context.Context
@ -545,6 +546,7 @@ func (srv *Server) setupLocalNode() error {
srv.nodedb = db
srv.localnode = enode.NewLocalNode(db, srv.PrivateKey)
srv.localnode.SetFallbackIP(net.IP{127, 0, 0, 1})
srv.localnodeAddress = srv.localnode.Node().URLv4()
// TODO: check conflicts
for _, p := range srv.Protocols {
for _, e := range p.Attributes {
@ -745,7 +747,7 @@ func (srv *Server) doPeerOp(fn peerOpFunc) {
func (srv *Server) run() {
defer debug.LogPanic()
if len(srv.Config.Protocols) > 0 {
srv.log.Info("Started P2P networking", "version", srv.Config.Protocols[0].Version, "self", srv.localnode.Node().URLv4(), "name", srv.Name)
srv.log.Info("Started P2P networking", "version", srv.Config.Protocols[0].Version, "self", srv.localnodeAddress, "name", srv.Name)
}
defer srv.loopWG.Done()
defer srv.nodedb.Close()