Skip to content

Commit 1297e54

Browse files
committed
add lock for deice agent status field
this is accessed/updated by multiple goroutines
1 parent e051a4a commit 1297e54

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Diff for: internal/device-agent/deviceagent.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ import (
1919

2020
type DeviceAgentServer struct {
2121
pb.UnimplementedDeviceAgentServer
22-
AgentStatus *pb.AgentStatus
2322
lock sync.RWMutex
2423
statusChannels map[uuid.UUID]chan *pb.AgentStatus
2524
Config *config.Config
2625
notifier notify.Notifier
2726
rc runtimeconfig.RuntimeConfig
2827
log *logrus.Entry
2928
sendEvent func(state.EventWithSpan)
29+
30+
AgentStatus *pb.AgentStatus
31+
agentStatusLock sync.RWMutex
3032
}
3133

3234
func (das *DeviceAgentServer) Login(ctx context.Context, request *pb.LoginRequest) (*pb.LoginResponse, error) {
@@ -45,7 +47,9 @@ func (das *DeviceAgentServer) Status(request *pb.AgentStatusRequest, statusServe
4547
das.log.Debug("grpc: client connection established to device helper")
4648

4749
agentStatusChan := make(chan *pb.AgentStatus, 8)
50+
das.agentStatusLock.RLock()
4851
agentStatusChan <- das.AgentStatus
52+
das.agentStatusLock.RUnlock()
4953

5054
das.lock.Lock()
5155
das.statusChannels[id] = agentStatusChan
@@ -81,7 +85,9 @@ func (das *DeviceAgentServer) ConfigureJITA(context.Context, *pb.ConfigureJITARe
8185
}
8286

8387
func (das *DeviceAgentServer) UpdateAgentStatus(status *pb.AgentStatus) {
88+
das.agentStatusLock.Lock()
8489
das.AgentStatus = status
90+
das.agentStatusLock.Unlock()
8591

8692
das.lock.RLock()
8793
for _, c := range das.statusChannels {

0 commit comments

Comments
 (0)