From e53cbd96ad650e2407f0677423d5ba793f73c989 Mon Sep 17 00:00:00 2001 From: CaIon Date: Wed, 3 Sep 2025 15:52:54 +0800 Subject: [PATCH] fix(channel): implement per-channel locking to ensure thread-safe updates in multi-key mode --- model/channel.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/model/channel.go b/model/channel.go index 39cbc022..a61b3ecc 100644 --- a/model/channel.go +++ b/model/channel.go @@ -607,8 +607,12 @@ func UpdateChannelStatus(channelId int, usingKey string, status int, reason stri return false } if channelCache.ChannelInfo.IsMultiKey { + // Use per-channel lock to prevent concurrent map read/write with GetNextEnabledKey + pollingLock := GetChannelPollingLock(channelId) + pollingLock.Lock() // 如果是多Key模式,更新缓存中的状态 handlerMultiKeyUpdate(channelCache, usingKey, status, reason) + pollingLock.Unlock() //CacheUpdateChannel(channelCache) //return true } else { @@ -639,7 +643,11 @@ func UpdateChannelStatus(channelId int, usingKey string, status int, reason stri if channel.ChannelInfo.IsMultiKey { beforeStatus := channel.Status + // Protect map writes with the same per-channel lock used by readers + pollingLock := GetChannelPollingLock(channelId) + pollingLock.Lock() handlerMultiKeyUpdate(channel, usingKey, status, reason) + pollingLock.Unlock() if beforeStatus != channel.Status { shouldUpdateAbilities = true }