Skip to content

Possible race condition of using workflow.Go #1514

Description

@shijiesheng

Describe the bug
Root cause of the race:
dispatcher.Close() (Cadence internals) kills all background workflow coroutines simultaneously by sending runtime.Goexit() to each via their unblock channel — without waiting for each to finish before starting the next. Each goroutine runs its deferred selector cleanup (removeReceiveCallback) in its own OS thread, concurrently. Since workflow.Go derives the goroutine context using WithValue (not WithCancel), ALL goroutines share the same root Done() channelImpl. Concurrent removeReceiveCallback on a shared, unsynchronized slice → data race.

The fix: Add goCtx, cancel := workflow.WithCancel(ctx); defer cancel() inside each background goroutine body, then use goCtx.Done() in the selector instead of ctx.Done(). workflow.WithCancel creates a fresh channelImpl for each goroutine's Done channel (confirmed in context.go: done: NewNamedChannel(parent, "cancelCtx-done-channel")). When Close() kills all goroutines concurrently, each removes its callback from its own unique channel — no shared state, no race. Parent cancellation still propagates correctly because the root workflow context is itself a *cancelCtx, so propagateCancel registers children on it (protected by childrenLock sync.Mutex) and cancels them sequentially.

To Reproduce
Is the issue reproducible?

  • [Yes | No]

Steps to reproduce the behavior:
A clear and concise description of the reproduce steps.

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here, E.g. Stackstace, workflow history.

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