Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Mock: Fix AfterFunc data race #42

Closed
wants to merge 1 commit into from
Closed

Mock: Fix AfterFunc data race #42

wants to merge 1 commit into from

Conversation

abhinav
Copy link
Contributor

@abhinav abhinav commented Nov 18, 2021

There's a data race if we call Mock.AfterFunc and Mock.Add
concurrently.

The included test demonstrates this with go test -race.

$ go test -race
==================
WARNING: DATA RACE
Read at 0x00c0000a64e8 by goroutine 50:
  github.com/benbjohnson/clock.(*internalTimer).Tick()
      [...]/clock/clock.go:314 +0xc4
  github.com/benbjohnson/clock.(*Mock).runNextTimer()
      [...]/clock/clock.go:156 +0x1ce
  github.com/benbjohnson/clock.(*Mock).Add()
      [...]/clock/clock.go:96 +0x97
  github.com/benbjohnson/clock.TestMock_AddAfterFuncRace.func3()
      [...]/clock/clock_test.go:687 +0xa7

Previous write at 0x00c0000a64e8 by goroutine 49:
  github.com/benbjohnson/clock.(*Mock).AfterFunc()
      [...]/clock/clock.go:170 +0x178
  github.com/benbjohnson/clock.TestMock_AddAfterFuncRace.func2()
      [...]/clock/clock_test.go:677 +0xb9

Goroutine 50 (running) created at:
  github.com/benbjohnson/clock.TestMock_AddAfterFuncRace()
      [...]/clock/clock_test.go:683 +0x464
  testing.tRunner()
      [...]/go/1.17.2/libexec/src/testing/testing.go:1259 +0x22f
  testing.(*T).Run·dwrap·21()
      [...]/go/1.17.2/libexec/src/testing/testing.go:1306 +0x47

Goroutine 49 (finished) created at:
  github.com/benbjohnson/clock.TestMock_AddAfterFuncRace()
      [...]/clock/clock_test.go:673 +0x347
  testing.tRunner()
      [...]/go/1.17.2/libexec/src/testing/testing.go:1259 +0x22f
  testing.(*T).Run·dwrap·21()
      [...]/go/1.17.2/libexec/src/testing/testing.go:1306 +0x47
==================
--- FAIL: TestMock_AddAfterFuncRace (0.01s)
    testing.go:1152: race detected during execution of test
FAIL
exit status 1

Fix the race by applying the same mutex used while reading t.fn
in internalTimer.Tick.

There's a data race if we call Mock.AfterFunc and Mock.Add
concurrently.

The included test demonstrates this with `go test -race`.

```
$ go test -race
==================
WARNING: DATA RACE
Read at 0x00c0000a64e8 by goroutine 50:
  github.com/benbjohnson/clock.(*internalTimer).Tick()
      [...]/clock/clock.go:314 +0xc4
  github.com/benbjohnson/clock.(*Mock).runNextTimer()
      [...]/clock/clock.go:156 +0x1ce
  github.com/benbjohnson/clock.(*Mock).Add()
      [...]/clock/clock.go:96 +0x97
  github.com/benbjohnson/clock.TestMock_AddAfterFuncRace.func3()
      [...]/clock/clock_test.go:687 +0xa7

Previous write at 0x00c0000a64e8 by goroutine 49:
  github.com/benbjohnson/clock.(*Mock).AfterFunc()
      [...]/clock/clock.go:170 +0x178
  github.com/benbjohnson/clock.TestMock_AddAfterFuncRace.func2()
      [...]/clock/clock_test.go:677 +0xb9

Goroutine 50 (running) created at:
  github.com/benbjohnson/clock.TestMock_AddAfterFuncRace()
      [...]/clock/clock_test.go:683 +0x464
  testing.tRunner()
      [...]/go/1.17.2/libexec/src/testing/testing.go:1259 +0x22f
  testing.(*T).Run·dwrap·21()
      [...]/go/1.17.2/libexec/src/testing/testing.go:1306 +0x47

Goroutine 49 (finished) created at:
  github.com/benbjohnson/clock.TestMock_AddAfterFuncRace()
      [...]/clock/clock_test.go:673 +0x347
  testing.tRunner()
      [...]/go/1.17.2/libexec/src/testing/testing.go:1259 +0x22f
  testing.(*T).Run·dwrap·21()
      [...]/go/1.17.2/libexec/src/testing/testing.go:1306 +0x47
==================
--- FAIL: TestMock_AddAfterFuncRace (0.01s)
    testing.go:1152: race detected during execution of test
FAIL
exit status 1
```

Fix the race by applying the same mutex used while reading `t.fn`
in `internalTimer.Tick`.
abhinav added a commit to uber-go/fx that referenced this pull request Nov 18, 2021
This temporarily pins to a fork of benbjohnson/clock to validate the fix
while we wait for benbjohnson/clock#42
to be reviewed upstream.
abhinav added a commit to uber-go/fx that referenced this pull request Nov 18, 2021
This temporarily pins to a fork of benbjohnson/clock to validate the fix
while we wait for benbjohnson/clock#42
to be reviewed upstream.
abhinav added a commit to uber-go/fx that referenced this pull request Nov 18, 2021
This temporarily pins to a fork of benbjohnson/clock to validate the fix
while we wait for benbjohnson/clock#42
to be reviewed upstream.
abhinav added a commit to uber-go/fx that referenced this pull request Nov 19, 2021
This temporarily pins to a fork of benbjohnson/clock to validate the fix
while we wait for benbjohnson/clock#42
to be reviewed upstream.
@vincentbernat
Copy link
Contributor

I did run with the same issue on my own code. The proposed fix makes it work.

@abhinav
Copy link
Contributor Author

abhinav commented Apr 19, 2023

Closing in favor of #52

@abhinav abhinav closed this Apr 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants