Repository navigation
Fix high CPU usage in case AmqpNetLite.Core 2.4.8+ - #102
Conversation
|
Maybe this pull request can be combined with an upgrade to amqpnetlite 2.5.x? @Havret is this something which would make sense or is this something which should be a different pull request? |
I think it would make better sense to bump amqpnetlite in a separate PR. |
There was a problem hiding this comment.
Pull request overview
Adjusts AMQP Open handshake negotiation to avoid overflowing IdleTimeOut when using AMQPNetLite.Core >= 2.4.8 (where the default IdleTimeout can be -1), preventing incorrect heartbeat scheduling that can lead to high CPU usage during idle periods.
Changes:
- Guarded the
Open.IdleTimeOutassignment to only cast positive values touint, otherwise send0.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Since AmqpNetLite v2.4.8 the IdleTimeOut supports negative values. The new default value is -1. If the timeout of -1 get casted to uint it becames uint.MaxValue. This lead to wrong working heartbeat and high CPU usage.
|
Dear @Havret , thank you for your review. I applied the changes and updated the PR. |
Havret
left a comment
There was a problem hiding this comment.
Will need some follow up work. But I will take care of it with a separate PR.
Since AMQPNetLite v2.4.8 the IdleTimeOut supports negative values.
The new default value is -1.
If the timeout of -1 get casted to uint it becames uint.MaxValue.
This lead to wrong working heartbeat and high CPU usage.
Below a detailled explaination about the issue and how to reproduce it.
How to reproduce the issue
This issue occurs every time you attempt to use a newer version of the AmqpNetLite.Core. The Nuget package for ApacheNMS.AMQP requires AMQPNetLite.Core >= 2.4.3 .
Up to version 2.4.7, there is no meaningful difference in the behavior of the library.
Starting on the version 2.4.8 and newer, I detected an high CPU consumption during idle.
This can be easily reproduced in this way:
example.csproj
Program.cs
Apart of some initial activity, you see 0% CPU usage
The CPU goes high up to 20% while the program is actually be in idle the whole time (no producers neither consumers)
How/why this occurs
The apache.nsm.amqp library performs a casting from the default value (if not overriden):

this lead to an unintended value in case of -1, the new default value fo this setting:

Proposed solution
Since the underlaying library AmqpNetLite.Core changed the default value and its behavior in this matter, I think the apache.nsm.amqp should perfom a casting only if the IdleTimeOut is not negative and pass
0elsewhere. This will allow to keep a low dependency requirement, remain backward capable and also fit newer version of AMQPNetLite.Core.