Skip to content

Commit b983b5e

Browse files
authored
backport: fix: move bufio reader creation out of for loop to fix telemetry unmarshal errors (#2789) (#2812)
* fix: move bufio reader creation out of for loop to fix telemetry unmarshal errors (#2789) * move bufio reader creation out of for loop if the bufio reader is created in the for loop we get unmarshaling errors * fix linter issue * add fixed ut * fix existing unit test flake due to closing pipe on error a previous fix ensured the socket closed on error, but this caused an existing ut to nondeterministically fail without the previous fix, the socket wouldn't have been closed on error * make read inline * make ut compatible with 1.4.x
1 parent 7002b34 commit b983b5e

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

telemetry/telemetrybuffer.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,14 @@ func (tb *TelemetryBuffer) StartServer() error {
123123
tb.connections = remove(tb.connections, index)
124124
}
125125
}()
126-
126+
reader := bufio.NewReader(conn)
127127
for {
128-
reportStr, err := read(conn)
129-
if err != nil {
128+
reportStr, readErr := reader.ReadBytes(Delimiter)
129+
if readErr != nil {
130130
return
131131
}
132+
reportStr = reportStr[:len(reportStr)-1]
133+
132134
var tmp map[string]interface{}
133135
err = json.Unmarshal(reportStr, &tmp)
134136
if err != nil {
@@ -195,16 +197,6 @@ func (tb *TelemetryBuffer) PushData(ctx context.Context) {
195197
}
196198
}
197199

198-
// read - read from the file descriptor
199-
func read(conn net.Conn) (b []byte, err error) {
200-
b, err = bufio.NewReader(conn).ReadBytes(Delimiter)
201-
if err == nil {
202-
b = b[:len(b)-1]
203-
}
204-
205-
return
206-
}
207-
208200
// Write - write to the file descriptor.
209201
func (tb *TelemetryBuffer) Write(b []byte) (c int, err error) {
210202
buf := make([]byte, len(b))

telemetry/telemetrybuffer_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,36 @@ func TestClientConnClose(t *testing.T) {
6767
tbClient.Close()
6868
}
6969

70+
func TestCloseOnWriteError(t *testing.T) {
71+
tbServer, closeTBServer := createTBServer(t)
72+
defer closeTBServer()
73+
74+
tbClient := NewTelemetryBuffer()
75+
err := tbClient.Connect()
76+
require.NoError(t, err)
77+
defer tbClient.Close()
78+
79+
data := []byte("{\"good\":1}")
80+
_, err = tbClient.Write(data)
81+
require.NoError(t, err)
82+
// need to wait for connection to populate in server
83+
time.Sleep(1 * time.Second)
84+
tbServer.mutex.Lock()
85+
conns := tbServer.connections
86+
tbServer.mutex.Unlock()
87+
require.Len(t, conns, 1)
88+
89+
// the connection should be automatically closed on failure
90+
badData := []byte("} malformed json }}}")
91+
_, err = tbClient.Write(badData)
92+
require.NoError(t, err)
93+
time.Sleep(1 * time.Second)
94+
tbServer.mutex.Lock()
95+
conns = tbServer.connections
96+
tbServer.mutex.Unlock()
97+
require.Empty(t, conns)
98+
}
99+
70100
func TestWrite(t *testing.T) {
71101
_, closeTBServer := createTBServer(t)
72102
defer closeTBServer()
@@ -84,8 +114,8 @@ func TestWrite(t *testing.T) {
84114
}{
85115
{
86116
name: "write",
87-
data: []byte("testdata"),
88-
want: len("testdata") + 1, // +1 due to Delimiter('\n)
117+
data: []byte("{\"testdata\":1}"),
118+
want: len("{\"testdata\":1}") + 1, // +1 due to Delimiter('\n)
89119
wantErr: false,
90120
},
91121
{

0 commit comments

Comments
 (0)