Skip to content

Racy code in 'Once-Only Initialization' section #18

@denpeshkov

Description

@denpeshkov

This code contains a race:

var initialized atomic.Int32

func maybeInit() {
    if initialized.CompareAndSwap(0, 1) {
        // initialize resources
    }
}

After the CAS operation succeeds - the initialized flag indicated that the resource is initialized, even when in reality it's still in progress of being initialized. If the other goroutine checks the initialized flag it can access an uninitialized object

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions