Skip to content

sync.WaitGroup - comment to add wg.Add() call before launching go routine? #4762

Closed
@gopherbot

Description

@gopherbot

by Matthew.Horsnell:

It may be worth adding an explicit comment in the documentation for sync.WaitGroup that
the call to Add should be done before launching the go routine containing the call to
Done.

You may never notice this race condition until shipping code, but the call to Add after
launching the go routine may not execute until the go routine completes, which will
generate a runtime panic as the call to Done inside the routine makes the waitgroup
negative.

What steps will reproduce the problem?

1. Code like http://play.golang.org/p/y_6dKky3qX
2. May occasionally panic due to the waitgroup becoming negative

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions