Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix datarace while update a channel.lastEventID #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

iscander
Copy link

@iscander iscander commented Jul 5, 2021

Protect the option by wrapping into a mutex. The data race is gone.
It closes #18

Provide fix and a unit test. The test was failing if run with race detection on.

go test -timeout 30s -v -race -run ^TestMultipleTopics$
....

WARNING: DATA RACE
Write at 0x00c0000120d8 by goroutine 39:
github.com/alexandrevicenzi/go-sse.(*Channel).SendMessage()
/Users/iscander/projects/go-sse/channel.go:26 +0x8c
github.com/alexandrevicenzi/go-sse.(*Server).SendMessage()
/Users/iscander/projects/go-sse/sse.go:116 +0x21b
github.com/alexandrevicenzi/go-sse.TestMultipleTopics.func2()
/Users/iscander/projects/go-sse/sse_test.go:135 +0xce

Previous write at 0x00c0000120d8 by goroutine 38:
github.com/alexandrevicenzi/go-sse.(*Channel).SendMessage()
/Users/iscander/projects/go-sse/channel.go:26 +0x8c
github.com/alexandrevicenzi/go-sse.(*Server).SendMessage()
/Users/iscander/projects/go-sse/sse.go:116 +0x21b
github.com/alexandrevicenzi/go-sse.TestMultipleTopics.func2()
/Users/iscander/projects/go-sse/sse_test.go:135 +0xce

Goroutine 39 (running) created at:
github.com/alexandrevicenzi/go-sse.TestMultipleTopics()
/Users/iscander/projects/go-sse/sse_test.go:132 +0x735
testing.tRunner()
/usr/local/Cellar/go/1.16.3/libexec/src/testing/testing.go:1193 +0x202

Goroutine 38 (running) created at:
github.com/alexandrevicenzi/go-sse.TestMultipleTopics()
/Users/iscander/projects/go-sse/sse_test.go:132 +0x735
testing.tRunner()
/usr/local/Cellar/go/1.16.3/libexec/src/testing/testing.go:1193 +0x202
...

The usage pattern: Imagine  we have a client which subsribed to multiple topics
inside one connection. It track changes of specific items state by their ID
as example.

The test is passing in general case. But run it with race detection option and
it fails due data race condition detected.

==================
WARNING: DATA RACE
Write at 0x00c0000120d8 by goroutine 39:
  github.com/alexandrevicenzi/go-sse.(*Channel).SendMessage()
      /Users/iscander/projects/go-sse/channel.go:26 +0x8c
  github.com/alexandrevicenzi/go-sse.(*Server).SendMessage()
      /Users/iscander/projects/go-sse/sse.go:116 +0x21b
  github.com/alexandrevicenzi/go-sse.TestMultipleTopics.func2()
      /Users/iscander/projects/go-sse/sse_test.go:135 +0xce

Previous write at 0x00c0000120d8 by goroutine 38:
  github.com/alexandrevicenzi/go-sse.(*Channel).SendMessage()
      /Users/iscander/projects/go-sse/channel.go:26 +0x8c
  github.com/alexandrevicenzi/go-sse.(*Server).SendMessage()
      /Users/iscander/projects/go-sse/sse.go:116 +0x21b
  github.com/alexandrevicenzi/go-sse.TestMultipleTopics.func2()
      /Users/iscander/projects/go-sse/sse_test.go:135 +0xce

Goroutine 39 (running) created at:
  github.com/alexandrevicenzi/go-sse.TestMultipleTopics()
      /Users/iscander/projects/go-sse/sse_test.go:132 +0x735
  testing.tRunner()
      /usr/local/Cellar/go/1.16.3/libexec/src/testing/testing.go:1193 +0x202

Goroutine 38 (running) created at:
  github.com/alexandrevicenzi/go-sse.TestMultipleTopics()
      /Users/iscander/projects/go-sse/sse_test.go:132 +0x735
  testing.tRunner()
      /usr/local/Cellar/go/1.16.3/libexec/src/testing/testing.go:1193 +0x202
Protect the option by wrappring into a mutex. The datarace is gone.
It closes alexandrevicenzi#18
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.

Data races
1 participant