From 5d7287b5570b89de42b785d29baed9127e53eaf3 Mon Sep 17 00:00:00 2001 From: ladeak Date: Thu, 8 Aug 2024 15:51:54 +0200 Subject: [PATCH] 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 --- .../Core/src/Internal/CertificatePathWatcher.cs | 2 +- .../Core/src/Internal/Http/Http1OutputProducer.cs | 2 +- .../Kestrel/Core/src/Internal/Http/HttpProtocol.cs | 2 +- .../src/Internal/Http2/FlowControl/InputFlowControl.cs | 2 +- .../Core/src/Internal/Http2/Http2FrameWriter.cs | 4 ++-- .../Core/src/Internal/Http2/Http2OutputProducer.cs | 9 ++++----- .../Kestrel/Core/src/Internal/Http2/Http2Stream.cs | 2 +- .../Kestrel/Core/src/Internal/Http3/Http3Connection.cs | 4 ++-- .../Core/src/Internal/Http3/Http3ControlStream.cs | 2 +- .../Core/src/Internal/Http3/Http3FrameWriter.cs | 2 +- .../Core/src/Internal/Http3/Http3OutputProducer.cs | 2 +- .../Kestrel/Core/src/Internal/Http3/Http3Stream.cs | 2 +- .../Kestrel/Core/src/Internal/HttpConnection.cs | 2 +- .../src/Internal/Infrastructure/KestrelConnection.cs | 2 +- .../PipeWriterHelpers/ConcurrentPipeWriter.cs | 4 ++-- .../Core/src/Internal/Infrastructure/TimeoutControl.cs | 7 +++---- .../Kestrel/Core/test/ConcurrentPipeWriterTests.cs | 10 +++++----- .../src/Internal/NamedPipeConnection.cs | 2 +- .../src/Internal/QuicConnectionContext.cs | 4 ++-- .../Transport.Quic/src/Internal/QuicStreamContext.cs | 2 +- .../Transport.Sockets/src/Internal/SocketConnection.cs | 2 +- .../Microbenchmarks/Mocks/MockHttpContextFactory.cs | 2 +- .../test/InMemory.FunctionalTests/EventSourceTests.cs | 2 +- .../InMemory.FunctionalTests/Http2/Http2TestBase.cs | 2 +- .../ServerInfrastructure/DuplexPipeStreamAdapter.cs | 2 +- 25 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/Servers/Kestrel/Core/src/Internal/CertificatePathWatcher.cs b/src/Servers/Kestrel/Core/src/Internal/CertificatePathWatcher.cs index 052255e1a343..73e956c585f9 100644 --- a/src/Servers/Kestrel/Core/src/Internal/CertificatePathWatcher.cs +++ b/src/Servers/Kestrel/Core/src/Internal/CertificatePathWatcher.cs @@ -17,7 +17,7 @@ internal sealed partial class CertificatePathWatcher : IDisposable private readonly string _contentRootDir; private readonly ILogger _logger; - private readonly object _metadataLock = new(); + private readonly Lock _metadataLock = new(); /// Acquire before accessing. private readonly Dictionary _metadataForDirectory = new(); diff --git a/src/Servers/Kestrel/Core/src/Internal/Http/Http1OutputProducer.cs b/src/Servers/Kestrel/Core/src/Internal/Http/Http1OutputProducer.cs index a1778a1cb4b5..c922e6bf6822 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http/Http1OutputProducer.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http/Http1OutputProducer.cs @@ -34,7 +34,7 @@ internal class Http1OutputProducer : IHttpOutputProducer, IDisposable private readonly TimingPipeFlusher _flusher; // This locks access to all of the below fields - private readonly object _contextLock = new object(); + private readonly Lock _contextLock = new(); private bool _pipeWriterCompleted; private bool _aborted; diff --git a/src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs b/src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs index 6cfa7f4e8ccb..eaae71743b57 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs @@ -43,7 +43,7 @@ internal abstract partial class HttpProtocol : IHttpResponseControl private Stack, object>>? _onStarting; private Stack, object>>? _onCompleted; - private readonly object _abortLock = new object(); + private readonly Lock _abortLock = new(); protected volatile bool _connectionAborted; private bool _preventRequestAbortedCancellation; private CancellationTokenSource? _abortedCts; diff --git a/src/Servers/Kestrel/Core/src/Internal/Http2/FlowControl/InputFlowControl.cs b/src/Servers/Kestrel/Core/src/Internal/Http2/FlowControl/InputFlowControl.cs index 00d8cf43d144..e3d57bad0507 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http2/FlowControl/InputFlowControl.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http2/FlowControl/InputFlowControl.cs @@ -25,7 +25,7 @@ internal sealed class InputFlowControl private FlowControl _flow; private int _pendingUpdateSize; private bool _windowUpdatesDisabled; - private readonly object _flowLock = new object(); + private readonly Lock _flowLock = new(); public InputFlowControl(uint initialWindowSize, uint minWindowSizeIncrement) { diff --git a/src/Servers/Kestrel/Core/src/Internal/Http2/Http2FrameWriter.cs b/src/Servers/Kestrel/Core/src/Internal/Http2/Http2FrameWriter.cs index b35082734b9c..6b601fdcc7be 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http2/Http2FrameWriter.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http2/Http2FrameWriter.cs @@ -71,7 +71,7 @@ internal sealed class Http2FrameWriter private bool IsFlowControlQueueLimitEnabled => _maximumFlowControlQueueSize > 0; - private readonly object _writeLock = new object(); + private readonly Lock _writeLock = new(); private readonly Http2Frame _outgoingFrame; private readonly Http2HeadersEnumerator _headersEnumerator = new Http2HeadersEnumerator(); private readonly ConcurrentPipeWriter _outputWriter; @@ -99,7 +99,7 @@ internal sealed class Http2FrameWriter private bool _completed; private bool _aborted; - private readonly object _windowUpdateLock = new(); + private readonly Lock _windowUpdateLock = new(); private long _connectionWindow; private readonly Queue _waitingForMoreConnectionWindow = new(); // This is the stream that consumed the last set of connection window diff --git a/src/Servers/Kestrel/Core/src/Internal/Http2/Http2OutputProducer.cs b/src/Servers/Kestrel/Core/src/Internal/Http2/Http2OutputProducer.cs index 2c551030a0ca..1a1e6a3a9879 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http2/Http2OutputProducer.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http2/Http2OutputProducer.cs @@ -28,7 +28,7 @@ internal sealed class Http2OutputProducer : IHttpOutputProducer, IHttpOutputAbor private readonly MemoryPool _memoryPool; private readonly Http2Stream _stream; - private readonly object _dataWriterLock = new object(); + private readonly Lock _dataWriterLock = new(); private readonly Pipe _pipe; private readonly ConcurrentPipeWriter _pipeWriter; private readonly PipeReader _pipeReader; @@ -599,7 +599,7 @@ public void Reset() internal void OnRequestProcessingEnded() { - var shouldCompleteStream = false; + bool shouldCompleteStream; lock (_dataWriterLock) { if (_requestProcessingComplete) @@ -625,7 +625,7 @@ internal void OnRequestProcessingEnded() internal ValueTask CompleteResponseAsync() { - var shouldCompleteStream = false; + bool shouldCompleteStream; ValueTask task = default; lock (_dataWriterLock) @@ -705,8 +705,7 @@ internal Memory GetFakeMemory(int minSize) public bool TryUpdateStreamWindow(int bytes) { - var schedule = false; - + bool schedule; lock (_dataWriterLock) { var maxUpdate = Http2PeerSettings.MaxWindowSize - _streamWindow; diff --git a/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs b/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs index 42237f871d74..da2b547c03a7 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs @@ -44,7 +44,7 @@ internal abstract partial class Http2Stream : HttpProtocol, IThreadPoolWorkItem, internal long DrainExpirationTimestamp { get; set; } private StreamCompletionFlags _completionState; - private readonly object _completionLock = new object(); + private readonly Lock _completionLock = new(); public void Initialize(Http2StreamContext context) { diff --git a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Connection.cs b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Connection.cs index e0f300839104..04f92469f3f4 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Connection.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Connection.cs @@ -35,9 +35,9 @@ internal sealed class Http3Connection : IHttp3StreamLifetimeHandler, IRequestPro // so start highest opened request stream ID at -4. private const long DefaultHighestOpenedRequestStreamId = -4; - private readonly object _sync = new(); + private readonly Lock _sync = new(); private readonly HttpMultiplexedConnectionContext _context; - private readonly object _protocolSelectionLock = new(); + private readonly Lock _protocolSelectionLock = new(); private readonly StreamCloseAwaitable _streamCompletionAwaitable = new(); private readonly IProtocolErrorCodeFeature _errorCodeFeature; private readonly Dictionary? _webtransportSessions; diff --git a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3ControlStream.cs b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3ControlStream.cs index dbd99d838a0e..473bdcee6fd4 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3ControlStream.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3ControlStream.cs @@ -28,7 +28,7 @@ internal abstract class Http3ControlStream : IHttp3Stream, IThreadPoolWorkItem private readonly Http3RawFrame _incomingFrame = new Http3RawFrame(); private volatile int _isClosed; private long _headerType; - private readonly object _completionLock = new(); + private readonly Lock _completionLock = new(); private bool _haveReceivedSettingsFrame; private StreamCompletionFlags _completionState; diff --git a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3FrameWriter.cs b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3FrameWriter.cs index 44ade9362ea1..4eb71e9641e6 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3FrameWriter.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3FrameWriter.cs @@ -29,7 +29,7 @@ internal sealed class Http3FrameWriter private const int MaxDataFrameSize = 16 * 1024; private const int HeaderBufferSize = 16 * 1024; - private readonly object _writeLock = new object(); + private readonly Lock _writeLock = new(); private readonly int _maxTotalHeaderSize; private readonly ConnectionContext _connectionContext; diff --git a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3OutputProducer.cs b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3OutputProducer.cs index ac8307de6e46..95bb906a8289 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3OutputProducer.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3OutputProducer.cs @@ -24,7 +24,7 @@ internal sealed class Http3OutputProducer : IHttpOutputProducer, IHttpOutputAbor private readonly Pipe _pipe; private readonly PipeWriter _pipeWriter; private readonly PipeReader _pipeReader; - private readonly object _dataWriterLock = new object(); + private readonly Lock _dataWriterLock = new(); private ValueTask _dataWriteProcessingTask; private bool _startedWritingDataFrames; private bool _streamCompleted; diff --git a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs index ccf595d7f89f..812df98d8bed 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs @@ -56,7 +56,7 @@ internal abstract partial class Http3Stream : HttpProtocol, IHttp3Stream, IHttpS private Http3MessageBody? _messageBody; private readonly ManualResetValueTaskSource _appCompletedTaskSource = new(); - private readonly object _completionLock = new(); + private readonly Lock _completionLock = new(); protected RequestHeaderParsingState _requestHeaderParsingState; protected readonly Http3RawFrame _incomingFrame = new(); diff --git a/src/Servers/Kestrel/Core/src/Internal/HttpConnection.cs b/src/Servers/Kestrel/Core/src/Internal/HttpConnection.cs index 7538e1df1e26..9fa76f9d2472 100644 --- a/src/Servers/Kestrel/Core/src/Internal/HttpConnection.cs +++ b/src/Servers/Kestrel/Core/src/Internal/HttpConnection.cs @@ -30,7 +30,7 @@ internal sealed class HttpConnection : ITimeoutHandler private readonly TimeProvider _timeProvider; private readonly TimeoutControl _timeoutControl; - private readonly object _protocolSelectionLock = new object(); + private readonly Lock _protocolSelectionLock = new(); private ProtocolSelectionState _protocolSelectionState = ProtocolSelectionState.Initializing; private Http1Connection? _http1Connection; diff --git a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelConnection.cs b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelConnection.cs index 045a4e2e0505..8a2f7635b659 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelConnection.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelConnection.cs @@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; internal abstract class KestrelConnection : IConnectionHeartbeatFeature, IConnectionCompleteFeature, IConnectionLifetimeNotificationFeature, IConnectionMetricsContextFeature { private List<(Action handler, object state)>? _heartbeatHandlers; - private readonly object _heartbeatLock = new object(); + private readonly Lock _heartbeatLock = new(); private Stack, object>>? _onCompleted; private bool _completed; diff --git a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/PipeWriterHelpers/ConcurrentPipeWriter.cs b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/PipeWriterHelpers/ConcurrentPipeWriter.cs index 2ca58149e7e3..bd625063782e 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/PipeWriterHelpers/ConcurrentPipeWriter.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/PipeWriterHelpers/ConcurrentPipeWriter.cs @@ -21,7 +21,7 @@ internal sealed class ConcurrentPipeWriter : PipeWriter private static readonly Exception _successfullyCompletedSentinel = new UnreachableException(); - private readonly object _sync; + private readonly Lock _sync; private readonly PipeWriter _innerPipeWriter; private readonly MemoryPool _pool; private readonly BufferSegmentStack _bufferSegmentPool = new BufferSegmentStack(InitialSegmentPoolSize); @@ -50,7 +50,7 @@ internal sealed class ConcurrentPipeWriter : PipeWriter // If an Complete() is called while a flush is in progress, we clean up after the flush loop completes, and call Complete() on the inner PipeWriter. private Exception? _completeException; - public ConcurrentPipeWriter(PipeWriter innerPipeWriter, MemoryPool pool, object sync) + public ConcurrentPipeWriter(PipeWriter innerPipeWriter, MemoryPool pool, Lock sync) { _innerPipeWriter = innerPipeWriter; _pool = pool; diff --git a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/TimeoutControl.cs b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/TimeoutControl.cs index ba907abd7c5b..165f2c555b60 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/TimeoutControl.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/TimeoutControl.cs @@ -16,7 +16,7 @@ internal sealed class TimeoutControl : ITimeoutControl, IConnectionTimeoutFeatur private long _lastTimestamp; private long _timeoutTimestamp = long.MaxValue; - private readonly object _readTimingLock = new object(); + private readonly Lock _readTimingLock = new(); private MinDataRate? _minReadRate; private long _minReadRateGracePeriodTicks; private bool _readTimingEnabled; @@ -28,7 +28,7 @@ internal sealed class TimeoutControl : ITimeoutControl, IConnectionTimeoutFeatur private int _concurrentIncompleteRequestBodies; private int _concurrentAwaitingReads; - private readonly object _writeTimingLock = new object(); + private readonly Lock _writeTimingLock = new(); private int _concurrentAwaitingWrites; private long _writeTimingTimeoutTimestamp; @@ -142,8 +142,7 @@ private void CheckForReadDataRateTimeout(long timestamp) private void CheckForWriteDataRateTimeout(long timestamp) { - var timeout = false; - + bool timeout; lock (_writeTimingLock) { // Assume overly long tick intervals are the result of server resource starvation. diff --git a/src/Servers/Kestrel/Core/test/ConcurrentPipeWriterTests.cs b/src/Servers/Kestrel/Core/test/ConcurrentPipeWriterTests.cs index 48f8777bea70..2c7c58d585ff 100644 --- a/src/Servers/Kestrel/Core/test/ConcurrentPipeWriterTests.cs +++ b/src/Servers/Kestrel/Core/test/ConcurrentPipeWriterTests.cs @@ -25,7 +25,7 @@ public async Task PassthroughIfAllFlushesAreAwaited() new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously), }; - var sync = new object(); + var sync = new Lock(); var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray); var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool, sync); @@ -89,7 +89,7 @@ public async Task QueuesIfFlushIsNotAwaited() new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously), }; - var sync = new object(); + var sync = new Lock(); var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray); var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool, sync); var flushTask0 = default(ValueTask); @@ -186,7 +186,7 @@ public async Task KeepsQueueIfInnerFlushFinishesBetweenGetMemoryAndAdvance() new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously), }; - var sync = new object(); + var sync = new Lock(); var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray); var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool, sync); var memory = default(Memory); @@ -270,7 +270,7 @@ public async Task CompleteFlushesQueuedBytes() new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously), }; - var sync = new object(); + var sync = new Lock(); var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray); var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool, sync); var memory = default(Memory); @@ -338,7 +338,7 @@ public async Task CancelPendingFlushInterruptsFlushLoop() new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously), }; - var sync = new object(); + var sync = new Lock(); var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray); var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool, sync); var flushTask0 = default(ValueTask); diff --git a/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeConnection.cs b/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeConnection.cs index f3c56b4c8e07..5f41cf425aab 100644 --- a/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeConnection.cs +++ b/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeConnection.cs @@ -25,7 +25,7 @@ internal sealed class NamedPipeConnection : TransportConnection, IConnectionName private bool _connectionShutdown; private bool _streamDisconnected; private Exception? _shutdownReason; - private readonly object _shutdownLock = new object(); + private readonly Lock _shutdownLock = new(); // Internal for testing. internal Task _receivingTask = Task.CompletedTask; diff --git a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs index 9fd38e0a8bfa..58b0c2c5c0a9 100644 --- a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs +++ b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs @@ -20,9 +20,9 @@ internal partial class QuicConnectionContext : TransportMultiplexedConnection private bool _streamPoolHeartbeatInitialized; // Ticks updated once per-second in heartbeat event. private long _heartbeatTimestamp; - private readonly object _poolLock = new object(); + private readonly Lock _poolLock = new(); - private readonly object _shutdownLock = new object(); + private readonly Lock _shutdownLock = new(); private readonly QuicConnection _connection; private readonly QuicTransportContext _context; private readonly ILogger _log; diff --git a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs index 6d7fd3b3c777..02abcea565bf 100644 --- a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs +++ b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs @@ -40,7 +40,7 @@ internal partial class QuicStreamContext : TransportConnection, IPooledStream, I private bool _streamClosed; private bool _serverAborted; private bool _clientAbort; - private readonly object _shutdownLock = new object(); + private readonly Lock _shutdownLock = new(); public QuicStreamContext(QuicConnectionContext connection, QuicTransportContext context) { diff --git a/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs b/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs index f60b8ca9a206..e93c929f0a5b 100644 --- a/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs +++ b/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs @@ -22,7 +22,7 @@ internal sealed partial class SocketConnection : TransportConnection private readonly IDuplexPipe _originalTransport; private readonly CancellationTokenSource _connectionClosedTokenSource = new CancellationTokenSource(); - private readonly object _shutdownLock = new object(); + private readonly Lock _shutdownLock = new(); private volatile Exception? _shutdownReason; private Task? _sendingTask; private Task? _receivingTask; diff --git a/src/Servers/Kestrel/perf/Microbenchmarks/Mocks/MockHttpContextFactory.cs b/src/Servers/Kestrel/perf/Microbenchmarks/Mocks/MockHttpContextFactory.cs index 1327b4262120..6d3bad3efbc5 100644 --- a/src/Servers/Kestrel/perf/Microbenchmarks/Mocks/MockHttpContextFactory.cs +++ b/src/Servers/Kestrel/perf/Microbenchmarks/Mocks/MockHttpContextFactory.cs @@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Microbenchmarks; public class MockHttpContextFactory : IHttpContextFactory { - private readonly object _lock = new object(); + private readonly Lock _lock = new(); private readonly Queue _cache = new Queue(); public HttpContext Create(IFeatureCollection featureCollection) diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/EventSourceTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/EventSourceTests.cs index 93a6c3e4e9ba..05f528a2f379 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/EventSourceTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/EventSourceTests.cs @@ -423,7 +423,7 @@ private class TestEventListener : EventListener private readonly ConcurrentQueue _events = new ConcurrentQueue(); private readonly ILogger _logger; - private readonly object _disposeLock = new object(); + private readonly Lock _disposeLock = new(); private bool _disposed; public TestEventListener() diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2TestBase.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2TestBase.cs index b0a921b13830..e4d452b91aa0 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2TestBase.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2TestBase.cs @@ -131,7 +131,7 @@ protected static IEnumerable> ReadRateRequestHeader protected readonly Dictionary _decodedHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase); protected readonly RequestFields _receivedRequestFields = new RequestFields(); protected readonly HashSet _abortedStreamIds = new HashSet(); - protected readonly object _abortedStreamIdsLock = new object(); + protected readonly Lock _abortedStreamIdsLock = new(); protected readonly TaskCompletionSource _closingStateReached = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); protected readonly TaskCompletionSource _closedStateReached = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); diff --git a/src/Shared/ServerInfrastructure/DuplexPipeStreamAdapter.cs b/src/Shared/ServerInfrastructure/DuplexPipeStreamAdapter.cs index aa2fd03a6d9a..668e18577254 100644 --- a/src/Shared/ServerInfrastructure/DuplexPipeStreamAdapter.cs +++ b/src/Shared/ServerInfrastructure/DuplexPipeStreamAdapter.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal; internal class DuplexPipeStreamAdapter : DuplexPipeStream, IDuplexPipe where TStream : Stream { private bool _disposed; - private readonly object _disposeLock = new object(); + private readonly Lock _disposeLock = new(); public DuplexPipeStreamAdapter(IDuplexPipe duplexPipe, Func createStream) : this(duplexPipe, new StreamPipeReaderOptions(leaveOpen: true), new StreamPipeWriterOptions(leaveOpen: true), createStream)