1
mirror of https://github.com/XTLS/Xray-core.git synced 2025-12-12 04:34:41 +04:00

prevent close of closed

This commit is contained in:
Fangliding
2025-08-25 13:55:17 +08:00
parent ecc2f73108
commit 73baf47358

View File

@@ -5,6 +5,7 @@ import (
go_errors "errors"
"io"
"sync"
"sync/atomic"
"time"
"github.com/xtls/xray-core/common"
@@ -371,6 +372,7 @@ type outboundConn struct {
closeOnce sync.Once
dialOnce sync.Once
closed atomic.Bool
conn net.Conn
connReady chan struct{}
@@ -381,6 +383,9 @@ func (c *outboundConn) dial() error {
if err != nil {
return err
}
if c.closed.Load() {
return errors.New("connection closed during dial")
}
c.conn = conn
c.connReady <- struct{}{}
return nil
@@ -423,6 +428,7 @@ func (c *outboundConn) Read(b []byte) (int, error) {
func (c *outboundConn) Close() error {
c.closeOnce.Do(func() {
c.access.Lock()
c.closed.Store(true)
close(c.connReady)
if c.conn != nil {
c.conn.Close()