mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-24 09:03:14 +01:00
[client] Fix race conditions (#2869)
* Fix concurrent map access in status * Fix race when retrieving ctx state error * Fix race when accessing service controller server instance
This commit is contained in:
parent
30f025e7dd
commit
e0bed2b0fb
@ -2,6 +2,7 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/kardianos/service"
|
"github.com/kardianos/service"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@ -13,10 +14,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type program struct {
|
type program struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
serv *grpc.Server
|
serv *grpc.Server
|
||||||
serverInstance *server.Server
|
serverInstance *server.Server
|
||||||
|
serverInstanceMu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newProgram(ctx context.Context, cancel context.CancelFunc) *program {
|
func newProgram(ctx context.Context, cancel context.CancelFunc) *program {
|
||||||
|
@ -61,7 +61,9 @@ func (p *program) Start(svc service.Service) error {
|
|||||||
}
|
}
|
||||||
proto.RegisterDaemonServiceServer(p.serv, serverInstance)
|
proto.RegisterDaemonServiceServer(p.serv, serverInstance)
|
||||||
|
|
||||||
|
p.serverInstanceMu.Lock()
|
||||||
p.serverInstance = serverInstance
|
p.serverInstance = serverInstance
|
||||||
|
p.serverInstanceMu.Unlock()
|
||||||
|
|
||||||
log.Printf("started daemon server: %v", split[1])
|
log.Printf("started daemon server: %v", split[1])
|
||||||
if err := p.serv.Serve(listen); err != nil {
|
if err := p.serv.Serve(listen); err != nil {
|
||||||
@ -72,6 +74,7 @@ func (p *program) Start(svc service.Service) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *program) Stop(srv service.Service) error {
|
func (p *program) Stop(srv service.Service) error {
|
||||||
|
p.serverInstanceMu.Lock()
|
||||||
if p.serverInstance != nil {
|
if p.serverInstance != nil {
|
||||||
in := new(proto.DownRequest)
|
in := new(proto.DownRequest)
|
||||||
_, err := p.serverInstance.Down(p.ctx, in)
|
_, err := p.serverInstance.Down(p.ctx, in)
|
||||||
@ -79,6 +82,7 @@ func (p *program) Stop(srv service.Service) error {
|
|||||||
log.Errorf("failed to stop daemon: %v", err)
|
log.Errorf("failed to stop daemon: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
p.serverInstanceMu.Unlock()
|
||||||
|
|
||||||
p.cancel()
|
p.cancel()
|
||||||
|
|
||||||
|
@ -207,7 +207,8 @@ func (c *ConnectClient) run(mobileDependency MobileDependency, probes *ProbeHold
|
|||||||
|
|
||||||
c.statusRecorder.MarkSignalDisconnected(nil)
|
c.statusRecorder.MarkSignalDisconnected(nil)
|
||||||
defer func() {
|
defer func() {
|
||||||
c.statusRecorder.MarkSignalDisconnected(state.err)
|
_, err := state.Status()
|
||||||
|
c.statusRecorder.MarkSignalDisconnected(err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// with the global Wiretrustee config in hand connect (just a connection, no stream yet) Signal
|
// with the global Wiretrustee config in hand connect (just a connection, no stream yet) Signal
|
||||||
|
@ -67,7 +67,7 @@ func (s *State) DeleteRoute(network string) {
|
|||||||
func (s *State) GetRoutes() map[string]struct{} {
|
func (s *State) GetRoutes() map[string]struct{} {
|
||||||
s.Mux.RLock()
|
s.Mux.RLock()
|
||||||
defer s.Mux.RUnlock()
|
defer s.Mux.RUnlock()
|
||||||
return s.routes
|
return maps.Clone(s.routes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocalPeerState contains the latest state of the local peer
|
// LocalPeerState contains the latest state of the local peer
|
||||||
|
Loading…
Reference in New Issue
Block a user