Skip to content

Commit 268b4a8

Browse files
findleyrgopherbot
authored andcommitted
internal/upload: fix upload locking
The upload locking logic left a brief race that could lead to duplicate uploads, as the existence of an uploaded report was checked before locking, not after. Fix this. As described in golang/go#67737, more races exist that could lead to broken or partial uploads, but this should prevent overcounting uploads, as was originally intended. This closes golang/go#65970, as it is the last thing I feel comfortable doing relative to the upload process. More refactoring will have to wait until 1.24. For golang/go#67737 Fixes golang/go#65970 Change-Id: Iadd58402bbe3fb32b4daf479d8d800eaef47c370 Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/598036 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent 1f628dd commit 268b4a8

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

internal/upload/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ func TestRun_Concurrent(t *testing.T) {
586586
for i, upload := range uploads {
587587
var got telemetry.Report
588588
if err := json.Unmarshal(upload, &got); err != nil {
589-
t.Fatal(err)
589+
t.Fatalf("error unmarshalling uploaded report: %v\ncontents:%s", err, upload)
590590
}
591591
if got, want := len(got.Programs), 1; got != want {
592592
t.Fatalf("got %d programs in upload #%d, want %d", got, i, want)

internal/upload/upload.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ func (u *uploader) uploadReportContents(fname string, buf []byte) bool {
6565
fdate = fdate[len(fdate)-len("2006-01-02"):]
6666

6767
newname := filepath.Join(u.dir.UploadDir(), fdate+".json")
68-
if _, err := os.Stat(newname); err == nil {
69-
// Another process uploaded but failed to clean up (or hasn't yet cleaned
70-
// up). Ensure that cleanup occurs.
71-
_ = os.Remove(fname)
72-
return false
73-
}
7468

7569
// Lock the upload, to prevent duplicate uploads.
7670
{
@@ -84,6 +78,14 @@ func (u *uploader) uploadReportContents(fname string, buf []byte) bool {
8478
defer os.Remove(lockname)
8579
}
8680

81+
if _, err := os.Stat(newname); err == nil {
82+
// Another process uploaded but failed to clean up (or hasn't yet cleaned
83+
// up). Ensure that cleanup occurs.
84+
u.logger.Printf("After acquire: report already uploaded")
85+
_ = os.Remove(fname)
86+
return false
87+
}
88+
8789
endpoint := u.uploadServerURL + "/" + fdate
8890
b := bytes.NewReader(buf)
8991
resp, err := http.Post(endpoint, "application/json", b)

0 commit comments

Comments
 (0)