Skip to content

Commit 008f128

Browse files
authored
pool async state in SslStream (#69418)
* pool async state in SslStream * fix tests * remove GetAwaiter from test
1 parent 4e9ac49 commit 008f128

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/libraries/System.Net.Security/src/System/Net/Security/SslStream.IO.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Buffers;
5-
using System.ComponentModel;
65
using System.Diagnostics;
76
using System.IO;
7+
using System.Runtime.CompilerServices;
88
using System.Runtime.ExceptionServices;
99
using System.Security.Authentication;
1010
using System.Security.Cryptography.X509Certificates;
@@ -675,7 +675,7 @@ private bool HaveFullTlsFrame(out int frameSize)
675675
return _buffer.EncryptedLength >= frameSize;
676676
}
677677

678-
678+
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
679679
private async ValueTask<int> EnsureFullTlsFrameAsync<TIOAdapter>(CancellationToken cancellationToken)
680680
where TIOAdapter : IReadWriteAdapter
681681
{
@@ -760,6 +760,7 @@ private SecurityStatusPal DecryptData(int frameSize)
760760
return status;
761761
}
762762

763+
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
763764
private async ValueTask<int> ReadAsyncInternal<TIOAdapter>(Memory<byte> buffer, CancellationToken cancellationToken)
764765
where TIOAdapter : IReadWriteAdapter
765766
{

src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamNetworkStreamTest.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -961,19 +961,20 @@ public async Task SslStream_RandomSizeWrites_OK(int bufferSize, int readBufferSi
961961

962962
await TestConfiguration.WhenAllOrAnyFailedWithTimeout(t1, t2);
963963

964-
Task writer = Task.Run(() =>
964+
Task writer = Task.Run(async () =>
965965
{
966966
Memory<byte> data = new Memory<byte>(dataToCopy);
967967
while (data.Length > 0)
968968
{
969969
int writeLength = Math.Min(data.Length, writeBufferSize);
970970
if (useAsync)
971971
{
972-
server.WriteAsync(data.Slice(0, writeLength)).GetAwaiter().GetResult();
972+
await server.WriteAsync(data.Slice(0, writeLength));
973973
}
974974
else
975975
{
976976
server.Write(data.Span.Slice(0, writeLength));
977+
await Task.CompletedTask;
977978
}
978979

979980
data = data.Slice(Math.Min(writeBufferSize, data.Length));
@@ -982,7 +983,7 @@ public async Task SslStream_RandomSizeWrites_OK(int bufferSize, int readBufferSi
982983
server.ShutdownAsync().GetAwaiter().GetResult();
983984
});
984985

985-
Task reader = Task.Run(() =>
986+
Task reader = Task.Run(async () =>
986987
{
987988
Memory<byte> readBuffer = new Memory<byte>(dataReceived);
988989
int totalLength = 0;
@@ -992,11 +993,12 @@ public async Task SslStream_RandomSizeWrites_OK(int bufferSize, int readBufferSi
992993
{
993994
if (useAsync)
994995
{
995-
readLength = client.ReadAsync(readBuffer.Slice(totalLength, readBufferSize)).GetAwaiter().GetResult();
996+
readLength = await client.ReadAsync(readBuffer.Slice(totalLength, readBufferSize));
996997
}
997998
else
998999
{
9991000
readLength = client.Read(readBuffer.Span.Slice(totalLength, readBufferSize));
1001+
await Task.CompletedTask;
10001002
}
10011003

10021004
if (readLength == 0)

0 commit comments

Comments
 (0)