From c8d6e2103b2e16507aa5a3fda5ddfa76f63f27d4 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 6 Aug 2026 12:21:05 +0800 Subject: [PATCH] fix(im/wecom): scope connection close to its own generation The heartbeat loop and the detached callback handlers close the client's current connection, but their goroutines can outlive the connection that spawned them: heartbeatCancel only signals, and callback handlers run on a context deliberately detached from the connection. A late-firing goroutine therefore closed whatever connection the reconnect loop had just opened, leaving the client to drop messages until the next read timeout. Pass the owning connection into both callers and close through closeConnIf, which only acts while that connection is still active. This matches the yunzhijia and qqbot drivers, which already scope their heartbeat loops to a single connection. Also fold Stop() into closeConn so it no longer holds c.mu across Close. Stop() was the one caller where the delay actually spread: the IM service tears channels down while holding its channel-map lock, so a TLS close_notify write to a stalled peer stalled every other channel operation. The receive loop's deferred cleanup now closes only through closeConnIf, dropping a redundant second Close of the same connection, and connectAndRun re-checks the closed flag after publishing the connection so a Stop() racing the dial cannot leave a receive loop running until its read deadline expires. --- internal/im/wecom/longconn.go | 59 ++++++--- internal/im/wecom/longconn_close_test.go | 152 +++++++++++++++++++++++ 2 files changed, 195 insertions(+), 16 deletions(-) create mode 100644 internal/im/wecom/longconn_close_test.go diff --git a/internal/im/wecom/longconn.go b/internal/im/wecom/longconn.go index 7d1b2538..b545c90d 100644 --- a/internal/im/wecom/longconn.go +++ b/internal/im/wecom/longconn.go @@ -217,12 +217,7 @@ func (c *LongConnClient) Start(ctx context.Context) error { // Stop gracefully closes the connection. func (c *LongConnClient) Stop() { c.closed.Store(true) - c.mu.Lock() - defer c.mu.Unlock() - if c.conn != nil { - _ = c.conn.Close() - c.conn = nil - } + c.closeConn() } // SendReply sends a text reply through the WebSocket connection. @@ -380,8 +375,7 @@ func (c *LongConnClient) connectAndRun(ctx context.Context) error { c.mu.Unlock() defer func() { - c.closeConn() - _ = conn.Close() + c.closeConnIf(conn) // NOTE: streamBufs is intentionally NOT cleared on reconnect. // Active streams survive reconnections — the WeCom replace-based // protocol means the next UpdateStreamContent will resend the full @@ -389,6 +383,13 @@ func (c *LongConnClient) connectAndRun(ctx context.Context) error { // cleans up the buffer, so there is no memory leak. }() + // A Stop() that lands between the dial and the assignment above finds no + // connection to close, so re-check here to avoid running a receive loop + // that nobody will tear down until the read deadline expires. + if c.closed.Load() { + return fmt.Errorf("client stopped") + } + // Authenticate if err := c.authenticate(ctx); err != nil { return fmt.Errorf("authenticate: %w", err) @@ -399,7 +400,7 @@ func (c *LongConnClient) connectAndRun(ctx context.Context) error { // Start heartbeat heartbeatCtx, heartbeatCancel := context.WithCancel(ctx) defer heartbeatCancel() - go c.heartbeatLoop(heartbeatCtx) + go c.heartbeatLoop(heartbeatCtx, conn) // Message receive loop with read deadline. // The deadline is reset on every successful read; if no message arrives @@ -421,7 +422,7 @@ func (c *LongConnClient) connectAndRun(ctx context.Context) error { switch frame.Cmd { case cmdMsgCallback, cmdEventCallback: // Detach from connection ctx so in-flight messages survive reconnects. - go c.handleCallback(context.WithoutCancel(ctx), frame) + go c.handleCallback(context.WithoutCancel(ctx), conn, frame) default: // pong or other control frames — ignore } @@ -472,7 +473,7 @@ func (c *LongConnClient) authenticate(ctx context.Context) error { return nil } -func (c *LongConnClient) heartbeatLoop(ctx context.Context) { +func (c *LongConnClient) heartbeatLoop(ctx context.Context, conn *ws.Conn) { ticker := time.NewTicker(defaultHeartbeatInterval) defer ticker.Stop() @@ -488,14 +489,14 @@ func (c *LongConnClient) heartbeatLoop(ctx context.Context) { } if err := c.writeJSON(frame); err != nil { logger.Warnf(ctx, "[WeCom] Heartbeat failed: %v, closing connection to trigger reconnect", err) - c.closeConn() + c.closeConnIf(conn) return } } } } -func (c *LongConnClient) handleCallback(ctx context.Context, frame wsFrame) { +func (c *LongConnClient) handleCallback(ctx context.Context, conn *ws.Conn, frame wsFrame) { // Log raw message body for debugging logger.Debugf(ctx, "[WeCom] Raw callback body: %s", string(frame.Body)) @@ -514,7 +515,7 @@ func (c *LongConnClient) handleCallback(ctx context.Context, frame wsFrame) { switch msg.Event.EventType { case "disconnected_event": logger.Warnf(ctx, "[WeCom] Server sent disconnected_event, closing connection to trigger reconnect") - c.closeConn() + c.closeConnIf(conn) default: logger.Infof(ctx, "[WeCom] Ignoring event type: %s", msg.Event.EventType) } @@ -701,8 +702,14 @@ func (c *LongConnClient) convertMixedMessage(msg *botMessage, chatID string, cha return nil } -// closeConn forcibly closes the underlying WebSocket, which unblocks any -// pending ReadMessage call in the receive loop and triggers a reconnection. +// closeConn forcibly closes the active WebSocket, which unblocks any pending +// ReadMessage call in the receive loop and triggers a reconnection. +// +// c.mu is released before Close because closing a TLS connection writes a +// close_notify alert and can block for seconds on a stalled peer. Holding c.mu +// across that write stalls Stop(), and the IM service tears channels down while +// holding its own channel-map lock, so the delay spreads to every other channel +// operation in the process. func (c *LongConnClient) closeConn() { c.mu.Lock() conn := c.conn @@ -713,6 +720,26 @@ func (c *LongConnClient) closeConn() { } } +// closeConnIf closes conn only while it is still the active connection. +// Callers bound to a single connection generation — the heartbeat loop and the +// detached callback handlers — must use this instead of closeConn: their +// goroutines can outlive the connection that spawned them, and an unconditional +// close would tear down a healthy connection opened by a later reconnect. +func (c *LongConnClient) closeConnIf(conn *ws.Conn) { + if conn == nil { + return + } + c.mu.Lock() + active := c.conn == conn + if active { + c.conn = nil + } + c.mu.Unlock() + if active { + _ = conn.Close() + } +} + // stripAtMentionBasic removes a leading "@Name" prefix from group chat content. // Strategies: double-space split → heuristic (space + "/" or CJK) → first @word. // Used directly by the webhook adapter (stateless) and as the base for diff --git a/internal/im/wecom/longconn_close_test.go b/internal/im/wecom/longconn_close_test.go new file mode 100644 index 00000000..99ad4c33 --- /dev/null +++ b/internal/im/wecom/longconn_close_test.go @@ -0,0 +1,152 @@ +package wecom + +import ( + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" + + ws "github.com/gorilla/websocket" +) + +// newTestConn dials a throwaway WebSocket server and returns the client side. +// The server drains frames until the client goes away. +func newTestConn(t *testing.T) *ws.Conn { + t.Helper() + + upgrader := ws.Upgrader{} + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + serverConn, err := upgrader.Upgrade(w, r, nil) + if err != nil { + return + } + defer func() { _ = serverConn.Close() }() + for { + if _, _, err := serverConn.ReadMessage(); err != nil { + return + } + } + })) + t.Cleanup(server.Close) + + conn, _, err := ws.DefaultDialer.Dial("ws"+strings.TrimPrefix(server.URL, "http"), nil) + if err != nil { + t.Fatalf("dial test websocket: %v", err) + } + t.Cleanup(func() { _ = conn.Close() }) + return conn +} + +// connUsable reports whether conn still accepts writes. +func connUsable(t *testing.T, conn *ws.Conn) bool { + t.Helper() + _ = conn.SetWriteDeadline(time.Now().Add(5 * time.Second)) + return conn.WriteMessage(ws.TextMessage, []byte("probe")) == nil +} + +func TestCloseConnClosesAndClearsActiveConnection(t *testing.T) { + conn := newTestConn(t) + c := &LongConnClient{conn: conn} + + c.closeConn() + + if c.conn != nil { + t.Error("closeConn left c.conn set") + } + if connUsable(t, conn) { + t.Error("closeConn did not close the connection") + } +} + +func TestStopClosesActiveConnection(t *testing.T) { + conn := newTestConn(t) + c := &LongConnClient{conn: conn} + + c.Stop() + + if !c.closed.Load() { + t.Error("Stop did not mark the client closed") + } + if c.conn != nil { + t.Error("Stop left c.conn set") + } + if connUsable(t, conn) { + t.Error("Stop did not close the connection") + } +} + +func TestCloseConnIfClosesActiveConnection(t *testing.T) { + conn := newTestConn(t) + c := &LongConnClient{conn: conn} + + c.closeConnIf(conn) + + if c.conn != nil { + t.Error("closeConnIf left c.conn set") + } + if connUsable(t, conn) { + t.Error("closeConnIf did not close the active connection") + } +} + +// A heartbeat loop or detached callback handler from a previous connection +// generation must not tear down the connection opened by a later reconnect. +func TestCloseConnIfIgnoresSupersededConnection(t *testing.T) { + stale := newTestConn(t) + active := newTestConn(t) + c := &LongConnClient{conn: active} + + c.closeConnIf(stale) + + if c.conn != active { + t.Fatal("closeConnIf cleared the active connection on behalf of a stale caller") + } + if !connUsable(t, active) { + t.Fatal("closeConnIf closed the active connection on behalf of a stale caller") + } +} + +func TestCloseConnIfIgnoresNilConnection(t *testing.T) { + conn := newTestConn(t) + c := &LongConnClient{conn: conn} + + c.closeConnIf(nil) + + if c.conn != conn { + t.Error("closeConnIf(nil) cleared the active connection") + } + if !connUsable(t, conn) { + t.Error("closeConnIf(nil) closed the active connection") + } +} + +// Exercises the close and write paths together so the race detector can catch +// any regression that reintroduces unsynchronized access to c.conn. +func TestCloseConnRacesWithWriteJSON(t *testing.T) { + for i := 0; i < 20; i++ { + conn := newTestConn(t) + c := &LongConnClient{conn: conn} + + var wg sync.WaitGroup + wg.Add(3) + go func() { + defer wg.Done() + _ = c.writeJSON(wsFrame{Cmd: cmdPing}) + }() + go func() { + defer wg.Done() + c.closeConnIf(conn) + }() + go func() { + defer wg.Done() + c.closeConn() + }() + wg.Wait() + + if c.conn != nil { + t.Fatal("connection pointer still set after concurrent closes") + } + } +}