DEV Community

Cover image for How To Fix Race Condition in Go: Part 2
Ganesh Kumar
Ganesh Kumar

Posted on

How To Fix Race Condition in Go: Part 2

Hello, I'm Ganesh. I'm working on FreeDevTools online, currently building a single platform for all development tools, cheat codes, and TL; DRs — a free, open-source hub where developers can quickly find and use tools without the hassle of searching the internet.

In previous part, we learned about race conditions. Now let's learn how to actually find race conditions in our code.

How Race Condition Results in Unexpected Behavior

By using few goroutines, we can see that race condition is not occurring. But when we increase the number of goroutines, we can see that race condition is occurring.

Let’s increase the number of goroutines to actualy see how race condition results:

package main

import (
    "fmt"
    "time"
)
func increment(counter *int) {
    *counter++
}
func main() {
    counter := 0
    for i := 0; i < 100; i++ {
        go increment(&counter)
    }

    time.Sleep(time.Second)
    fmt.Println("Counter:", counter)
}
Enter fullscreen mode Exit fullscreen mode

Expected Output should be 100

Actual Output

gk@jarvis:~/exp/code/rd$ go run main.go
Counter: 100
gk@jarvis:~/exp/code/rd$ go run main.go
Counter: 984
Enter fullscreen mode Exit fullscreen mode

We used 100 goroutines to increment counter, so it should be 100.
But it’s almost always less. Each goroutine reads and writes counter at the same time, and some updates get lost.

For example:

if Goroutine A reads counter = 5, increments it to 6.
and Goroutine B reads counter = 5 (before Goroutine A writes), increments it to 6.
Both write back 6, losing one increment.

This overlapping is the main root cause of a race condition.

Detecting Race Conditions

Go has a built-in tool which helps to spot race conditions.

Run the program with the -race flag:

go run -race main.go
Enter fullscreen mode Exit fullscreen mode

Let’s use last example:

package main
import (
    "fmt"
    "time"
)
func increment(counter *int) {
    *counter++
}
func main() {
    counter := 0
    for i := 0; i < 100; i++ {
        go increment(&counter)
    }
    time.Sleep(time.Second)
    fmt.Println("Counter:", counter)
}
Enter fullscreen mode Exit fullscreen mode

When you run go run -race main.go, you’ll see a warning like:

==================
WARNING: DATA RACE
Read at 0x00c00011e018 by goroutine 8:
  main.increment()
      /home/gk/exp/code/rd/main.go:9 +0x35
  main.main.gowrap2()
      /home/gk/exp/code/rd/main.go:15 +0x17

Previous write at 0x00c00011e018 by goroutine 7:
  main.increment()
      /home/gk/exp/code/rd/main.go:9 +0x47
  main.main.gowrap1()
      /home/gk/exp/code/rd/main.go:14 +0x17

Goroutine 8 (running) created at:
  main.main()
      /home/gk/exp/code/rd/main.go:15 +0x110

Goroutine 7 (finished) created at:
  main.main()
      /home/gk/exp/code/rd/main.go:14 +0xa6
==================
==================
WARNING: DATA RACE
Read at 0x00c00011e018 by main goroutine:
  main.main()
      /home/gk/exp/code/rd/main.go:18 +0x152

Previous write at 0x00c00011e018 by goroutine 8:
  main.increment()
      /home/gk/exp/code/rd/main.go:9 +0x47
  main.main.gowrap2()
      /home/gk/exp/code/rd/main.go:15 +0x17

Goroutine 8 (finished) created at:
  main.main()
      /home/gk/exp/code/rd/main.go:15 +0x110
==================
Counter: 2
Found 2 data race(s)
exit status 66
Enter fullscreen mode Exit fullscreen mode

This tells us two goroutines are clashing over counter.

The race detector doesn’t fix the problem but helps us to find it.

Conclusion

This is very common race condition happend in any programming language but we must understand why it is happens and how to indentify it and fix it.

If you have build very large application, you can use race detector to find race conditions.

In next part, we will learn how to fix race conditions.


FreeDevTools

I’ve been building for FreeDevTools.

A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction when searching for tools and materials.

Any feedback or contributions are welcome!

It’s online, open-source, and ready for anyone to use.

👉 Check it out: FreeDevTools

⭐ Star it on GitHub: freedevtools

Top comments (0)