Skip to content

Commit

Permalink
lib/protoparser: handle unexpected EOF error when parsing lines in pr…
Browse files Browse the repository at this point in the history
…ometheus exposition format (#4851)

Previously only io.EOF was handled, and io.ErrUnexpectedEOF was ignored, but it may happen if the client interrupts the connection.

#4817
  • Loading branch information
dmitryk-dk authored and valyala committed Aug 28, 2023
1 parent 54a67d4 commit 9d39eec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/protoparser/common/lines_reader.go
Expand Up @@ -93,7 +93,7 @@ again:
}

func isEOFLikeError(err error) bool {
if errors.Is(err, io.EOF) {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
return true
}
s := err.Error()
Expand Down
27 changes: 27 additions & 0 deletions lib/protoparser/common/lines_reader_test.go
Expand Up @@ -2,6 +2,7 @@ package common

import (
"bytes"
"errors"
"fmt"
"io"
"reflect"
Expand All @@ -25,6 +26,20 @@ func TestReadLinesBlockFailure(t *testing.T) {
if _, _, err := ReadLinesBlock(fr, nil, nil); err == nil {
t.Fatalf("expecting non-nil error")
}

un := &unexpectedEOF{}
if _, _, err := ReadLinesBlock(un, nil, nil); err != nil {
if !errors.Is(err, io.EOF) {
t.Fatalf("get unexpected error, expecting io.EOF")
}
}

ef := eofErr{}
if _, _, err := ReadLinesBlock(ef, nil, nil); err != nil {
if !errors.Is(err, io.EOF) {
t.Fatalf("get unexpected error, expecting io.EOF")
}
}
}

// empty string
Expand All @@ -41,6 +56,18 @@ func (fr *failureReader) Read(p []byte) (int, error) {
return 0, fmt.Errorf("some error")
}

type unexpectedEOF struct{}

func (un unexpectedEOF) Read(p []byte) (int, error) {
return 0, io.ErrUnexpectedEOF
}

type eofErr struct{}

func (eo eofErr) Read(p []byte) (int, error) {
return 0, io.EOF
}

func TestReadLinesBlockMultiLinesSingleByteReader(t *testing.T) {
f := func(s string, linesExpected []string) {
t.Helper()
Expand Down

0 comments on commit 9d39eec

Please sign in to comment.