fix: close request bodies when HTTP client rejects before transport

This commit is contained in:
2026-09-23 13:40:31 +08:00
parent fe66221a9c
commit b9259e628e
2 changed files with 36 additions and 3 deletions

View File

@@ -15,6 +15,31 @@ import (
"git.yuntuo.hk/k2safe/go-omni/logx"
)
type trackedBody struct {
io.Reader
closed bool
}
func (b *trackedBody) Close() error { b.closed = true; return nil }
func TestRejectedRequestClosesBody(t *testing.T) {
for _, closed := range []bool{false, true} {
client, _ := New(Config{DisableLogging: true})
body := &trackedBody{Reader: strings.NewReader("body")}
request, _ := http.NewRequest("POST", "http://unused.invalid", body)
if closed {
client.Close()
} else {
request.URL = nil
}
_, err := client.Do(request)
client.Close()
if err == nil || !body.closed {
t.Fatalf("error=%v bodyClosed=%v", err, body.closed)
}
}
}
func TestRetryCorrelationPoolAndWriteSafety(t *testing.T) {
var calls, connections atomic.Int32
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {