Skip to content

Commit 7870117

Browse files
committed
syz-ci/manager.go: use context to cancel file transfer instead of pipe CloseWithError
1 parent 2ba116b commit 7870117

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

syz-ci/manager.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/google/syzkaller/prog"
3838
_ "github.com/google/syzkaller/sys"
3939
"github.com/google/syzkaller/sys/targets"
40+
"golang.org/x/sync/errgroup"
4041
)
4142

4243
// This is especially slightly longer than syzkaller rebuild period.
@@ -832,15 +833,19 @@ func (mgr *Manager) uploadBuildAssets(buildInfo *dashapi.Build, assetFolder stri
832833
return ret, nil
833834
}
834835

835-
func (mgr *Manager) httpGET(path string) (resp *http.Response, err error) {
836+
func (mgr *Manager) httpGET(ctx context.Context, path string) (resp *http.Response, err error) {
836837
addr := mgr.managercfg.HTTP
837838
if addr != "" && addr[0] == ':' {
838839
addr = "127.0.0.1" + addr // in case addr is ":port"
839840
}
840841
client := &http.Client{
841842
Timeout: time.Hour,
842843
}
843-
return client.Get(fmt.Sprintf("http://%s%s", addr, path))
844+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://%s%s", addr, path), nil)
845+
if err != nil {
846+
return nil, err
847+
}
848+
return client.Do(req)
844849
}
845850

846851
func (mgr *Manager) uploadCoverReport() error {
@@ -860,7 +865,7 @@ func (mgr *Manager) uploadCoverReport() error {
860865
}
861866
defer buildSem.Signal()
862867

863-
resp, err := mgr.httpGET("/cover")
868+
resp, err := mgr.httpGET(context.Background(), "/cover")
864869
if err != nil {
865870
return fmt.Errorf("failed to get report: %w", err)
866871
}
@@ -896,7 +901,8 @@ func (mgr *Manager) uploadCoverJSONLToGCS(ctx context.Context, mgrSrc, gcsDest s
896901
}
897902
defer buildSem.Signal()
898903

899-
resp, err := mgr.httpGET(mgrSrc)
904+
eg, egCtx := errgroup.WithContext(ctx)
905+
resp, err := mgr.httpGET(egCtx, mgrSrc)
900906
if err != nil {
901907
return fmt.Errorf("failed to httpGet %s: %w", mgrSrc, err)
902908
}
@@ -909,10 +915,8 @@ func (mgr *Manager) uploadCoverJSONLToGCS(ctx context.Context, mgrSrc, gcsDest s
909915
}
910916

911917
pr, pw := io.Pipe()
912-
defer pr.Close()
913-
go func() {
914-
var closeError error
915-
defer func() { pw.CloseWithError(closeError) }()
918+
eg.Go(func() error {
919+
defer pw.Close()
916920
var w io.Writer
917921
w = pw
918922
if compress {
@@ -923,19 +927,23 @@ func (mgr *Manager) uploadCoverJSONLToGCS(ctx context.Context, mgrSrc, gcsDest s
923927
decoder := json.NewDecoder(resp.Body)
924928
for decoder.More() {
925929
if err := f(w, decoder); err != nil {
926-
closeError = fmt.Errorf("callback: %w", err)
927-
return
930+
return fmt.Errorf("callback: %w", err)
928931
}
929932
}
930-
}()
931-
fileName := fmt.Sprintf("%s/%s-%s-%d-%d.jsonl",
932-
mgr.mgrcfg.DashboardClient,
933-
mgr.name, curTime.Format(time.DateOnly),
934-
curTime.Hour(), curTime.Minute())
935-
if err := uploadFile(ctx, gcsDest, fileName, pr, publish); err != nil {
936-
return fmt.Errorf("failed to uploadFileGCS(): %w", err)
937-
}
938-
return nil
933+
return nil
934+
})
935+
eg.Go(func() error {
936+
defer pr.Close()
937+
fileName := fmt.Sprintf("%s/%s-%s-%d-%d.jsonl",
938+
mgr.mgrcfg.DashboardClient,
939+
mgr.name, curTime.Format(time.DateOnly),
940+
curTime.Hour(), curTime.Minute())
941+
if err := uploadFile(egCtx, gcsDest, fileName, pr, publish); err != nil {
942+
return fmt.Errorf("failed to uploadFileGCS(): %w", err)
943+
}
944+
return nil
945+
})
946+
return eg.Wait()
939947
}
940948

941949
func (mgr *Manager) uploadCoverStat(fuzzingMinutes int) error {
@@ -1043,13 +1051,13 @@ func uploadFile(ctx context.Context, dstPath, name string, file io.Reader, publi
10431051
log.Logf(0, "uploading %v to %v", name, URLStr)
10441052
if strings.HasPrefix(URLStr, "http://") ||
10451053
strings.HasPrefix(URLStr, "https://") {
1046-
return uploadFileHTTPPut(URLStr, file)
1054+
return uploadFileHTTPPut(ctx, URLStr, file)
10471055
}
10481056
return gcs.UploadFile(ctx, file, URLStr, publish)
10491057
}
10501058

1051-
func uploadFileHTTPPut(URL string, file io.Reader) error {
1052-
req, err := http.NewRequest(http.MethodPut, URL, file)
1059+
func uploadFileHTTPPut(ctx context.Context, URL string, file io.Reader) error {
1060+
req, err := http.NewRequestWithContext(ctx, http.MethodPut, URL, file)
10531061
if err != nil {
10541062
return fmt.Errorf("failed to create HTTP PUT request: %w", err)
10551063
}

syz-ci/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestUploadCoverJSONLToGCS(t *testing.T) {
166166
inputJSONL: "{",
167167
inputTime: time.Time{},
168168
wantGCSFileName: "test-bucket/test-namespace/mgr-name-0001-01-01-0-0.jsonl",
169-
wantError: "failed to uploadFileGCS(): io.Copy: callback: cover.ProgramCoverage: unexpected EOF",
169+
wantError: "callback: cover.ProgramCoverage: unexpected EOF",
170170
},
171171
}
172172

0 commit comments

Comments
 (0)