kerryjiang / SuperSocket

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

Writing is not allowed after writer was completed. #512

Open ozgun77 opened 3 years ago

ozgun77 commented 3 years ago

Hello,

I'm getting lots of "Writing is not allowed after writer was completed." exception when sending message in production. What can cause this error?

Thank you.

chucklu commented 3 years ago

https://github.com/kerryjiang/SuperSocket/issues/362

chucklu commented 3 years ago

https://github.com/kerryjiang/SuperSocket/issues/263

ozgun77 commented 3 years ago

"任意客户端关闭后服务端尝试发送数据时抛出异常应合理,业务代理有责任处理这样的一场,也可以在发送之前检查一下连接状态。"

I'm checking the session state before sending message, but until I send message session is closed I guess. Do you have any suggestions to overcome this?

ozgun77 commented 3 years ago

By the way this is StackTraceString:

at System.IO.Pipelines.ThrowHelper.ThrowInvalidOperationException_NoWritingAllowed()\n at System.IO.Pipelines.Pipe.GetSpan(Int32 sizeHint)\n at SuperSocket.WebSocket.WebSocketEncoder.EncodeSingleFragment(IBufferWriter1 writer, Byte opCode, Int32 expectedHeadLength, ReadOnlySpan1 text)\n at SuperSocket.WebSocket.WebSocketEncoder.Encode(IBufferWriter1 writer, WebSocketPackage pack)\n at SuperSocket.Channel.PipeChannel1.WritePackageWithEncoder[TPackage](IBufferWriter1 writer, IPackageEncoder1 packageEncoder, TPackage package)\n at SuperSocket.Channel.PipeChannel1.SendAsync[TPackage](IPackageEncoder1 packageEncoder, TPackage package)\n at SuperSocket.WebSocket.Server.WebSocketPackageHandler.Handle(IAppSession session, WebSocketPackage package)\n at SuperSocket.Server.PackageHandlingSchedulerBase`1.HandlePackageInternal(IAppSession session, TPackageInfo package)

Does this exception definitely occurs because of trying to send message after client closes connection?

kerryjiang commented 3 years ago

@ozgun77 which version do you use?

ozgun77 commented 3 years ago

@kerryjiang SuperSocket.WebSocket.Server 2.0.0-beta.8. Sometimes these errors getting too much, 10-15k errors for a minute.

kerryjiang commented 3 years ago

@ozgun77 Could you upgrade to the latest beta? And is there a way to reproduce this problem?

superttl commented 3 years ago

Category: SuperSocketService EventId: 0 Session[355474cf-2d0e-42d3-9ee4-a4bfdbe4e4c9]: session exception. Exception: SuperSocket.PackageHandlingException1[SuperSocket.ProtoBase.StringPackageInfo]: Session 355474cf-2d0e-42d3-9ee4-a4bfdbe4e4c9 got an error when handle a package. ---> System.InvalidOperationException: Writing is not allowed after writer was completed. at System.IO.Pipelines.ThrowHelper.ThrowInvalidOperationException_NoWritingAllowed() at SuperSocket.Channel.PipeChannel1.SendAsync(ReadOnlyMemory1 buffer) at WSDService.IoTServer.SendCommandAsync(IAppSession session, RequestCommand request) at WSDService.IoTServer.<>c__DisplayClass16_0.<<CreateServer>b__3>d.MoveNext() --- End of stack trace from previous location --- at SuperSocket.DelegatePackageHandler1.Handle(IAppSession session, TReceivePackageInfo package) at SuperSocket.Server.PackageHandlingSchedulerBase`1.HandlePackageInternal(IAppSession session, TPackageInfo package) --- End of inner exception stack trace ---

ozgun77 commented 3 years ago

I think this code in PipeChannel might causing this exception:

await Out.Writer.CompleteAsync().ConfigureAwait(false);

I think pipe is being closed before send task completed.

mago-pablo commented 2 years ago

I am getting a similar error:

Handle is already used by another Socket. at System.Net.Sockets.SocketAsyncEngine.RegisterCore(IntPtr socketHandle, SocketAsyncContext context) at System.Net.Sockets.SocketAsyncEngine.RegisterSocket(IntPtr socketHandle, SocketAsyncContext context) at System.Net.Sockets.SocketAsyncContext.Register() at System.Net.Sockets.SocketAsyncContext.OperationQueue1.StartAsyncOperation(SocketAsyncContext context, TOperation operation, Int32 observedSequenceNumber, CancellationToken cancellationToken) at System.Net.Sockets.SocketAsyncContext.ReceiveAsync(Memory1 buffer, SocketFlags flags, Int32& bytesReceived, Action5 callback, CancellationToken cancellationToken) at System.Net.Sockets.SocketAsyncEventArgs.DoOperationReceive(SafeSocketHandle handle, CancellationToken cancellationToken) at System.Net.Sockets.Socket.ReceiveAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ReceiveAsync(Socket socket, CancellationToken cancellationToken) at System.Net.Sockets.Socket.ReceiveAsync(Memory1 buffer, SocketFlags socketFlags, Boolean fromNetworkStream, CancellationToken cancellationToken) at System.Net.Sockets.SocketTaskExtensions.ReceiveAsync(Socket socket, Memory1 buffer, SocketFlags socketFlags, CancellationToken cancellationToken) at SuperSocket.Channel.TcpPipeChannel1.ReceiveAsync(Socket socket, Memory1 memory, SocketFlags socketFlags, CancellationToken cancellationToken) in C:\git_opensource\Supersocket\src\SuperSocket.Channel\TcpPipeChannel.cs:line 39 at SuperSocket.Channel.TcpPipeChannel1.FillPipeWithDataAsync(Memory1 memory, CancellationToken cancellationToken) in C:\git_opensource\Supersocket\src\SuperSocket.Channel\TcpPipeChannel.cs:line 34 at SuperSocket.Channel.PipeChannel1.FillPipeAsync(PipeWriter writer) in C:\git_opensource\Supersocket\src\SuperSocket.Channel\PipeChannel.cs:

mago-pablo commented 2 years ago

await Out.Writer.CompleteAsync().ConfigureAwait(false);

Is there something to change here for a high volume client enviorment?

mago-pablo commented 2 years ago

Doing further testing, with the same server instance, one in a windows server and a second in Ubuntu 20.04, it seems in Windows it has less ocurrence of the error and is more stable.