-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read response when client closes connection #1232 #1233
Merged
erikdubbelboer
merged 8 commits into
valyala:master
from
ArminBTVS:forcefully-close-conn-resp
Mar 14, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
135c2c4
Read response when client closes connection #1232
becheran de67094
Fix edge case were client responds with invalid header
becheran 59d2fcc
Follow linter suggestions for tests
becheran 5c93662
Changes after review
becheran 3b1bcc7
Reafactor error check after review
becheran 4e9614f
Handle connection reset on windows
becheran e4d02bd
Remove format string from test where not needed
becheran 8d7b611
Run connection reset tests not on Windows
becheran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package fasthttp | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// See issue #1232 | ||
func TestRstConnResponseWhileSending(t *testing.T) { | ||
const expectedStatus = http.StatusTeapot | ||
const payload = "payload" | ||
|
||
srv, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer srv.Close() | ||
|
||
go func() { | ||
for { | ||
conn, err := srv.Accept() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Read at least one byte of the header | ||
// Otherwise we would have an unsolicited response | ||
_, err = ioutil.ReadAll(io.LimitReader(conn, 1)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// Respond | ||
_, err = conn.Write([]byte("HTTP/1.1 418 Teapot\r\n\r\n")) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// Forcefully close connection | ||
err = conn.(*net.TCPConn).SetLinger(0) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
conn.Close() | ||
} | ||
}() | ||
|
||
svrUrl := "http://" + srv.Addr().String() | ||
client := HostClient{Addr: srv.Addr().String()} | ||
|
||
for i := 0; i < 100; i++ { | ||
req := AcquireRequest() | ||
defer ReleaseRequest(req) | ||
resp := AcquireResponse() | ||
defer ReleaseResponse(resp) | ||
|
||
req.Header.SetMethod("POST") | ||
req.SetBodyStream(strings.NewReader(payload), len(payload)) | ||
req.SetRequestURI(svrUrl) | ||
|
||
err = client.Do(req, resp) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if expectedStatus != resp.StatusCode() { | ||
t.Fatalf("Expected %d status code, but got %d", expectedStatus, resp.StatusCode()) | ||
} | ||
} | ||
} | ||
|
||
// See issue #1232 | ||
func TestRstConnClosedWithoutResponse(t *testing.T) { | ||
const payload = "payload" | ||
|
||
srv, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer srv.Close() | ||
|
||
go func() { | ||
for { | ||
conn, err := srv.Accept() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Read at least one byte of the header | ||
// Otherwise we would have an unsolicited response | ||
_, err = ioutil.ReadAll(io.LimitReader(conn, 1)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// Respond with incomplete header | ||
_, err = conn.Write([]byte("Http")) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// Forcefully close connection | ||
err = conn.(*net.TCPConn).SetLinger(0) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
conn.Close() | ||
} | ||
}() | ||
|
||
svrUrl := "http://" + srv.Addr().String() | ||
client := HostClient{Addr: srv.Addr().String()} | ||
|
||
for i := 0; i < 100; i++ { | ||
req := AcquireRequest() | ||
defer ReleaseRequest(req) | ||
resp := AcquireResponse() | ||
defer ReleaseResponse(resp) | ||
|
||
req.Header.SetMethod("POST") | ||
req.SetBodyStream(strings.NewReader(payload), len(payload)) | ||
req.SetRequestURI(svrUrl) | ||
|
||
err = client.Do(req, resp) | ||
|
||
if !isConnectionReset(err) { | ||
t.Fatal("Expected connection reset error") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package fasthttp | ||
|
||
import ( | ||
"errors" | ||
"syscall" | ||
) | ||
|
||
func isConnectionReset(err error) bool { | ||
return errors.Is(err, syscall.ECONNRESET) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package fasthttp | ||
|
||
import ( | ||
"errors" | ||
"syscall" | ||
) | ||
|
||
func isConnectionReset(err error) bool { | ||
return errors.Is(err, syscall.WSAECONNRESET) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now when a connection is closed with
ECONNRESET
it will still be released to the pool here (as there was no error before) and will try to be used again.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ArminBTVS, thanks for your pull request
needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moredure true. Did not see this case. I did make the change as @moredure suggested. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack