Skip to content

Commit

Permalink
fix SemaphoreFullException in SendAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Sep 29, 2024
1 parent 7fad98e commit 23e2dda
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/SuperSocket.Connection/PipeConnectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,19 @@ private void CheckConnectionOpen()

public override async ValueTask SendAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
var sendLockAcquired = false;

try
{
await SendLock.WaitAsync(cancellationToken).ConfigureAwait(false);
sendLockAcquired = true;
WriteBuffer(OutputWriter, buffer);
await OutputWriter.FlushAsync(cancellationToken).ConfigureAwait(false);
}
finally
{
SendLock.Release();
if (sendLockAcquired)
SendLock.Release();
}
}

Expand All @@ -186,29 +190,37 @@ private void WriteBuffer(PipeWriter writer, ReadOnlyMemory<byte> buffer)

public override async ValueTask SendAsync<TPackage>(IPackageEncoder<TPackage> packageEncoder, TPackage package, CancellationToken cancellationToken = default)
{
var sendLockAcquired = false;

try
{
await SendLock.WaitAsync(cancellationToken).ConfigureAwait(false);
sendLockAcquired = true;
WritePackageWithEncoder<TPackage>(OutputWriter, packageEncoder, package);
await OutputWriter.FlushAsync(cancellationToken).ConfigureAwait(false);
}
finally
{
SendLock.Release();
if (sendLockAcquired)
SendLock.Release();
}
}

public override async ValueTask SendAsync(Action<PipeWriter> write, CancellationToken cancellationToken)
{
var sendLockAcquired = false;

try
{
await SendLock.WaitAsync(cancellationToken).ConfigureAwait(false);
sendLockAcquired = true;
write(OutputWriter);
await OutputWriter.FlushAsync(cancellationToken).ConfigureAwait(false);
}
finally
{
SendLock.Release();
if (sendLockAcquired)
SendLock.Release();
}
}

Expand Down

0 comments on commit 23e2dda

Please sign in to comment.