Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Serialized to wrong size" exception when call BasicPublish #1620

Closed
AugustoDeveloper opened this issue Jun 28, 2024 · 9 comments
Closed

"Serialized to wrong size" exception when call BasicPublish #1620

AugustoDeveloper opened this issue Jun 28, 2024 · 9 comments
Assignees

Comments

@AugustoDeveloper
Copy link

AugustoDeveloper commented Jun 28, 2024

Describe the bug

I updated the RabbitMQ.Client library to 6.8.1, before it was 5.1.1, and for some reason i receive some exceptions, but the InvalidOperationException occurrs more frequently, with stack trace

Message: Serialized to wrong size, expect 14068, offset 14056
     Source: RabbitMQ.Client
     StackTrace:    at RabbitMQ.Client.Impl.SessionBase.Transmit(OutgoingCommand& cmd)
   at RabbitMQ.Client.Framing.Impl.Model._Private_BasicPublish(String exchange, String routingKey, Boolean mandatory, IBasicProperties basicProperties, ReadOnlyMemory`1 body)
   at RabbitMQ.Client.Impl.ModelBase.BasicPublish(String exchange, String routingKey, Boolean mandatory, IBasicProperties basicProperties, ReadOnlyMemory`1 body)

In 4 hours range of time, this exception occurs 25 times. Unfortunately, i can't repro this error, but i needed to try if you guys can help me with some insights.

Reproduction steps

Well, thats the part i really confused, i can't reproduce, yet.

Expected behavior

In version 5.1.1 of RabbitMQ.Client this erros does not occurrs.

Additional context

when this error is thrown my app reconnect to rabbitmq and retry publish for 3 times and it works, the message is published

@AugustoDeveloper
Copy link
Author

I had this much of errors occuring right now:
image

@lukebakken lukebakken self-assigned this Jun 28, 2024
@lukebakken lukebakken added this to the 6.9.0 milestone Jun 28, 2024
@lukebakken
Copy link
Contributor

For reference - https://groups.google.com/g/rabbitmq-users/c/oW1_PMRlbSA

@AugustoDeveloper without a way to reproduce this issue, it's unlikely I will be able to fix it. If this issue were common, we would hear about it all of the time, yet it has only been reported twice.

To start, you MUST let us know the following:

  • RabbitMQ version, and Erlang version
  • .NET version you are using

If you can share ALL of your code, I may be able to see what the issue is.

@AugustoDeveloper
Copy link
Author

For reference - https://groups.google.com/g/rabbitmq-users/c/oW1_PMRlbSA

@AugustoDeveloper without a way to reproduce this issue, it's unlikely I will be able to fix it. If this issue were common, we would hear about it all of the time, yet it has only been reported twice.

To start, you MUST let us know the following:

  • RabbitMQ version, and Erlang version
  • .NET version you are using

If you can share ALL of your code, I may be able to see what the issue is.

RabbitMQ Version: 3.12.1
Erlang Version: 26.0.2
.Net Version: 8.0.203

Well, i cant share everything, but i can show the method that calls BasicPublish

        private Task<PublishResult> PublishAsync(byte[] messageBody, string routingKey, CancellationToken cancellation)
        {
            return Task.Run(() =>
            {
                try
                {
                    CreateBasicPropperties();

                    cancellation.ThrowIfCancellationRequested();

                    CurrentModel
                        .BasicPublish(Configuration.Exchange, routingKey, mandatory: false, Properties, messageBody);

                    return PublishResult.Published;
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, nameof(PublishAsync));

                    return PublishResult.Unpublished(messageBody, Configuration.Exchange, routingKey);
                }
            });
        }
...
        protected void CreateBasicPropperties()
        {
            if (Properties is null)
            {
                lock (lockProperties)
                {
                    if (Properties is null)
                    {
                        Properties = CurrentModel.CreateBasicProperties();
                        Properties.DeliveryMode = PersistentDeliveryMode;
                    }
                }
            }
        }

@lukebakken
Copy link
Contributor

IModel instances are not thread safe. Create a new one in your PublishAsync method.

Also, if you would like to really help out, please test your code with the latest version 7 RC, which supports async methods natively:

https://www.nuget.org/packages/RabbitMQ.Client/7.0.0-rc.3

Please note that IChannel instances are still not thread safe. IConnection is.

@AugustoDeveloper
Copy link
Author

AugustoDeveloper commented Jun 28, 2024

IModel instances are not thread safe. Create a new one in your PublishAsync method.

Also, if you would like to really help out, please test your code with the latest version 7 RC, which supports async methods natively:

https://www.nuget.org/packages/RabbitMQ.Client/7.0.0-rc.3

Please note that IChannel instances are still not thread safe. IConnection is.

Ok, i will try, thank you very much. But just don't figure it out why in the version 5.1.1 of library this error not ocurring at all. Can you have any idea that's happen?

The code still the same

@lukebakken
Copy link
Contributor

Ok, i will try, thank you very much. But just don't figure it out why in the version 5.1.1 of library this error not ocurring at all. Can you have any idea that's happen?

Something changed internally. IModel has never been guaranteed to be thread-safe, it just happened to work in version 5.

@AugustoDeveloper
Copy link
Author

IModel instances are not thread safe. Create a new one in your PublishAsync method.

Also, if you would like to really help out, please test your code with the latest version 7 RC, which supports async methods natively:

https://www.nuget.org/packages/RabbitMQ.Client/7.0.0-rc.3

Please note that IChannel instances are still not thread safe. IConnection is.

That's works for me, thank you very much @lukebakken

@lukebakken
Copy link
Contributor

That's works for me,

I'm not sure what you mean -

  • You created a new IModel instance?
  • You were able to test version 7.0.0-rc.3?

@lukebakken lukebakken removed the bug label Jul 1, 2024
@lukebakken lukebakken removed this from the 6.9.0 milestone Jul 1, 2024
@lukebakken lukebakken closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2024
@AugustoDeveloper
Copy link
Author

AugustoDeveloper commented Jul 1, 2024

Sorry for not specified what fixed my problem, my problem was fixed using a new instance of IModel inside of async method PublishAsync

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants