Skip to content

Fix data race when creating context #828

@barweiss

Description

@barweiss

There's a data race that exists when creating Ziti contexts:

==================
WARNING: DATA RACE
Read at 0x000104174c90 by goroutine 2003:
  github.com/openziti/sdk-golang/ziti.NewId()
      /Users/bar/go/pkg/mod/github.com/openziti/sdk-golang@v1.2.1/ziti/contexts.go:46 +0x60
  github.com/openziti/sdk-golang/ziti.NewContextWithOpts()
      /Users/bar/go/pkg/mod/github.com/openziti/sdk-golang@v1.2.1/ziti/contexts.go:85 +0x3e4
...

The issue is that NewId() increments a global counter without any synchronization:

var idCount = 0

// NewId will return a unique string id suitable for ziti.Context Id functionality.
func NewId() string {
	idCount = idCount + 1

	return strconv.Itoa(idCount)
}

An easy way to solve this can be to the atomic package

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