Temporal goroutines race condintions

Example of a simple race condition in golang:
Example of a fix:

Question, If I do something similar in temporal(workflow.Go), will I need some sort of mutex handling in order to avoid race conditions ? If so can I use gos sync.Mutex ?

Temporal Go SDK has a mutex sample - samples-go/mutex at master · temporalio/samples-go · GitHub

Does that help?

@jonsve got some more info on the use of mutexes in Temporal from Maxim:

Mutex is only needed when your code blocks on things like Timer or Activity Futures.

Temporal uses cooperative multitasking meaning that threads not preempted unless they block on some Temporal API such as await, sleep or Future.Get. There are no race conditions with Temporal that would require a mutex unless some blocking operation is involved as mentioned earlier.

1 Like

Ok so for example

RaceWorkflow has a racecondition ? and OkWorkflow doesn’t ?

Both of them don’t have race conditions as threads are only preempted in the timer.Get call. To have a race condition you need something like:

  value := cool.Value + 1
  timer.Get(goctx, nil)
  cool.Value = value
1 Like