Skip to content

Possible data race in manager memory test #2536

@VincentRbbmnd

Description

@VincentRbbmnd

Found in manager/state/store/memory_test.go lines 2036-2054

for c := 0; c != 5; c++ {
	wg.Add(1)
	go func() {
		defer wg.Done()
		for i := 0; i < b.N; i++ {
			_ = s.Update(func(tx1 Tx) error {
				_ = UpdateNode(tx1, &api.Node{
					ID: nodeIDs[i%benchmarkNumNodes],
					Spec: api.NodeSpec{
						Annotations: api.Annotations{
							Name: nodeIDs[i%benchmarkNumNodes] + "_" + strconv.Itoa(c) + "_" + strconv.Itoa(i),
						},
					},
				})
				return nil
			})
		}
	}()
}

Loop variable c is used within the func literal (in the statement strconv.Itoa(c)), possibly leading to a data race, causing all loop iterations to use the same value of c as mentioned here.

Two possible fixes:

  • Pass c as a param in the func literal
    go func(c int) {
        ...
    }(c)
  • Create a new variable
    for c := 0; c != 5; c++ {
        ...
        x := c
        go func() {
            ...
            Name: nodeIDs[i%benchmarkNumNodes] + "_" + strconv.Itoa(x) + "_" + strconv.Itoa(i),
            ...
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions