Skip to content

Commit a50e2a9

Browse files
authored
fix: race condition when waiting for sharedError (#474)
1 parent 871cf24 commit a50e2a9

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

go/porcelain/deploy.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ func (n *Netlify) uploadFiles(ctx context.Context, d *models.Deploy, files *depl
405405
sharedErr := &uploadError{err: nil, mutex: &sync.Mutex{}}
406406
sem := make(chan int, n.uploadLimit)
407407
wg := &sync.WaitGroup{}
408-
defer wg.Wait()
409408

410409
var required []string
411410
switch t {
@@ -447,6 +446,8 @@ func (n *Netlify) uploadFiles(ctx context.Context, d *models.Deploy, files *depl
447446
}
448447
}
449448

449+
wg.Wait()
450+
450451
return sharedErr.err
451452
}
452453

go/porcelain/deploy_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,36 @@ func TestUploadFiles_Cancelation(t *testing.T) {
291291
require.ErrorIs(t, err, gocontext.Canceled)
292292
}
293293

294+
func TestUploadFiles_Errors(t *testing.T) {
295+
ctx := gocontext.Background()
296+
297+
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
298+
rw.WriteHeader(http.StatusInternalServerError)
299+
}))
300+
defer server.Close()
301+
302+
hu, _ := url.Parse(server.URL)
303+
tr := apiClient.NewWithClient(hu.Host, "/api/v1", []string{"http"}, http.DefaultClient)
304+
client := NewRetryable(tr, strfmt.Default, 1)
305+
client.uploadLimit = 1
306+
ctx = context.WithAuthInfo(ctx, apiClient.BearerToken("token"))
307+
308+
// Create some files to deploy
309+
dir, err := ioutil.TempDir("", "deploy")
310+
require.NoError(t, err)
311+
defer os.RemoveAll(dir)
312+
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "foo.html"), []byte("Hello"), 0644))
313+
314+
files, err := walk(dir, nil, false, false)
315+
require.NoError(t, err)
316+
d := &models.Deploy{}
317+
for _, bundle := range files.Files {
318+
d.Required = append(d.Required, bundle.Sum)
319+
}
320+
err = client.uploadFiles(ctx, d, files, nil, fileUpload, time.Minute)
321+
require.Equal(t, err.Error(), "[PUT /deploys/{deploy_id}/files/{path}][500] uploadDeployFile default &{Code:0 Message:}")
322+
}
323+
294324
func TestUploadFiles_SkipEqualFiles(t *testing.T) {
295325
ctx := gocontext.Background()
296326

0 commit comments

Comments
 (0)