zeromq / netmq

A 100% native C# implementation of ZeroMQ for .NET
Other
2.93k stars 744 forks source link

Fix Division by Zero Error in FairQueueing Class When m_active is Zero #1088

Closed fales1488 closed 2 months ago

fales1488 commented 2 months ago

https://github.com/zeromq/netmq/blob/97da3b46ba768eb5050547276a9ef948741a9de4/src/NetMQ/Core/Patterns/Utils/FairQueueing.cs#L125

Description

There is a potential DivideByZeroException in the FairQueueing class of NetMQ when all pipes become deactivated (m_active becomes zero). This exception occurs specifically in the RecvPipe method during the modulo operation used for round-robin scheduling of the pipes.

Steps to Reproduce

  1. Create a scenario where all pipes are systematically deactivated, setting m_active to zero.
  2. Continue to receive messages or perform operations that lead to the modulo operation (m_current = (m_current + 1) % m_active).

Expected Behavior

The system should handle situations where m_active is zero without throwing a DivideByZeroException, possibly by skipping the modulo operation when no active pipes are available.

Actual Behavior

The system throws a DivideByZeroException when trying to perform the modulo operation with m_active set to zero due to all pipes being deactivated.

Proposed Solution

To prevent this issue, the modulo operation in the RecvPipe method should be conditionally performed only when m_active is greater than zero. Here is the suggested change:


if (!m_more)
    m_current = m_active > 0 ? (m_current + 1) % m_active : 0;