Skip to content

Commit 9e2a055

Browse files
committed
close chan with status channel lock
1 parent 1297e54 commit 9e2a055

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import (
1919

2020
type DeviceAgentServer struct {
2121
pb.UnimplementedDeviceAgentServer
22-
lock sync.RWMutex
23-
statusChannels map[uuid.UUID]chan *pb.AgentStatus
24-
Config *config.Config
25-
notifier notify.Notifier
26-
rc runtimeconfig.RuntimeConfig
27-
log *logrus.Entry
28-
sendEvent func(state.EventWithSpan)
22+
Config *config.Config
23+
notifier notify.Notifier
24+
rc runtimeconfig.RuntimeConfig
25+
log *logrus.Entry
26+
sendEvent func(state.EventWithSpan)
27+
28+
statusChannelsLock sync.RWMutex
29+
statusChannels map[uuid.UUID]chan *pb.AgentStatus
2930

3031
AgentStatus *pb.AgentStatus
3132
agentStatusLock sync.RWMutex
@@ -51,20 +52,20 @@ func (das *DeviceAgentServer) Status(request *pb.AgentStatusRequest, statusServe
5152
agentStatusChan <- das.AgentStatus
5253
das.agentStatusLock.RUnlock()
5354

54-
das.lock.Lock()
55+
das.statusChannelsLock.Lock()
5556
das.statusChannels[id] = agentStatusChan
56-
das.lock.Unlock()
57+
das.statusChannelsLock.Unlock()
5758

5859
defer func() {
5960
das.log.Debugf("grpc: client connection with device helper closed")
6061
if !request.GetKeepConnectionOnComplete() {
6162
das.log.Debugf("grpc: keepalive not requested, tearing down connections...")
6263
das.sendEvent(state.SpanEvent(statusServer.Context(), state.EventDisconnect))
6364
}
65+
das.statusChannelsLock.Lock()
6466
close(agentStatusChan)
65-
das.lock.Lock()
6667
delete(das.statusChannels, id)
67-
das.lock.Unlock()
68+
das.statusChannelsLock.Unlock()
6869
}()
6970

7071
for {
@@ -89,15 +90,15 @@ func (das *DeviceAgentServer) UpdateAgentStatus(status *pb.AgentStatus) {
8990
das.AgentStatus = status
9091
das.agentStatusLock.Unlock()
9192

92-
das.lock.RLock()
93+
das.statusChannelsLock.RLock()
9394
for _, c := range das.statusChannels {
9495
select {
9596
case c <- status:
9697
default:
9798
das.log.Errorf("BUG: update agent status: channel is full")
9899
}
99100
}
100-
das.lock.RUnlock()
101+
das.statusChannelsLock.RUnlock()
101102
}
102103

103104
func (das *DeviceAgentServer) SetAgentConfiguration(ctx context.Context, req *pb.SetAgentConfigurationRequest) (*pb.SetAgentConfigurationResponse, error) {

0 commit comments

Comments
 (0)