Skip to content

Commit a570da9

Browse files
gfoidlYohDeadfall
authored andcommitted
Fixed potential torn reads in EventCounters
The backing fields for the counters are `long`, so there was potential torn read on 32-bit platforms. `Volatile.Read` ensures that the value is read in a atomic way, so no torn reads can occur.
1 parent 8d21440 commit a570da9

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/Npgsql/NpgsqlEventSource.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,43 +134,43 @@ protected override void OnEventCommand(EventCommandEventArgs command)
134134
// overhead by at all times even when counters aren't enabled.
135135
// On disable, PollingCounters will stop polling for values so it should be fine to leave them around.
136136

137-
_bytesWrittenPerSecondCounter = new IncrementingPollingCounter("bytes-written-per-second", this, () => _bytesWritten)
137+
_bytesWrittenPerSecondCounter = new IncrementingPollingCounter("bytes-written-per-second", this, () => Volatile.Read(ref _bytesWritten))
138138
{
139139
DisplayName = "Bytes Written",
140140
DisplayRateTimeScale = TimeSpan.FromSeconds(1)
141141
};
142142

143-
_bytesReadPerSecondCounter = new IncrementingPollingCounter("bytes-read-per-second", this, () => _bytesRead)
143+
_bytesReadPerSecondCounter = new IncrementingPollingCounter("bytes-read-per-second", this, () => Volatile.Read(ref _bytesRead))
144144
{
145145
DisplayName = "Bytes Read",
146146
DisplayRateTimeScale = TimeSpan.FromSeconds(1)
147147
};
148148

149-
_commandsPerSecondCounter = new IncrementingPollingCounter("commands-per-second", this, () => _totalCommands)
149+
_commandsPerSecondCounter = new IncrementingPollingCounter("commands-per-second", this, () => Volatile.Read(ref _totalCommands))
150150
{
151151
DisplayName = "Command Rate",
152152
DisplayRateTimeScale = TimeSpan.FromSeconds(1)
153153
};
154154

155-
_totalCommandsCounter = new PollingCounter("total-commands", this, () => _totalCommands)
155+
_totalCommandsCounter = new PollingCounter("total-commands", this, () => Volatile.Read(ref _totalCommands))
156156
{
157157
DisplayName = "Total Commands",
158158
};
159159

160-
_currentCommandsCounter = new PollingCounter("current-commands", this, () => _currentCommands)
160+
_currentCommandsCounter = new PollingCounter("current-commands", this, () => Volatile.Read(ref _currentCommands))
161161
{
162162
DisplayName = "Current Commands"
163163
};
164164

165-
_failedCommandsCounter = new PollingCounter("failed-commands", this, () => _failedCommands)
165+
_failedCommandsCounter = new PollingCounter("failed-commands", this, () => Volatile.Read(ref _failedCommands))
166166
{
167167
DisplayName = "Failed Commands"
168168
};
169169

170170
_preparedCommandsRatioCounter = new PollingCounter(
171171
"prepared-commands-ratio",
172172
this,
173-
() => (double)_totalPreparedCommands / (double)_totalCommands)
173+
() => (double)Volatile.Read(ref _totalPreparedCommands) / Volatile.Read(ref _totalCommands))
174174
{
175175
DisplayName = "Prepared Commands Ratio",
176176
DisplayUnits = "%"
@@ -191,17 +191,17 @@ protected override void OnEventCommand(EventCommandEventArgs command)
191191
DisplayName = "Busy Connections"
192192
};
193193

194-
_multiplexingAverageCommandsPerBatchCounter = new PollingCounter("multiplexing-average-commands-per-batch", this, () => (double)_multiplexingCommandsSent / _multiplexingBatchesSent)
194+
_multiplexingAverageCommandsPerBatchCounter = new PollingCounter("multiplexing-average-commands-per-batch", this, () => (double)Volatile.Read(ref _multiplexingCommandsSent) / Volatile.Read(ref _multiplexingBatchesSent))
195195
{
196196
DisplayName = "Average commands per multiplexing batch"
197197
};
198198

199-
_multiplexingAverageWaitsPerBatchCounter = new PollingCounter("multiplexing-average-waits-per-batch", this, () => (double)_multiplexingWaits / _multiplexingBatchesSent)
199+
_multiplexingAverageWaitsPerBatchCounter = new PollingCounter("multiplexing-average-waits-per-batch", this, () => (double)Volatile.Read(ref _multiplexingWaits) / Volatile.Read(ref _multiplexingBatchesSent))
200200
{
201201
DisplayName = "Average waits per multiplexing batch"
202202
};
203203

204-
_multiplexingAverageWriteTimePerBatchCounter = new PollingCounter("multiplexing-average-write-time-per-batch", this, () => (double)_multiplexingTicksWritten / _multiplexingBatchesSent / 1000)
204+
_multiplexingAverageWriteTimePerBatchCounter = new PollingCounter("multiplexing-average-write-time-per-batch", this, () => (double)Volatile.Read(ref _multiplexingTicksWritten) / Volatile.Read(ref _multiplexingBatchesSent) / 1000)
205205
{
206206
DisplayName = "Average write time per multiplexing batch (us)",
207207
DisplayUnits = "us"

0 commit comments

Comments
 (0)