Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes "InvalidOperationException" errors by performing async operations in SemaphoreSlim #796

Merged
merged 20 commits into from
Nov 19, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public SNISslStream(Stream innerStream, bool leaveInnerStreamOpen, RemoteCertifi
}

// Prevent the ReadAsync's collision by running task in Semaphore Slim
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
await _readAsyncQueueSemaphore.WaitAsync().ConfigureAwait(false);
try
{
return _readAsyncQueueSemaphore.WaitAsync()
.ContinueWith(_ => base.ReadAsync(buffer, offset, count, cancellationToken).GetAwaiter().GetResult());
return await base.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
}
finally
{
Expand All @@ -40,11 +40,12 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
}

// Prevent the WriteAsync's collision by running task in Semaphore Slim
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
await _writeAsyncQueueSemaphore.WaitAsync().ConfigureAwait(false);
try
{
return _writeAsyncQueueSemaphore.WaitAsync().ContinueWith(_ => base.WriteAsync(buffer, offset, count, cancellationToken));
await base.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
}
finally
{
Expand All @@ -68,12 +69,12 @@ public SNINetworkStream(Socket socket, bool ownsSocket) : base(socket, ownsSocke
}

// Prevent the ReadAsync's collision by running task in Semaphore Slim
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
await _readAsyncQueueSemaphore.WaitAsync().ConfigureAwait(false);
try
{
return _readAsyncQueueSemaphore.WaitAsync()
.ContinueWith(_ => base.ReadAsync(buffer, offset, count, cancellationToken).GetAwaiter().GetResult());
return await base.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
}
finally
{
Expand All @@ -82,11 +83,12 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
}

// Prevent the WriteAsync's collision by running task in Semaphore Slim
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
await _writeAsyncQueueSemaphore.WaitAsync().ConfigureAwait(false);
try
{
return _writeAsyncQueueSemaphore.WaitAsync().ContinueWith(_ => base.WriteAsync(buffer, offset, count, cancellationToken));
await base.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
}
finally
{
Expand Down