Skip to content

CSHARP-5621: Fix BinaryConnectionTests.ReceiveMessage_should_throw_a_FormatException fails with TimeoutException #1715

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

medhatiwari
Copy link
Contributor

Jira Ticket

This PR fixes a test case failure in BinaryConnectionTests.ReceiveMessage_should_throw_a_FormatException_when_message_is_an_invalid_size when run on Big Endian systems like s390x.

Root Cause:
The test uses BitConverter.GetBytes(length) which encodes the integer in system-native endianness. MongoDB’s wire protocol, however, requires that all integer fields be little-endian. When the test runs on a Big Endian system, the message size is encoded incorrectly, leading the driver to misinterpret the message size and wait for more data, resulting in a TimeoutException.

Fix:
fix the test by reversing the byte array explicitly if BitConverter.IsLittleEndian == false. This ensures consistent little-endian encoding regardless of the platform.

Alternative:
Instead of using BitConverter and reversing bytes manually, we could replace the logic with BinaryPrimitives.WriteInt32LittleEndian, which guarantees correct little-endian encoding

Span<byte> bytes = stackalloc byte[4];
BinaryPrimitives.WriteInt32LittleEndian(bytes, length);
stream.Write(bytes);

cc: @giritrivedi

…ormatException fails with TimeoutException

Signed-off-by: Medha Tiwari <medhavns1@gmail.com>
@medhatiwari medhatiwari requested a review from a team as a code owner June 19, 2025 09:55
@medhatiwari medhatiwari requested review from papafe and removed request for a team June 19, 2025 09:55
@papafe papafe added the chore Label to hide PR from generated Release Notes label Jun 23, 2025
@@ -374,6 +374,10 @@ public void ReceiveMessage_should_throw_a_FormatException_when_message_is_an_inv
using (var stream = new BlockingMemoryStream())
{
var bytes = BitConverter.GetBytes(length);
if (!BitConerter.IsLittleEndian)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a spelling mistake here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, typo indeed, fixed it

@medhatiwari medhatiwari requested a review from papafe June 24, 2025 05:30
@papafe papafe requested a review from BorisDog June 24, 2025 16:25
@medhatiwari medhatiwari changed the title CSHARP-5621 Fix BinaryConnectionTests.ReceiveMessage_should_throw_a_FormatException fails with TimeoutException CSHARP-5621: Fix BinaryConnectionTests.ReceiveMessage_should_throw_a_FormatException fails with TimeoutException Jun 24, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
chore Label to hide PR from generated Release Notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants