kerryjiang / SuperSocket

SuperSocket is a light weight, cross platform and extensible socket server application framework.
Apache License 2.0
3.93k stars 1.15k forks source link

SendAsync SemaphoreSlim Exception #748

Closed ygr757326 closed 3 weeks ago

ygr757326 commented 1 month ago

1725873682653

MyPackageHandler SendAsync error:Adding the specified count to the semaphore would cause it to exceed its maximum count. System.Threading.SemaphoreFullException: Adding the specified count to the semaphore would cause it to exceed its maximum count. at System.Threading.SemaphoreSlim.Release(Int32 releaseCount) at SuperSocket.Connection.PipeConnectionBase.SendAsync[TPackage](IPackageEncoder`1 packageEncoder, TPackage package, CancellationToken cancellationToken)

kerryjiang commented 1 month ago

That's strange, I will take a look at this problem. That would be great if you have a unit test to reproduce this problem.

Helder-Henderson commented 1 month ago

Good morning

I had same problem yesterday...

In my cenario i throw some exceptions on Command - ExecuteAsync.

2024-09-21 23:24:53.234 -03:00 [ERR] Session[893c4fbd-f327-4683-aa55-1f4c32183aaa]: session exception. SuperSocket.PackageHandlingException1[SuperSocket.WebSocket.WebSocketPackage]: Session 893c4fbd-f327-4683-aa55-1f4c32183aaa got an error when handle a package. ---> System.Threading.SemaphoreFullException: Adding the specified count to the semaphore would cause it to exceed its maximum count. at System.Threading.SemaphoreSlim.Release(Int32 releaseCount) at SuperSocket.Connection.PipeConnectionBase.SendAsync[TPackage](IPackageEncoder1 packageEncoder, TPackage package, CancellationToken cancellationToken) at GerenciadorFacial.Commands.RegistrarEquipamento.ExecuteAsync(EquipamentoSession session, CustomDataPackage package, CancellationToken cancellationToken) in C:\dev\<>\<>\<>\Commands\RegistrarEquipamento.cs:line 32 at SuperSocket.Command.AsyncCommandWrap4.ExecuteAsync(TAppSession session, TPackageInfo package, CancellationToken cancellationToken) at SuperSocket.Command.CommandMiddleware3.CommandSet1.ExecuteAsync(IAppSession session, TPackageInfo package, CancellationToken cancellationToken) at SuperSocket.Command.CommandMiddleware3.HandlePackage(IAppSession session, TPackageInfo package, CancellationToken cancellationToken) at SuperSocket.WebSocket.Server.WebSocketPackageHandler.Handle(IAppSession session, WebSocketPackage package, CancellationToken cancellationToken) at SuperSocket.Server.PackageHandlingSchedulerBase`1.HandlePackageInternal(IAppSession session, TPackageInfo package, CancellationToken cancellationToken) --- End of inner exception stack trace ---

Helder-Henderson commented 1 month ago

@kerryjiang is possible to implement one sample using JsonAsyncCommand ?

Basic basic, only for understand it.

kerryjiang commented 1 month ago

Found the problem and working on a fix.

kerryjiang commented 1 month ago

Pushed a fix.

With this fix, you would get TaskCancellationException in the same situation.

I suggest you check the cancellationToken is triggered before you call SendAsync(*).

if (!cancellationToken.IsCancellationRequested)
    await session.SendAsync(xxxx, cancellationToken);

cancellationToken.IsCancellationRequested == true means the connection is closed or is being closed.

@ygr757326 @Helder-Henderson

kerryjiang commented 1 month ago

@kerryjiang is possible to implement one sample using JsonAsyncCommand ?

Basic basic, only for understand it.

@Helder-Henderson Do you use token in async json command? Token allows your client to talk with server in this order: Request A, Request B, Request C, Response C, Response A, Response B.

Helder-Henderson commented 1 month ago

No no, sorry, I found implementation about JsonCommands on unit tests...

My questions was not related to the issue, sorry !

kerryjiang commented 1 month ago

Released SuperSocket 2.0.0-beta.27 to fix this issue: https://www.nuget.org/packages/SuperSocket/2.0.0-beta.27