msgpack / msgpack-cli

MessagePack implementation for Common Language Infrastructure / msgpack.org[C#]
http://msgpack.org
Apache License 2.0
835 stars 175 forks source link

PackAsync & UnpackAsync not working when passing data continuously using for loop c# #342

Open pariventhan opened 4 years ago

pariventhan commented 4 years ago

Hi,

I am using TcpListener to transfer data from client to Server, This is service writing early 1000 lines with 4000 chars in one minutes.

Once I connected with server, code passing 1000 calls to server. I am using msgPack to send the data.

When I send the data in sync it's working fine, but If i am using Async, after 2 item, the channel got break. Following is the sample code, basically we are using this for application logging, so from the application we are passing log information using msgPack.

`using (var stream = _tcpClient.GetStream()) {

                            for (int itemCount = 0; itemCount < 1000; itemCount++)
                            {

                                try
                                {
                                    await _serializer.PackAsync(stream, logobj);
                                    //await stream.FlushAsync();
                                }
                                catch (Exception)
                                {

                                }
                            }

                    }`

In the server code am getting below error

System.Runtime.Serialization.SerializationException: Unpacker is not in the array header at offset 0. The stream may not be array.
   at MsgPack.Serialization.SerializationExceptions.ThrowIsNotArrayHeader(Unpacker unpacker)
   at MsgPack.Serialization.EmittingSerializers.Generated.System_Tuple_3_System_Int32_System_Int32_System_String_Serializer2.UnpackFromAsyncCore(Unpacker , CancellationToken )
   at MsgPack.Serialization.MessagePackSerializer`1.UnpackFromAsync(Unpacker unpacker, CancellationToken cancellationToken)
   at MsgPack.Serialization.MessagePackSerializer`1.UnpackAsync(Stream stream, CancellationToken cancellationToken)
yfakariya commented 4 years ago

Thank you for feedback and sorry for delayed response.

I recognized that packer wrote broken binary because of the exception. I think it is important for logging to have error resistance unpacking, but I have no good idea for it now. I guess we cannot detect broken range automatically.

It is welcome to discuss the solution, or point out my misunderstanding. Thank you.