Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(im/feishu): actually close WebSocket long connection when channel is stopped
养乐多baada32cb6db
diff --git a/go.mod b/go.modindex 382ac3c60..efb749fdf 100644--- a/go.mod+++ b/go.mod@@ -31,7 +31,7 @@ require (github.com/jackc/pgx/v5 v5.9.2github.com/joho/godotenv v1.5.1github.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.7github.com/longbridgeapp/opencc v0.3.13github.com/mark3labs/mcp-go v0.52.0github.com/milvus-io/milvus/client/v2 v2.6.4diff --git a/go.sum b/go.sumindex 460809795..07ef63e79 100644--- a/go.sum+++ b/go.sum@@ -2125,6 +2125,8 @@ github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0github.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.goindex 8e4db0e5f..24bee0bd9 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, nildefault:return nil, nil, fmt.Errorf("unknown Feishu mode: %s", mode)diff --git a/internal/im/feishu/longconn.go b/internal/im/feishu/longconn.goindex 1f70bf663..6da1810a5 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.goindex 0282804d1..92cf2406d 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()