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 potential torn reads by counters #1073

Merged
merged 3 commits into from
Oct 13, 2020

Conversation

JamesNK
Copy link
Member

@JamesNK JamesNK commented Oct 7, 2020

32bit and ARM might have a torn read of counter long values. Volatile ensures correct value is always read.

@@ -136,31 +136,31 @@ protected override void OnEventCommand(EventCommandEventArgs command)
// This is the convention for initializing counters in the RuntimeEventSource (lazily on the first enable command).
// They aren't disabled afterwards...

_totalCallsCounter ??= new PollingCounter("total-calls", this, () => _totalCalls)
_totalCallsCounter ??= new PollingCounter("total-calls", this, () => Volatile.Read(ref _totalCalls))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's about Interlocked.Read vs. Volatile.Read.
In ASP.NET Core for the kestrel counters I've used Volatile.Read, which does the correct read, then opened dotnet/docs#20984 to update the docs. The consensus over there is to use Interlocked.Read as it's documented to prevent torn-reads.

I'm fine with either variant here, just wanted to point this fact out (and for completeness).

@JamesNK JamesNK merged commit 00d6a0b into grpc:master Oct 13, 2020
@JamesNK JamesNK deleted the jamesnk/eventsource-volatile branch October 13, 2020 23:47
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.

3 participants