Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions pkg/mediorum/server/replicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,33 @@ func (ss *MediorumServer) replicateFileToHost(ctx context.Context, peer string,

r, w := io.Pipe()
m := multipart.NewWriter(w)
errChan := make(chan error)
errChan := make(chan error, 1)

go func() {
defer w.Close()
defer m.Close()
part, err := m.CreateFormFile(filesFormFieldName, fileName)
if err != nil {
_ = w.CloseWithError(err)
errChan <- err
return
}
if _, err = io.Copy(part, file); err != nil {
_ = w.CloseWithError(err)
errChan <- err
return
}
close(errChan)
if err := m.Close(); err != nil {
_ = w.CloseWithError(err)
errChan <- err
return
}
errChan <- w.Close()
}()

closeBodyWithError := func(err error) error {
_ = r.CloseWithError(err)
return err
}

req, err := signature.SignedPost(
ctx,
peer+"/internal/blobs",
Expand All @@ -203,7 +213,7 @@ func (ss *MediorumServer) replicateFileToHost(ctx context.Context, peer string,
ss.Config.Self.Host,
)
if err != nil {
return err
return closeBodyWithError(err)
}
if len(placementHosts) > 0 {
req.Header.Set(placementHostsHeader, encodePlacementHosts(placementHosts))
Expand All @@ -212,12 +222,12 @@ func (ss *MediorumServer) replicateFileToHost(ctx context.Context, peer string,
// send it
resp, err := ss.peerHTTPClient.Do(req)
if err != nil {
return err
return closeBodyWithError(err)
}

defer resp.Body.Close()
if resp.StatusCode != 200 {
return errors.New(resp.Status)
return closeBodyWithError(errors.New(resp.Status))
}

return <-errChan
Expand Down
66 changes: 66 additions & 0 deletions pkg/mediorum/server/replicate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package server

import (
"bytes"
"context"
"io"
"net/http"
"runtime"
"runtime/pprof"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

type roundTripFunc func(*http.Request) (*http.Response, error)

func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}

func countReplicateFileToHostGoroutines() int {
var stacks bytes.Buffer
_ = pprof.Lookup("goroutine").WriteTo(&stacks, 2)
return strings.Count(stacks.String(), "replicateFileToHost.func1")
}

func TestReplicateFileToHostClosesPipeOnEarlyHTTPError(t *testing.T) {
ss := testNetwork[0]
originalClient := ss.peerHTTPClient
ss.peerHTTPClient = &http.Client{
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusInternalServerError,
Status: "500 Internal Server Error",
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader("")),
Request: req,
}, nil
}),
}
t.Cleanup(func() {
ss.peerHTTPClient = originalClient
})

before := countReplicateFileToHostGoroutines()
err := ss.replicateFileToHost(
context.Background(),
"http://unread-peer.test",
"leak-regression-cid",
strings.NewReader(strings.Repeat("x", 1024*1024)),
nil,
)
assert.Error(t, err)

deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) {
if countReplicateFileToHostGoroutines() <= before {
return
}
runtime.Gosched()
time.Sleep(10 * time.Millisecond)
}
assert.LessOrEqual(t, countReplicateFileToHostGoroutines(), before)
}
Loading