From baada32cb6dbe95a14ac15cb8f35600934c07bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=BB=E4=B9=90=E5=A4=9A?= <865730675@qq.com> Date: Fri, 3 Jul 2026 10:49:28 +0800 Subject: [PATCH] fix(im/feishu): actually close WebSocket long connection when channel is stopped MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Deleting or disabling a Feishu channel left its WebSocket long connection alive: the Lark SDK's Start blocks on a bare select{} and never observes ctx, and v3.6.1 exposed no public Close(), so the wsCancel() cleanup was a no-op. The socket stayed open, the SDK kept auto-reconnecting, and Feishu kept pushing messages — causing cross-environment message duplication when the same bot was bound elsewhere. Fix in three layers: 1. Connection teardown - Upgrade oapi-sdk-go v3.6.1 -> v3.9.7 (adds Client.Close()). - Add LongConnClient.Close() wrapping wsClient.Close(); factory's cleanup now calls Close() first, then wsCancel() as a belt-and-braces fallback. 2. Multi-instance coordination - wsLeaderRetryLoop: after acquiring leadership, re-check the DB before starting; abort if the channel was deleted/disabled while waiting. - wsLeaderRenewLoop: on each renewal, re-check the DB; step down if the channel was deleted/disabled on another instance (HTTP request may have been served elsewhere), so the leader doesn't keep the connection open until process restart. 3. Robustness - DB checks distinguish gorm.ErrRecordNotFound (channel truly gone -> stop/abort) from transient DB errors (skip this round, retry next tick) so a DB hiccup can't cause a spurious stop or permanent leader-abort. - startChannelInternal now stops any existing channelState for the same ID before overwriting, making it idempotent against concurrent start paths and preventing adapter/long-connection leaks. No interface changes; webhook and long-poll modes unaffected. Co-Authored-By: Claude --- go.mod | 2 +- go.sum | 2 ++ internal/im/feishu/factory.go | 11 ++++++- internal/im/feishu/longconn.go | 13 ++++++++ internal/im/service.go | 59 ++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 382ac3c6..efb749fd 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,7 @@ require ( github.com/jackc/pgx/v5 v5.9.2 github.com/joho/godotenv v1.5.1 github.com/ks3sdklib/aws-sdk-go v1.11.0 - github.com/larksuite/oapi-sdk-go/v3 v3.6.1 + github.com/larksuite/oapi-sdk-go/v3 v3.9.7 github.com/longbridgeapp/opencc v0.3.13 github.com/mark3labs/mcp-go v0.52.0 github.com/milvus-io/milvus/client/v2 v2.6.4 diff --git a/go.sum b/go.sum index 46080979..07ef63e7 100644 --- a/go.sum +++ b/go.sum @@ -2125,6 +2125,8 @@ github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0 github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= github.com/larksuite/oapi-sdk-go/v3 v3.6.1 h1:vAdu+sX9yXNkKnKnYQeIv6yBkjP37Q1JEJHmMa2eCjQ= github.com/larksuite/oapi-sdk-go/v3 v3.6.1/go.mod h1:ZEplY+kwuIrj/nqw5uSCINNATcH3KdxSN7y+UxYY5fI= +github.com/larksuite/oapi-sdk-go/v3 v3.9.7 h1:0AlevGG8zJ5uLhDuxgG4N1ULc6s7GEenV7ZRfER2R+Y= +github.com/larksuite/oapi-sdk-go/v3 v3.9.7/go.mod h1:ZEplY+kwuIrj/nqw5uSCINNATcH3KdxSN7y+UxYY5fI= github.com/leaanthony/debme v1.2.1 h1:9Tgwf+kjcrbMQ4WnPcEIUcQuIZYqdWftzZkBr+i/oOc= github.com/leaanthony/debme v1.2.1/go.mod h1:3V+sCm5tYAgQymvSOfYQ5Xx2JCr+OXiD9Jkw3otUjiA= github.com/leaanthony/go-ansi-parser v1.6.1 h1:xd8bzARK3dErqkPFtoF9F3/HgN8UQk0ed1YDKpEz01A= diff --git a/internal/im/feishu/factory.go b/internal/im/feishu/factory.go index 8e4db0e5..24bee0bd 100644 --- a/internal/im/feishu/factory.go +++ b/internal/im/feishu/factory.go @@ -42,7 +42,16 @@ func NewFactory() im.AdapterFactory { } }() - return adapter, wsCancel, nil + // stop tears down the connection for real. wsCancel alone is a + // no-op: the Feishu SDK's Start blocks on select{} and never + // observes ctx, so we must call Close() to actually close the + // socket and disable auto-reconnect. wsCancel is kept as a + // belt-and-braces fallback for the start goroutine. + stop := func() { + client.Close() + wsCancel() + } + return adapter, stop, nil default: return nil, nil, fmt.Errorf("unknown Feishu mode: %s", mode) diff --git a/internal/im/feishu/longconn.go b/internal/im/feishu/longconn.go index 1f70bf66..6da1810a 100644 --- a/internal/im/feishu/longconn.go +++ b/internal/im/feishu/longconn.go @@ -53,6 +53,19 @@ func (c *LongConnClient) Start(ctx context.Context) error { return c.wsClient.Start(ctx) } +// Close tears down the WebSocket long connection. +// +// This is the only reliable way to stop a Feishu long connection: the SDK's +// Start blocks on a bare select{} and neither Start, pingLoop, nor +// receiveMessageLoop observe the passed context, so cancelling ctx alone leaves +// the underlying socket alive and the SDK auto-reconnecting. Client.Close() +// (added in oapi-sdk-go v3.9.7) flips autoReconnect off and calls disconnect, +// which actually closes the socket. Callers should still cancel the start ctx +// as a belt-and-braces fallback for the start goroutine. +func (c *LongConnClient) Close() { + c.wsClient.Close() +} + // feishuLoggerAdapter bridges the Feishu SDK logger to our unified logger, // replacing raw SDK connection messages with a consistent format. type feishuLoggerAdapter struct { diff --git a/internal/im/service.go b/internal/im/service.go index 0282804d..92cf2406 100644 --- a/internal/im/service.go +++ b/internal/im/service.go @@ -965,6 +965,12 @@ func (s *Service) startChannelInternal(channel *IMChannel, factory AdapterFactor } s.mu.Lock() + // Idempotency: another goroutine may have started this channel while + // factory was running above (factory is called unlocked). Stop the old + // state before overwriting so its adapter / long connection doesn't leak. + if existing, ok := s.channels[channel.ID]; ok { + s.stopChannelLocked(channel.ID, existing) + } s.channels[channel.ID] = &channelState{ Channel: channel, Adapter: adapter, @@ -1068,6 +1074,31 @@ func (s *Service) wsLeaderRenewLoop(ctx context.Context, channelID string) { s.StopChannel(channelID) return } + // Still the leader — verify the channel is still active. A + // delete/disable is served by whichever instance got the HTTP + // request; without this check the leader would keep the long + // connection open until process restart. The renew interval + // bounds the worst-case lag. + ch, err := s.GetChannelByID(channelID) + if errors.Is(err, gorm.ErrRecordNotFound) { + logger.Infof(context.Background(), + "[IM] Channel %s deleted; leader stepping down", channelID) + s.StopChannel(channelID) + return + } + if err != nil { + // Transient DB error — don't stop a possibly-healthy channel + // on a DB hiccup. Skip this round; the next renewal re-checks. + logger.Warnf(context.Background(), + "[IM] DB check failed for channel %s during leader renewal: %v (skipping this round)", channelID, err) + continue + } + if !ch.Enabled { + logger.Infof(context.Background(), + "[IM] Channel %s disabled; leader stepping down", channelID) + s.StopChannel(channelID) + return + } case <-ctx.Done(): return } @@ -1088,6 +1119,34 @@ func (s *Service) wsLeaderRetryLoop(channel *IMChannel) { return } if s.tryAcquireWSLeader(channel.ID) { + // Re-check the DB before starting: the channel may have been + // deleted or disabled on another instance while we waited for + // leadership. The in-memory `channel` is a startup snapshot and + // won't reflect that, so without this guard we'd resurrect a + // stopped channel (and reopen its long connection). + fresh, err := s.GetChannelByID(channel.ID) + if errors.Is(err, gorm.ErrRecordNotFound) { + // Channel was actually deleted — give up for good. + s.releaseWSLeader(channel.ID) + logger.Infof(context.Background(), + "[IM] Channel %s deleted while waiting for leadership; aborting leader takeover", channel.ID) + return + } + if err != nil { + // Transient DB error — don't make a destructive decision. + // Release the lock for this round and retry on the next tick. + s.releaseWSLeader(channel.ID) + logger.Warnf(context.Background(), + "[IM] DB check failed for channel %s during leader takeover: %v (will retry)", channel.ID, err) + continue + } + if !fresh.Enabled { + s.releaseWSLeader(channel.ID) + logger.Infof(context.Background(), + "[IM] Channel %s disabled while waiting for leadership; aborting leader takeover", channel.ID) + return + } + channel = fresh // use latest config (credentials/mode may have changed) logger.Infof(context.Background(), "[IM] Acquired leadership for channel %s, starting adapter", channel.ID) s.mu.RLock()