Skip to content

Commit 2827620

Browse files
committed
fix(publish): pre-register published repo key before task submission
apiPublishRepoOrSnapshot appended published.Key() to resources inside the task closure, after maybeRunTaskInBackground had already been called. The task's locked-resource set is fixed at submission time, so that append had no effect — the published repo key was never registered as a resource. Two concurrent POST /api/publish/{prefix} requests for the same prefix/distribution therefore did not conflict in the task queue: both ran in parallel, each loaded an empty PublishedRepoCollection from the DB, both passed CheckDuplicate, and the second Add silently overwrote the first. Fix: compute the published repo key ("U{storagePrefix}>>{distribution}") from the already-known storage/prefix/distribution values and append it to resources before calling maybeRunTaskInBackground, so concurrent creates for the same destination are serialised by the task queue. The now-dead append inside the closure is removed.
1 parent 8dc61cf commit 2827620

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

api/publish.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,17 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
300300

301301
collection := collectionFactory.PublishedRepoCollection()
302302

303+
// Pre-register the published repo key in resources so that concurrent
304+
// POST requests for the same prefix/distribution are serialized by the
305+
// task queue rather than racing on CheckDuplicate + Add.
306+
if b.Distribution != "" {
307+
storagePrefix := prefix
308+
if storage != "" {
309+
storagePrefix = storage + ":" + prefix
310+
}
311+
resources = append(resources, "U"+storagePrefix+">>"+b.Distribution)
312+
}
313+
303314
taskName := fmt.Sprintf("Publish %s repository %s/%s with components \"%s\" and sources \"%s\"",
304315
b.SourceKind, param, b.Distribution, strings.Join(components, `", "`), strings.Join(names, `", "`))
305316
maybeRunTaskInBackground(c, taskName, resources, func(out aptly.Progress, detail *task.Detail) (*task.ProcessReturnValue, error) {
@@ -332,8 +343,6 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
332343
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to publish: %s", err)
333344
}
334345

335-
resources = append(resources, string(published.Key()))
336-
337346
if b.Origin != "" {
338347
published.Origin = b.Origin
339348
}

0 commit comments

Comments
 (0)