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

Fixes race condition in EventStream #880

Merged
merged 1 commit into from
Aug 12, 2023

Conversation

ekcasey
Copy link
Contributor

@ekcasey ekcasey commented Aug 9, 2023

Locks the mutex for reading when getting Length(). I am hoping this will solve the following data race go test is reporting:

WARNING: DATA RACE
Read at 0x00c000224a30 by goroutine 5563:
  sync/atomic.LoadInt32()
      /opt/hostedtoolcache/go/1.20.6/x64/src/runtime/race_amd64.s:202 +0xb
  sync/atomic.LoadInt32()
      <autogenerated>:1 +0x16
  github.com/asynkron/protoactor-go/actor.(*deadLetterProcess).SendSystemMessage()
      /home/runner/go/pkg/mod/github.com/asynkron/protoactor-go@v0.0.0-20230703103118-df5e4f42621c/actor/deadletter.go:97 +0x18c
  github.com/asynkron/protoactor-go/actor.(*deadLetterProcess).Stop()
      /home/runner/go/pkg/mod/github.com/asynkron/protoactor-go@v0.0.0-20230703103118-df5e4f42621c/actor/deadletter.go:104 +0x2c
  github.com/asynkron/protoactor-go/actor.(*actorContext).Stop()
      /home/runner/go/pkg/mod/github.com/asynkron/protoactor-go@v0.0.0-20230703103118-df5e4f42621c/actor/actor_context.go:436 +0x298
...

Previous write at 0x00c000224a30 by goroutine 5570:
  github.com/asynkron/protoactor-go/eventstream.(*EventStream).Subscribe()
      /home/runner/go/pkg/mod/github.com/asynkron/protoactor-go@v0.0.0-20230703103118-df5e4f42621c/eventstream/eventstream.go:44 +0x1e4
...

I am assuming the atomic read was supposed to protect against? But if the write isn't atomic I think there is still the potential for issues. As I understand it read and write should either both use the mutex (as in this PR) or ensure that all operations are atomic.

Locks the mutex for reading when getting Length(). Otherwise, a data race can occur. This can happen despite the previous atomic read because writes are non-atomic.

Signed-off-by: Emily Casey <emily@atomicjar.com>
@CLAassistant
Copy link

CLAassistant commented Aug 9, 2023

CLA assistant check
All committers have signed the CLA.

@rogeralsing
Copy link
Collaborator

Awesome!

@rogeralsing rogeralsing merged commit 3987052 into asynkron:dev Aug 12, 2023
2 checks passed
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.

None yet

3 participants