Skip to content

Commit

Permalink
Move release to finally
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra committed Nov 17, 2020
1 parent f6b5da4 commit 3eaa757
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,28 @@ public SNISslStream(Stream innerStream, bool leaveInnerStreamOpen, RemoteCertifi
// Prevent the ReadAsync's collision by running task in Semaphore Slim
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return _readAsyncQueueSemaphore.WaitAsync().ContinueWith<int>(t => base.ReadAsync(buffer, offset, count, cancellationToken).Result)
.ContinueWith(t => _readAsyncQueueSemaphore.Release(t.Result));
try
{
return _readAsyncQueueSemaphore.WaitAsync()
.ContinueWith<int>(_ => base.ReadAsync(buffer, offset, count, cancellationToken).GetAwaiter().GetResult());
}
finally
{
_readAsyncQueueSemaphore.Release();
}
}

// Prevent the WriteAsync's collision by running task in Semaphore Slim
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return _writeAsyncQueueSemaphore.WaitAsync().ContinueWith(t => base.WriteAsync(buffer, offset, count, cancellationToken))
.ContinueWith(t => _writeAsyncQueueSemaphore.Release());
try
{
return _writeAsyncQueueSemaphore.WaitAsync().ContinueWith(_ => base.WriteAsync(buffer, offset, count, cancellationToken));
}
finally
{
_writeAsyncQueueSemaphore.Release();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2170,10 +2170,9 @@ public Task WaitAsync()
return tcs.Task;
}

public int Release(int i = default)
public void Release()
{
_semaphore.Release();
return i;
}
}

Expand Down

0 comments on commit 3eaa757

Please sign in to comment.