From 86415c1e2a33cfd2723d5624a4023c1a30f5fb75 Mon Sep 17 00:00:00 2001 From: Eken Chan Date: Mon, 24 Jun 2024 18:21:51 +0800 Subject: [PATCH 1/3] blocking splithttp read --- transport/internet/splithttp/upload_queue.go | 26 ++++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/transport/internet/splithttp/upload_queue.go b/transport/internet/splithttp/upload_queue.go index 8b2c8f36329e..a173b0fba323 100644 --- a/transport/internet/splithttp/upload_queue.go +++ b/transport/internet/splithttp/upload_queue.go @@ -51,9 +51,15 @@ func (h *UploadQueue) Read(b []byte) (int, error) { return 0, io.EOF } - needMorePackets := false + if len(h.heap) == 0 { + packet, more := <-h.pushedPackets + if !more { + return 0, io.EOF + } + heap.Push(&h.heap, packet) + } - if len(h.heap) > 0 { + for len(h.heap) > 0 { packet := heap.Pop(&h.heap).(Packet) n := 0 @@ -81,18 +87,12 @@ func (h *UploadQueue) Read(b []byte) (int, error) { return 0, newError("packet queue is too large") } heap.Push(&h.heap, packet) - needMorePackets = true - } - } else { - needMorePackets = true - } - - if needMorePackets { - packet, more := <-h.pushedPackets - if !more { - return 0, io.EOF + packet2, more := <-h.pushedPackets + if !more { + return 0, io.EOF + } + heap.Push(&h.heap, packet2) } - heap.Push(&h.heap, packet) } return 0, nil From b90c9c28ce0ff78d7c76a1d2a274a54ebdc05f0f Mon Sep 17 00:00:00 2001 From: mmmray <142015632+mmmray@users.noreply.github.com> Date: Mon, 24 Jun 2024 13:14:03 +0200 Subject: [PATCH 2/3] Add testcase --- .../internet/splithttp/upload_queue_test.go | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 transport/internet/splithttp/upload_queue_test.go diff --git a/transport/internet/splithttp/upload_queue_test.go b/transport/internet/splithttp/upload_queue_test.go new file mode 100644 index 000000000000..8185cd8f9ea1 --- /dev/null +++ b/transport/internet/splithttp/upload_queue_test.go @@ -0,0 +1,22 @@ +package splithttp_test + +import ( + "testing" + + "github.com/xtls/xray-core/common" + . "github.com/xtls/xray-core/transport/internet/splithttp" +) + +func Test_regression_readzero(t *testing.T) { + q := NewUploadQueue(10) + q.Push(Packet{ + Payload: []byte("x"), + Seq: 0, + }) + buf := make([]byte, 20) + n, err := q.Read(buf) + common.Must(err) + if n != 1 { + t.Error("n=", n) + } +} From 379e417a0af8248dfa39e48eaae4bc6f1357aa1e Mon Sep 17 00:00:00 2001 From: Eken Chan Date: Mon, 24 Jun 2024 21:20:00 +0800 Subject: [PATCH 3/3] simplify conditions --- transport/internet/splithttp/upload_queue.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transport/internet/splithttp/upload_queue.go b/transport/internet/splithttp/upload_queue.go index a173b0fba323..62c6941144e6 100644 --- a/transport/internet/splithttp/upload_queue.go +++ b/transport/internet/splithttp/upload_queue.go @@ -47,7 +47,7 @@ func (h *UploadQueue) Close() error { } func (h *UploadQueue) Read(b []byte) (int, error) { - if h.closed && len(h.heap) == 0 && len(h.pushedPackets) == 0 { + if h.closed { return 0, io.EOF }