Reapply "chore: fix potential deadlock"

This reverts commit 6116e8c934.
This commit is contained in:
andig 2024-08-18 20:24:40 +02:00
parent ac5e68648d
commit d74fbd383c

View file

@ -56,16 +56,18 @@ func (cp *handler[T]) Add(dev Device[T]) error {
// Delete deletes device
func (cp *handler[T]) Delete(name string) error {
cp.mu.RLock()
defer cp.mu.RUnlock()
cp.mu.Lock()
for i, dev := range cp.devices {
if name == dev.Config().Name {
cp.devices = append(cp.devices[:i], cp.devices[i+1:]...)
cp.mu.Unlock()
bus.Publish(cp.topic, OpDelete, dev)
return nil
}
}
cp.mu.Unlock()
return fmt.Errorf("not found: %s", name)
}