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

PublishAsync doesn't return a result nor exception #1996

Closed
don-pironet-hatch opened this issue May 16, 2024 · 4 comments
Closed

PublishAsync doesn't return a result nor exception #1996

don-pironet-hatch opened this issue May 16, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@don-pironet-hatch
Copy link

Describe the bug

When publishing async messages after each other we often don't get a result back and also no exception is thrown.
On top of that we also have seen cases that the awaits are all executed but with no result (which is weird because we don't see the the result of the publish and still it goes to the next publish).

Which component is your bug related to?

  • Client

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of MQTTnet 'v4.3.4.1084'.
  2. Run this code see below
  3. Not always a result is returned
  4. We don't see any exception nor error.

Expected behavior

What we expect is or a result is returned either success or not OR an exception is thrown that something did go wrong

Code example

	_mqttFactory = new MqttFactory();
        _mqttClient = _mqttFactory.CreateMqttClient();


	var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer(ipAddress).WithKeepAlivePeriod(TimeSpan.FromSeconds(30)).WithCleanSession().Build();
        var response = await _mqttClient.ConnectAsync(mqttClientOptions);

        private async Task PublishTopic(string topic, byte[]? payload = null)
        {

            try
            {
                var applicationMessageBuilder = new MqttApplicationMessageBuilder()
                    .WithTopic(topic);

                if (payload != null)
                {
                    applicationMessageBuilder.WithPayload(payload);
                }

                var result = await _mqttClient.PublishAsync(applicationMessageBuilder.Build(), CancellationToken.None);
                Logger.WriteLine(
                        $"Publish topic: {topic} with success: {result.IsSuccess}"); // Often not seen
            }
            catch (Exception exception)
            {
                Logger.WriteLine($"MQTT publish failed with exception: {exception}"); // No exception when the result is never returned either
            }
        }

        public async Task Foo(byte[] data)
        {
            await _mqttPublisher.Publish(topic1, data);
            await _mqttPublisher.Publish(topic2, data); // continues even though we didn't get a result nor exception
            await _mqttPublisher.Publish(topic3, data);
        }
 
@don-pironet-hatch don-pironet-hatch added the bug Something isn't working label May 16, 2024
@chkr1011
Copy link
Collaborator

What exactly means "no result". Is there null returned or is there a wrong reason code?

@SeppPenner
Copy link
Collaborator

public async Task Foo(byte[] data)
{
await _mqttPublisher.Publish(topic1, data);
await _mqttPublisher.Publish(topic2, data); // continues even though we didn't get a result nor exception
await _mqttPublisher.Publish(topic3, data);
}

How do you expect to get a result if you don't read it into a variable? await _mqttPublisher.Publish(topic2, data); will always proceed unless an exception is thrown and that is alright?

What about something like:

public async Task Foo(byte[] data)
{
    var x = await _mqttPublisher.Publish(topic1, data);
    var y = await _mqttPublisher.Publish(topic2, data); // continues even though we didn't get a result nor exception
    var z = await _mqttPublisher.Publish(topic3, data);
}

@don-pironet-hatch
Copy link
Author

don-pironet-hatch commented May 17, 2024

What I would expect is that this line:

var result = await _mqttClient.PublishAsync(applicationMessageBuilder.Build(), CancellationToken.None);
always have a result stored in that property before returning. And if there is an exception that an exception is thrown. Now we see that it publishes the mqtt command but before the PublishAsync ends we are already executing the next commands

@chkr1011
Copy link
Collaborator

@don-pironet-hatch Please provide a full running example to that we can try to reproduce the problem. The code you posted contains a _mqttPublisher where the implementation is not clear and how it works internally.

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

No branches or pull requests

3 participants