Skip to content

fix: move wg.Add(1) before goroutine launch to prevent negative WaitGroup counter#129

Merged
alajmo merged 1 commit into
alajmo:mainfrom
mikeboiko:fix/waitgroup-race-condition
May 12, 2026
Merged

fix: move wg.Add(1) before goroutine launch to prevent negative WaitGroup counter#129
alajmo merged 1 commit into
alajmo:mainfrom
mikeboiko:fix/waitgroup-race-condition

Conversation

@mikeboiko

Copy link
Copy Markdown
Contributor

Problem

Running mani run with --parallel intermittently panics with:

panic: sync: negative WaitGroup counter

Root Cause

In core/exec/text.go, the stdout/stderr copy goroutines were launched with defer wg.Done() before wg.Add(1) was called. If a goroutine completed before Add(1) executed, Done() would decrement the counter below zero, causing a panic.

Fix

Move wg.Add(1) to before each go func(...) call — the standard Go pattern for WaitGroups.

Fixes #124

…roup counter

The goroutines for copying stdout/stderr were launched with defer wg.Done()
before wg.Add(1) was called. If a goroutine completed before Add(1) executed,
Done() would decrement the counter below zero, causing a panic.

Fixes alajmo#124

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@alajmo alajmo merged commit a95bcee into alajmo:main May 12, 2026
1 check passed
@alajmo

alajmo commented May 12, 2026

Copy link
Copy Markdown
Owner

Thanks for fixing this.

@mikeboiko

Copy link
Copy Markdown
Contributor Author

Thanks for fixing this.

np - and thanks for merging :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intermittent error: panic: sync: negative WaitGroup counter

2 participants