Skip to content

Commit 23be6a7

Browse files
author
ladeak
committed
Use System.Threading.Lock in Kestrel
Replacing lock objects with Use System.Threading.Lock HTTP3 has a few dictionaries/lists locked that is not addressed in this PR
1 parent 257d690 commit 23be6a7

File tree

25 files changed

+38
-40
lines changed

25 files changed

+38
-40
lines changed

src/Servers/Kestrel/Core/src/Internal/CertificatePathWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal sealed partial class CertificatePathWatcher : IDisposable
1717
private readonly string _contentRootDir;
1818
private readonly ILogger<CertificatePathWatcher> _logger;
1919

20-
private readonly object _metadataLock = new();
20+
private readonly Lock _metadataLock = new();
2121

2222
/// <remarks>Acquire <see cref="_metadataLock"/> before accessing.</remarks>
2323
private readonly Dictionary<string, DirectoryWatchMetadata> _metadataForDirectory = new();

src/Servers/Kestrel/Core/src/Internal/Http/Http1OutputProducer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal class Http1OutputProducer : IHttpOutputProducer, IDisposable
3434
private readonly TimingPipeFlusher _flusher;
3535

3636
// This locks access to all of the below fields
37-
private readonly object _contextLock = new object();
37+
private readonly Lock _contextLock = new();
3838

3939
private bool _pipeWriterCompleted;
4040
private bool _aborted;

src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal abstract partial class HttpProtocol : IHttpResponseControl
3636
private Stack<KeyValuePair<Func<object, Task>, object>>? _onStarting;
3737
private Stack<KeyValuePair<Func<object, Task>, object>>? _onCompleted;
3838

39-
private readonly object _abortLock = new object();
39+
private readonly Lock _abortLock = new();
4040
protected volatile bool _connectionAborted;
4141
private bool _preventRequestAbortedCancellation;
4242
private CancellationTokenSource? _abortedCts;

src/Servers/Kestrel/Core/src/Internal/Http2/FlowControl/InputFlowControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal sealed class InputFlowControl
1313
private FlowControl _flow;
1414
private int _pendingUpdateSize;
1515
private bool _windowUpdatesDisabled;
16-
private readonly object _flowLock = new object();
16+
private readonly Lock _flowLock = new();
1717

1818
public InputFlowControl(uint initialWindowSize, uint minWindowSizeIncrement)
1919
{

src/Servers/Kestrel/Core/src/Internal/Http2/Http2FrameWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal sealed class Http2FrameWriter
5656

5757
private bool IsFlowControlQueueLimitEnabled => _maximumFlowControlQueueSize > 0;
5858

59-
private readonly object _writeLock = new object();
59+
private readonly Lock _writeLock = new();
6060
private readonly Http2Frame _outgoingFrame;
6161
private readonly Http2HeadersEnumerator _headersEnumerator = new Http2HeadersEnumerator();
6262
private readonly ConcurrentPipeWriter _outputWriter;
@@ -84,7 +84,7 @@ internal sealed class Http2FrameWriter
8484
private bool _completed;
8585
private bool _aborted;
8686

87-
private readonly object _windowUpdateLock = new();
87+
private readonly Lock _windowUpdateLock = new();
8888
private long _connectionWindow;
8989
private readonly Queue<Http2OutputProducer> _waitingForMoreConnectionWindow = new();
9090
// This is the stream that consumed the last set of connection window

src/Servers/Kestrel/Core/src/Internal/Http2/Http2OutputProducer.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal sealed class Http2OutputProducer : IHttpOutputProducer, IHttpOutputAbor
2121

2222
private readonly MemoryPool<byte> _memoryPool;
2323
private readonly Http2Stream _stream;
24-
private readonly object _dataWriterLock = new object();
24+
private readonly Lock _dataWriterLock = new();
2525
private readonly Pipe _pipe;
2626
private readonly ConcurrentPipeWriter _pipeWriter;
2727
private readonly PipeReader _pipeReader;
@@ -592,7 +592,7 @@ public void Reset()
592592

593593
internal void OnRequestProcessingEnded()
594594
{
595-
var shouldCompleteStream = false;
595+
bool shouldCompleteStream;
596596
lock (_dataWriterLock)
597597
{
598598
if (_requestProcessingComplete)
@@ -618,7 +618,7 @@ internal void OnRequestProcessingEnded()
618618

619619
internal ValueTask<FlushResult> CompleteResponseAsync()
620620
{
621-
var shouldCompleteStream = false;
621+
bool shouldCompleteStream;
622622
ValueTask<FlushResult> task = default;
623623

624624
lock (_dataWriterLock)
@@ -698,8 +698,7 @@ internal Memory<byte> GetFakeMemory(int minSize)
698698

699699
public bool TryUpdateStreamWindow(int bytes)
700700
{
701-
var schedule = false;
702-
701+
bool schedule;
703702
lock (_dataWriterLock)
704703
{
705704
var maxUpdate = Http2PeerSettings.MaxWindowSize - _streamWindow;

src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal abstract partial class Http2Stream : HttpProtocol, IThreadPoolWorkItem,
3232
internal long DrainExpirationTimestamp { get; set; }
3333

3434
private StreamCompletionFlags _completionState;
35-
private readonly object _completionLock = new object();
35+
private readonly Lock _completionLock = new();
3636

3737
public void Initialize(Http2StreamContext context)
3838
{

src/Servers/Kestrel/Core/src/Internal/Http3/Http3Connection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ internal sealed class Http3Connection : IHttp3StreamLifetimeHandler, IRequestPro
3535
// so start highest opened request stream ID at -4.
3636
private const long DefaultHighestOpenedRequestStreamId = -4;
3737

38-
private readonly object _sync = new();
38+
private readonly Lock _sync = new();
3939
private readonly HttpMultiplexedConnectionContext _context;
40-
private readonly object _protocolSelectionLock = new();
40+
private readonly Lock _protocolSelectionLock = new();
4141
private readonly StreamCloseAwaitable _streamCompletionAwaitable = new();
4242
private readonly IProtocolErrorCodeFeature _errorCodeFeature;
4343
private readonly Dictionary<long, WebTransportSession>? _webtransportSessions;

src/Servers/Kestrel/Core/src/Internal/Http3/Http3ControlStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal abstract class Http3ControlStream : IHttp3Stream, IThreadPoolWorkItem
2828
private readonly Http3RawFrame _incomingFrame = new Http3RawFrame();
2929
private volatile int _isClosed;
3030
private long _headerType;
31-
private readonly object _completionLock = new();
31+
private readonly Lock _completionLock = new();
3232

3333
private bool _haveReceivedSettingsFrame;
3434
private StreamCompletionFlags _completionState;

src/Servers/Kestrel/Core/src/Internal/Http3/Http3FrameWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal sealed class Http3FrameWriter
2929
private const int MaxDataFrameSize = 16 * 1024;
3030
private const int HeaderBufferSize = 16 * 1024;
3131

32-
private readonly object _writeLock = new object();
32+
private readonly Lock _writeLock = new();
3333

3434
private readonly int _maxTotalHeaderSize;
3535
private readonly ConnectionContext _connectionContext;

0 commit comments

Comments
 (0)