Skip to content

Commit 20ad9fa

Browse files
committed
Merged PR 11174: Run callbacks outside of locks
1 parent 5f146ad commit 20ad9fa

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/Servers/Kestrel/Core/src/Internal/Infrastructure/TimeoutControl.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ private void CheckForReadDataRateTimeout(long timestamp)
8989
return;
9090
}
9191

92+
var timeout = false;
93+
9294
lock (_readTimingLock)
9395
{
9496
if (!_readTimingEnabled)
@@ -105,10 +107,7 @@ private void CheckForReadDataRateTimeout(long timestamp)
105107
var elapsedSeconds = (double)_readTimingElapsedTicks / TimeSpan.TicksPerSecond;
106108
var rate = _readTimingBytesRead / elapsedSeconds;
107109

108-
if (rate < _minReadRate.BytesPerSecond && !Debugger.IsAttached)
109-
{
110-
_timeoutHandler.OnTimeout(TimeoutReason.ReadDataRate);
111-
}
110+
timeout = rate < _minReadRate.BytesPerSecond && !Debugger.IsAttached;
112111
}
113112

114113
// PauseTimingReads() cannot just set _timingReads to false. It needs to go through at least one tick
@@ -120,10 +119,18 @@ private void CheckForReadDataRateTimeout(long timestamp)
120119
_readTimingPauseRequested = false;
121120
}
122121
}
122+
123+
if (timeout)
124+
{
125+
// Run callbacks outside of the lock
126+
_timeoutHandler.OnTimeout(TimeoutReason.ReadDataRate);
127+
}
123128
}
124129

125130
private void CheckForWriteDataRateTimeout(long timestamp)
126131
{
132+
var timeout = false;
133+
127134
lock (_writeTimingLock)
128135
{
129136
// Assume overly long tick intervals are the result of server resource starvation.
@@ -135,10 +142,13 @@ private void CheckForWriteDataRateTimeout(long timestamp)
135142
_writeTimingTimeoutTimestamp += extraTimeForTick;
136143
}
137144

138-
if (_concurrentAwaitingWrites > 0 && timestamp > _writeTimingTimeoutTimestamp && !Debugger.IsAttached)
139-
{
140-
_timeoutHandler.OnTimeout(TimeoutReason.WriteDataRate);
141-
}
145+
timeout = _concurrentAwaitingWrites > 0 && timestamp > _writeTimingTimeoutTimestamp && !Debugger.IsAttached;
146+
}
147+
148+
if (timeout)
149+
{
150+
// Run callbacks outside of the lock
151+
_timeoutHandler.OnTimeout(TimeoutReason.WriteDataRate);
142152
}
143153
}
144154

0 commit comments

Comments
 (0)