Skip to content

Commit

Permalink
[client] Fix race conditions (#2869)
Browse files Browse the repository at this point in the history
* Fix concurrent map access in status

* Fix race when retrieving ctx state error

* Fix race when accessing service controller server instance
  • Loading branch information
lixmal authored Nov 11, 2024
1 parent 30f025e commit e0bed2b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
10 changes: 6 additions & 4 deletions client/cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"sync"

"github.com/kardianos/service"
log "github.com/sirupsen/logrus"
Expand All @@ -13,10 +14,11 @@ import (
)

type program struct {
ctx context.Context
cancel context.CancelFunc
serv *grpc.Server
serverInstance *server.Server
ctx context.Context
cancel context.CancelFunc
serv *grpc.Server
serverInstance *server.Server
serverInstanceMu sync.Mutex
}

func newProgram(ctx context.Context, cancel context.CancelFunc) *program {
Expand Down
4 changes: 4 additions & 0 deletions client/cmd/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ func (p *program) Start(svc service.Service) error {
}
proto.RegisterDaemonServiceServer(p.serv, serverInstance)

p.serverInstanceMu.Lock()
p.serverInstance = serverInstance
p.serverInstanceMu.Unlock()

log.Printf("started daemon server: %v", split[1])
if err := p.serv.Serve(listen); err != nil {
Expand All @@ -72,13 +74,15 @@ func (p *program) Start(svc service.Service) error {
}

func (p *program) Stop(srv service.Service) error {
p.serverInstanceMu.Lock()
if p.serverInstance != nil {
in := new(proto.DownRequest)
_, err := p.serverInstance.Down(p.ctx, in)
if err != nil {
log.Errorf("failed to stop daemon: %v", err)
}
}
p.serverInstanceMu.Unlock()

p.cancel()

Expand Down
3 changes: 2 additions & 1 deletion client/internal/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ func (c *ConnectClient) run(mobileDependency MobileDependency, probes *ProbeHold

c.statusRecorder.MarkSignalDisconnected(nil)
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
Expand Down
2 changes: 1 addition & 1 deletion client/internal/peer/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (s *State) DeleteRoute(network string) {
func (s *State) GetRoutes() map[string]struct{} {
s.Mux.RLock()
defer s.Mux.RUnlock()
return s.routes
return maps.Clone(s.routes)
}

// LocalPeerState contains the latest state of the local peer
Expand Down

0 comments on commit e0bed2b

Please sign in to comment.