prevent accidental removal of existing device on duplicate ID conflict

When a new device connection is established, it creates a Device struct and
checks for ID conflicts. If a conflict exists, the new device connection
closes and triggers DelDevice via its defer statement. The original
implementation used LoadAndDelete which removed any device with the given ID,
causing the existing device to be incorrectly removed.

This changes the deletion logic to use CompareAndDelete, which verifies
both the device ID and the specific device instance. Now when a duplicate
connection closes, only the new (unregistered) device is attempted for removal,
preserving the existing device in the registry.

This ensures legitimate devices remain connected when duplicate connection
attempts occur.

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
Jianhui Zhao
2025-08-14 12:17:11 +08:00
parent 9ca83abda9
commit 1d24c36d43
+1 -1
View File
@@ -102,7 +102,7 @@ func (srv *RttyServer) DelDevice(dev *Device) {
return
}
if _, loaded := g.devices.LoadAndDelete(dev.id); loaded {
if deleted := g.devices.CompareAndDelete(dev.id, dev); deleted {
if g.count.Add(-1) == 0 {
srv.groups.Delete(dev.group)
}