Skip to content

Commit 15a186a

Browse files
authored
Merge pull request kubernetes#129792 from likakuli/fix-errshortbuffer
fix: Fix the issue of relist caused by client-side timeout
2 parents 9e55587 + 38a21e0 commit 15a186a

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

staging/src/k8s.io/apimachinery/pkg/util/framer/framer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ func (r *lengthDelimitedFrameReader) Read(data []byte) (int, error) {
9191
}
9292
n, err := io.ReadAtLeast(r.r, data[:max], int(max))
9393
r.remaining -= n
94-
if err == io.ErrShortBuffer || r.remaining > 0 {
95-
return n, io.ErrShortBuffer
96-
}
9794
if err != nil {
9895
return n, err
9996
}
97+
if r.remaining > 0 {
98+
return n, io.ErrShortBuffer
99+
}
100100
if n != expect {
101101
return n, io.ErrUnexpectedEOF
102102
}

staging/src/k8s.io/apimachinery/pkg/util/framer/framer_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ import (
2020
"bytes"
2121
"errors"
2222
"io"
23+
"net/http"
24+
"net/http/httptest"
2325
"testing"
26+
"time"
27+
28+
netutil "k8s.io/apimachinery/pkg/util/net"
2429
)
2530

2631
func TestRead(t *testing.T) {
@@ -98,6 +103,7 @@ func TestReadLarge(t *testing.T) {
98103
t.Fatalf("unexpected: %v %d", err, n)
99104
}
100105
}
106+
101107
func TestReadInvalidFrame(t *testing.T) {
102108
data := []byte{
103109
0x00, 0x00, 0x00, 0x04,
@@ -120,6 +126,46 @@ func TestReadInvalidFrame(t *testing.T) {
120126
}
121127
}
122128

129+
func TestReadClientTimeout(t *testing.T) {
130+
header := []byte{
131+
0x00, 0x00, 0x00, 0x04,
132+
}
133+
data := []byte{
134+
0x01, 0x02, 0x03, 0x04,
135+
0x00, 0x00, 0x00, 0x03,
136+
0x05, 0x06, 0x07,
137+
0x00, 0x00, 0x00, 0x00,
138+
0x00, 0x00, 0x00, 0x01,
139+
0x08,
140+
}
141+
142+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
143+
w.WriteHeader(http.StatusOK)
144+
_, _ = w.Write(header)
145+
if flusher, ok := w.(http.Flusher); ok {
146+
flusher.Flush()
147+
}
148+
time.Sleep(1 * time.Second)
149+
_, _ = w.Write(data)
150+
}))
151+
defer server.Close()
152+
153+
client := &http.Client{
154+
Timeout: 500 * time.Millisecond,
155+
}
156+
157+
resp, err := client.Get(server.URL)
158+
if err != nil {
159+
t.Fatalf("unexpected: %v", err)
160+
}
161+
162+
r := NewLengthDelimitedFrameReader(resp.Body)
163+
buf := make([]byte, 1)
164+
if n, err := r.Read(buf); err == nil || !netutil.IsTimeout(err) {
165+
t.Fatalf("unexpected: %v %d", err, n)
166+
}
167+
}
168+
123169
func TestJSONFrameReader(t *testing.T) {
124170
b := bytes.NewBufferString("{\"test\":true}\n1\n[\"a\"]")
125171
r := NewJSONFramedReader(io.NopCloser(b))

0 commit comments

Comments
 (0)