From 2be04f3fb9048d1d672cfc5b27e8b6873e025ff6 Mon Sep 17 00:00:00 2001 From: xfy Date: Fri, 5 Jun 2026 11:35:20 +0800 Subject: [PATCH] fix(lua): add mutex protection for TCPSocket.currentOp in async methods --- internal/lua/api_socket_tcp.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/lua/api_socket_tcp.go b/internal/lua/api_socket_tcp.go index fa77071..27bd603 100644 --- a/internal/lua/api_socket_tcp.go +++ b/internal/lua/api_socket_tcp.go @@ -198,7 +198,10 @@ func (s *TCPSocket) ConnectAsync(_ *glua.LState, host string, port int) (*Socket if err != nil { return nil, err } - return s.currentOp, nil + s.mu.RLock() + op := s.currentOp + s.mu.RUnlock() + return op, nil } // Send 发送数据 @@ -245,13 +248,17 @@ func (s *TCPSocket) SendAsync(data []byte) (*SocketOperation, error) { // 开始操作 op := s.manager.StartOperation(s, OpSend, s.sendTimeout) + s.mu.Lock() s.currentOp = op + s.mu.Unlock() s.setState(SocketStateSending) // 在 goroutine 中执行发送 go func() { defer func() { + s.mu.Lock() s.currentOp = nil + s.mu.Unlock() s.setState(SocketStateConnected) }() @@ -330,13 +337,17 @@ func (s *TCPSocket) ReceiveAsync(size int) (*SocketOperation, error) { // 开始操作 op := s.manager.StartOperation(s, OpReceive, s.readTimeout) + s.mu.Lock() s.currentOp = op + s.mu.Unlock() s.setState(SocketStateReceiving) // 在 goroutine 中执行接收 go func() { defer func() { + s.mu.Lock() s.currentOp = nil + s.mu.Unlock() s.setState(SocketStateConnected) }()