Skip to content

About the memory error problem of InjectApplicationMessage of MqttServer in client.WithCleanSession(false) #2113

@xljiulang

Description

@xljiulang

Describe the bug

When I was doing performance optimization extension for MqttServer's InjectApplicationMessage , I used ArrayPool<byte> memory and unexpectedly found that the test QoS_Tests.Preserve_Message_Order_For_Queued_Messages failed because I cleared and recycled poolPayload after await MqttServer.InjectApplicationMessage( poolPayload) . I think there is no problem with my operation timing, and this behavior should be allowed.

If I use the byte[] poolPayload that I manually new, this test is successful. The reason is that the caller provides a payload in an independent space for InjectApplicationMessage, and this payload will only be used by MqttServer in the future until it is recycled by GC.

Which component is your bug related to?

  • Server

To Reproduce

Modify the InjectApplicationMessage code of MqttServerExtensions and run the test Preserve_Message_Order_For_Queued_Messages.

public static async Task InjectApplicationMessage(
    this MqttServer server,
    string topic,
    string payload = null,
    MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,
    bool retain = false)
{
    ArgumentNullException.ThrowIfNull(server);
    ArgumentNullException.ThrowIfNull(topic);

    var payloadBuffer = Encoding.UTF8.GetBytes(payload ?? string.Empty);

    var message = new MqttApplicationMessageBuilder()
        .WithTopic(topic)
        .WithPayload(payloadBuffer)
        .WithQualityOfServiceLevel(qualityOfServiceLevel)
        .WithRetainFlag(retain)
        .Build();

    await server.InjectApplicationMessage(new InjectedMqttApplicationMessage(message));

    // clear the buffer
    payloadBuffer.AsSpan().Clear();
}

Expected behavior

After await InjectApplicationMessage(), the memory reference to the payload must be released.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions