Skip to content

Commit 00d6a0b

Browse files
authored
Fix potential torn reads by counters (#1073)
1 parent 27257a7 commit 00d6a0b

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/Grpc.AspNetCore.Server/Internal/GrpcEventSource.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,31 @@ protected override void OnEventCommand(EventCommandEventArgs command)
136136
// This is the convention for initializing counters in the RuntimeEventSource (lazily on the first enable command).
137137
// They aren't disabled afterwards...
138138

139-
_totalCallsCounter ??= new PollingCounter("total-calls", this, () => _totalCalls)
139+
_totalCallsCounter ??= new PollingCounter("total-calls", this, () => Volatile.Read(ref _totalCalls))
140140
{
141141
DisplayName = "Total Calls",
142142
};
143-
_currentCallsCounter ??= new PollingCounter("current-calls", this, () => _currentCalls)
143+
_currentCallsCounter ??= new PollingCounter("current-calls", this, () => Volatile.Read(ref _currentCalls))
144144
{
145145
DisplayName = "Current Calls"
146146
};
147-
_callsFailedCounter ??= new PollingCounter("calls-failed", this, () => _callsFailed)
147+
_callsFailedCounter ??= new PollingCounter("calls-failed", this, () => Volatile.Read(ref _callsFailed))
148148
{
149149
DisplayName = "Total Calls Failed",
150150
};
151-
_callsDeadlineExceededCounter ??= new PollingCounter("calls-deadline-exceeded", this, () => _callsDeadlineExceeded)
151+
_callsDeadlineExceededCounter ??= new PollingCounter("calls-deadline-exceeded", this, () => Volatile.Read(ref _callsDeadlineExceeded))
152152
{
153153
DisplayName = "Total Calls Deadline Exceeded",
154154
};
155-
_messagesSentCounter ??= new PollingCounter("messages-sent", this, () => _messageSent)
155+
_messagesSentCounter ??= new PollingCounter("messages-sent", this, () => Volatile.Read(ref _messageSent))
156156
{
157157
DisplayName = "Total Messages Sent",
158158
};
159-
_messagesReceivedCounter ??= new PollingCounter("messages-received", this, () => _messageReceived)
159+
_messagesReceivedCounter ??= new PollingCounter("messages-received", this, () => Volatile.Read(ref _messageReceived))
160160
{
161161
DisplayName = "Total Messages Received",
162162
};
163-
_callsUnimplementedCounter ??= new PollingCounter("calls-unimplemented", this, () => _callsUnimplemented)
163+
_callsUnimplementedCounter ??= new PollingCounter("calls-unimplemented", this, () => Volatile.Read(ref _callsUnimplemented))
164164
{
165165
DisplayName = "Total Calls Unimplemented",
166166
};

src/Grpc.Net.Client/Internal/GrpcEventSource.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,27 @@ protected override void OnEventCommand(EventCommandEventArgs command)
125125
// This is the convention for initializing counters in the RuntimeEventSource (lazily on the first enable command).
126126
// They aren't disabled afterwards...
127127

128-
_totalCallsCounter ??= new PollingCounter("total-calls", this, () => _totalCalls)
128+
_totalCallsCounter ??= new PollingCounter("total-calls", this, () => Volatile.Read(ref _totalCalls))
129129
{
130130
DisplayName = "Total Calls",
131131
};
132-
_currentCallsCounter ??= new PollingCounter("current-calls", this, () => _currentCalls)
132+
_currentCallsCounter ??= new PollingCounter("current-calls", this, () => Volatile.Read(ref _currentCalls))
133133
{
134134
DisplayName = "Current Calls"
135135
};
136-
_callsFailedCounter ??= new PollingCounter("calls-failed", this, () => _callsFailed)
136+
_callsFailedCounter ??= new PollingCounter("calls-failed", this, () => Volatile.Read(ref _callsFailed))
137137
{
138138
DisplayName = "Total Calls Failed",
139139
};
140-
_callsDeadlineExceededCounter ??= new PollingCounter("calls-deadline-exceeded", this, () => _callsDeadlineExceeded)
140+
_callsDeadlineExceededCounter ??= new PollingCounter("calls-deadline-exceeded", this, () => Volatile.Read(ref _callsDeadlineExceeded))
141141
{
142142
DisplayName = "Total Calls Deadline Exceeded",
143143
};
144-
_messagesSentCounter ??= new PollingCounter("messages-sent", this, () => _messageSent)
144+
_messagesSentCounter ??= new PollingCounter("messages-sent", this, () => Volatile.Read(ref _messageSent))
145145
{
146146
DisplayName = "Total Messages Sent",
147147
};
148-
_messagesReceivedCounter ??= new PollingCounter("messages-received", this, () => _messageReceived)
148+
_messagesReceivedCounter ??= new PollingCounter("messages-received", this, () => Volatile.Read(ref _messageReceived))
149149
{
150150
DisplayName = "Total Messages Received",
151151
};

test/FunctionalTests/Client/CancellationTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ await call.RequestStream.WriteAsync(new DataMessage
131131
throw;
132132
}
133133
});
134+
135+
// Wait a short amount of time so that any server cancellation error
136+
// finishes being thrown before the next test starts.
137+
await Task.Delay(50);
134138
}
135139

136140
[Test]

0 commit comments

Comments
 (0)