Skip to content

Commit b740a6f

Browse files
authored
fix: close the syncResult channel by any goroutine that receives the data (#3348)
fix: race condition in on-demand syncing Signed-off-by: Łukasz Jakimczuk <ljakimczuk@gmail.com>
1 parent f0404e7 commit b740a6f

1 file changed

Lines changed: 8 additions & 20 deletions

File tree

pkg/extensions/sync/on_demand.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ func (onDemand *BaseOnDemand) SyncImage(ctx context.Context, repo, reference str
5555

5656
syncResult, _ := val.(chan error)
5757

58-
err, ok := <-syncResult
59-
// if channel closed exit
60-
if !ok {
61-
return nil
62-
}
58+
err := <-syncResult
6359

6460
return err
6561
}
@@ -68,14 +64,10 @@ func (onDemand *BaseOnDemand) SyncImage(ctx context.Context, repo, reference str
6864
onDemand.requestStore.Store(req, syncResult)
6965

7066
defer onDemand.requestStore.Delete(req)
71-
defer close(syncResult)
7267

7368
go onDemand.syncImage(ctx, repo, reference, syncResult)
7469

75-
err, ok := <-syncResult
76-
if !ok {
77-
return nil
78-
}
70+
err := <-syncResult
7971

8072
return err
8173
}
@@ -95,11 +87,7 @@ func (onDemand *BaseOnDemand) SyncReferrers(ctx context.Context, repo string,
9587

9688
syncResult, _ := val.(chan error)
9789

98-
err, ok := <-syncResult
99-
// if channel closed exit
100-
if !ok {
101-
return nil
102-
}
90+
err := <-syncResult
10391

10492
return err
10593
}
@@ -108,21 +96,19 @@ func (onDemand *BaseOnDemand) SyncReferrers(ctx context.Context, repo string,
10896
onDemand.requestStore.Store(req, syncResult)
10997

11098
defer onDemand.requestStore.Delete(req)
111-
defer close(syncResult)
11299

113100
go onDemand.syncReferrers(ctx, repo, subjectDigestStr, referenceTypes, syncResult)
114101

115-
err, ok := <-syncResult
116-
if !ok {
117-
return nil
118-
}
102+
err := <-syncResult
119103

120104
return err
121105
}
122106

123107
func (onDemand *BaseOnDemand) syncReferrers(ctx context.Context, repo, subjectDigestStr string,
124108
referenceTypes []string, syncResult chan error,
125109
) {
110+
defer close(syncResult)
111+
126112
var err error
127113
for serviceID, service := range onDemand.services {
128114
err = service.SyncReferrers(ctx, repo, subjectDigestStr, referenceTypes)
@@ -176,6 +162,8 @@ func (onDemand *BaseOnDemand) syncReferrers(ctx context.Context, repo, subjectDi
176162
}
177163

178164
func (onDemand *BaseOnDemand) syncImage(ctx context.Context, repo, reference string, syncResult chan error) {
165+
defer close(syncResult)
166+
179167
var err error
180168

181169
for serviceID, service := range onDemand.services {

0 commit comments

Comments
 (0)