Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit f8e46b4

Browse files
committed
ensure: use mutex with addInstructions
1 parent bc88218 commit f8e46b4

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

cmd/dep/ensure.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/golang/dep/internal/gps/paths"
2323
"github.com/golang/dep/internal/gps/pkgtree"
2424
"github.com/pkg/errors"
25-
"golang.org/x/sync/syncmap"
2625
)
2726

2827
const ensureShortHelp = `Ensure a dependency is safely vendored in the project`
@@ -470,9 +469,10 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
470469
constraint gps.Constraint
471470
typ addType
472471
}
472+
addInstructions := make(map[gps.ProjectRoot]addInstruction)
473473

474-
// Create a syncmap to store all the addInstruction(s) concurrently.
475-
addInstructions := new(syncmap.Map)
474+
// A mutex for limited access to addInstructions by goroutines.
475+
var mutex sync.Mutex
476476

477477
// Channel for receiving all the errors.
478478
errCh := make(chan error, len(args))
@@ -519,8 +519,9 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
519519

520520
someConstraint := !gps.IsAny(pc.Constraint) || pc.Ident.Source != ""
521521

522-
instrInterface, has := addInstructions.LoadOrStore(pc.Ident.ProjectRoot, addInstruction{})
523-
instr := instrInterface.(addInstruction)
522+
// Obtain a lock for addInstructions
523+
mutex.Lock()
524+
instr, has := addInstructions[pc.Ident.ProjectRoot]
524525
if has {
525526
// Multiple packages from the same project were specified as
526527
// arguments; make sure they agree on declared constraints.
@@ -575,7 +576,8 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
575576
instr.ephReq[path] = true
576577
}
577578

578-
addInstructions.Store(pc.Ident.ProjectRoot, instr)
579+
addInstructions[pc.Ident.ProjectRoot] = instr
580+
mutex.Unlock()
579581
}(arg)
580582
}
581583

@@ -597,9 +599,7 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
597599

598600
// We're now sure all of our add instructions are individually and mutually
599601
// valid, so it's safe to begin modifying the input parameters.
600-
addInstructions.Range(func(ki, vi interface{}) bool {
601-
pr := ki.(gps.ProjectRoot)
602-
instr := vi.(addInstruction)
602+
for pr, instr := range addInstructions {
603603
// The arg processing logic above only adds to the ephReq list if
604604
// that package definitely needs to be on that list, so we don't
605605
// need to check instr.typ here - if it's in instr.ephReq, it
@@ -616,9 +616,7 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
616616
Constraint: instr.constraint,
617617
}
618618
}
619-
620-
return true
621-
})
619+
}
622620

623621
// Re-prepare a solver now that our params are complete.
624622
solver, err = gps.Prepare(params, sm)
@@ -636,10 +634,7 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
636634
var reqlist []string
637635
appender := dep.NewManifest()
638636

639-
addInstructions.Range(func(ki, vi interface{}) bool {
640-
pr := ki.(gps.ProjectRoot)
641-
instr := vi.(addInstruction)
642-
637+
for pr, instr := range addInstructions {
643638
for path := range instr.ephReq {
644639
reqlist = append(reqlist, path)
645640
}
@@ -667,9 +662,7 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
667662
}
668663
appender.Constraints[pr] = pp
669664
}
670-
671-
return true
672-
})
665+
}
673666

674667
extra, err := appender.MarshalTOML()
675668
if err != nil {

0 commit comments

Comments
 (0)