From 1d24c36d43e81ee3200de30a03cae2ef48aaed09 Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Thu, 14 Aug 2025 12:17:11 +0800 Subject: [PATCH] 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 --- server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.go b/server.go index b0ca83e..651a252 100644 --- a/server.go +++ b/server.go @@ -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) }