diff --git a/src/libraries/System.Net.Security/src/System/Net/Security/Pal.OSX/SafeDeleteSslContext.cs b/src/libraries/System.Net.Security/src/System/Net/Security/Pal.OSX/SafeDeleteSslContext.cs index 7ff7b26e7a5e62..4e2384544a62f0 100644 --- a/src/libraries/System.Net.Security/src/System/Net/Security/Pal.OSX/SafeDeleteSslContext.cs +++ b/src/libraries/System.Net.Security/src/System/Net/Security/Pal.OSX/SafeDeleteSslContext.cs @@ -20,6 +20,7 @@ internal sealed class SafeDeleteSslContext : SafeDeleteContext private const int OSStatus_errSSLWouldBlock = -9803; private const int InitialBufferSize = 2048; private readonly SafeSslHandle _sslContext; + private readonly object _lock = new object(); private ArrayBuffer _inputBuffer = new ArrayBuffer(InitialBufferSize); private ArrayBuffer _outputBuffer = new ArrayBuffer(InitialBufferSize); @@ -205,7 +206,7 @@ protected override void Dispose(bool disposing) SafeSslHandle sslContext = _sslContext; if (null != sslContext) { - lock (_sslContext) + lock (_lock) { _inputBuffer.Dispose(); _outputBuffer.Dispose(); @@ -228,7 +229,7 @@ private static unsafe int WriteToConnection(IntPtr connection, byte* data, void* // but if we were to pool the buffers we would have a potential use-after-free issue. try { - lock (context) + lock (context._lock) { ulong length = (ulong)*dataLength; Debug.Assert(length <= int.MaxValue); @@ -260,7 +261,7 @@ private static unsafe int ReadFromConnection(IntPtr connection, byte* data, void try { - lock (context) + lock (context._lock) { ulong toRead = (ulong)*dataLength; @@ -297,7 +298,7 @@ private static unsafe int ReadFromConnection(IntPtr connection, byte* data, void internal void Write(ReadOnlySpan buf) { - lock (_sslContext) + lock (_lock) { _inputBuffer.EnsureAvailableSpace(buf.Length); buf.CopyTo(_inputBuffer.AvailableSpan); @@ -309,7 +310,7 @@ internal void Write(ReadOnlySpan buf) internal void ReadPendingWrites(ref ProtocolToken token) { - lock (_sslContext) + lock (_lock) { if (_outputBuffer.ActiveLength == 0) { @@ -331,7 +332,7 @@ internal int ReadPendingWrites(byte[] buf, int offset, int count) Debug.Assert(count >= 0); Debug.Assert(count <= buf.Length - offset); - lock (_sslContext) + lock (_lock) { int limit = Math.Min(count, _outputBuffer.ActiveLength);