mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-09-15 22:10:26 +00:00
XHTTP client: Fix a race condition and a data race (#6665)
https://github.com/XTLS/Xray-core/pull/6665#issuecomment-5429028477 --------- Co-authored-by: 风扇滑翔翼 <Fangliding.fshxy@outlook.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptrace"
|
"net/http/httptrace"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/apernet/quic-go/http3"
|
"github.com/apernet/quic-go/http3"
|
||||||
"github.com/xtls/xray-core/common"
|
"github.com/xtls/xray-core/common"
|
||||||
@@ -32,7 +33,7 @@ type DialerClient interface {
|
|||||||
type DefaultDialerClient struct {
|
type DefaultDialerClient struct {
|
||||||
transportConfig *Config
|
transportConfig *Config
|
||||||
client *http.Client
|
client *http.Client
|
||||||
closed bool
|
closed atomic.Bool
|
||||||
httpVersion string
|
httpVersion string
|
||||||
// pool of net.Conn, created using dialUploadConn
|
// pool of net.Conn, created using dialUploadConn
|
||||||
uploadRawPool *sync.Pool
|
uploadRawPool *sync.Pool
|
||||||
@@ -40,7 +41,7 @@ type DefaultDialerClient struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *DefaultDialerClient) IsClosed() bool {
|
func (c *DefaultDialerClient) IsClosed() bool {
|
||||||
return c.closed
|
return c.closed.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DefaultDialerClient) OpenStream(ctx context.Context, url string, sessionId string, body io.Reader, uploadOnly bool) (wrc io.ReadCloser, remoteAddr, localAddr net.Addr, err error) {
|
func (c *DefaultDialerClient) OpenStream(ctx context.Context, url string, sessionId string, body io.Reader, uploadOnly bool) (wrc io.ReadCloser, remoteAddr, localAddr net.Addr, err error) {
|
||||||
@@ -72,7 +73,7 @@ func (c *DefaultDialerClient) OpenStream(ctx context.Context, url string, sessio
|
|||||||
resp, err := c.client.Do(req)
|
resp, err := c.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !uploadOnly { // stream-down is enough
|
if !uploadOnly { // stream-down is enough
|
||||||
c.closed = true
|
c.closed.Store(true)
|
||||||
errors.LogInfoInner(ctx, err, "failed to "+method+" "+url)
|
errors.LogInfoInner(ctx, err, "failed to "+method+" "+url)
|
||||||
}
|
}
|
||||||
gotConn.Close()
|
gotConn.Close()
|
||||||
@@ -108,7 +109,7 @@ func (c *DefaultDialerClient) PostPacket(ctx context.Context, url string, sessio
|
|||||||
if c.httpVersion != "1.1" {
|
if c.httpVersion != "1.1" {
|
||||||
resp, err := c.client.Do(req)
|
resp, err := c.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.closed = true
|
c.closed.Store(true)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,7 +149,7 @@ func (c *DefaultDialerClient) PostPacket(ctx context.Context, url string, sessio
|
|||||||
if h1UploadConn.UnreadedResponsesCount > 0 {
|
if h1UploadConn.UnreadedResponsesCount > 0 {
|
||||||
resp, err := http.ReadResponse(h1UploadConn.RespBufReader, req)
|
resp, err := http.ReadResponse(h1UploadConn.RespBufReader, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.closed = true
|
c.closed.Store(true)
|
||||||
return fmt.Errorf("error while reading response: %s", err.Error())
|
return fmt.Errorf("error while reading response: %s", err.Error())
|
||||||
}
|
}
|
||||||
io.Copy(io.Discard, resp.Body)
|
io.Copy(io.Discard, resp.Body)
|
||||||
|
|||||||
@@ -595,11 +595,12 @@ func (w uploadWriter) Write(b []byte) (int, error) {
|
|||||||
|
|
||||||
var writed int
|
var writed int
|
||||||
for _, buff := range buffer.MultiBuffer {
|
for _, buff := range buffer.MultiBuffer {
|
||||||
|
n := int(buff.Len())
|
||||||
err := w.WriteMultiBuffer(buf.MultiBuffer{buff})
|
err := w.WriteMultiBuffer(buf.MultiBuffer{buff})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return writed, err
|
return writed, err
|
||||||
}
|
}
|
||||||
writed += int(buff.Len())
|
writed += n
|
||||||
}
|
}
|
||||||
return writed, nil
|
return writed, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user