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

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

Open
xljiulang opened this issue Nov 21, 2024 · 0 comments · May be fixed by #2115
Labels
bug Something isn't working

Comments

@xljiulang
Copy link
Contributor

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.

@xljiulang xljiulang added the bug Something isn't working label Nov 21, 2024
xljiulang added a commit to xljiulang/MQTTnet that referenced this issue Nov 22, 2024
@xljiulang xljiulang linked a pull request Nov 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant