Skip to content

[PubSub] Exceptions related to stopping publishing #3253

@jnsjll

Description

@jnsjll

Type of issue

  • Bug
  • Enhancement
  • Compliance
  • Question
  • Help wanted

Current Behavior

I can observe two different kind of exceptions being logged when stopping a MqttPubSubConnection. They are caught, however, but preventing them in the first place would avoid giving the impression that there is an error / unexpected exception somewhere.

  • When disposing an UaPubSubApplication instance that uses an MqttPubSubConnection or calling its Stop method, the MQTT client is disconnected. However, its disconnect handler is still in place, trying to reconnect after five seconds, leading to an object disposed exception. This can be seen by enabling logging in the ConsoleReferencePublisher application, pressing CTRL+C and waiting for at least five seconds. Image
  • There is a racing condition when calling the MqttPubSubConnection.Stop() method at the same time an UaPublisher is about to publish network messages. That can lead to a situation in which the MQTT client is disconnected already but its PublishAsync method is still being called. Image

Expected Behavior

No exceptions are logged on a graceful stop.

Steps To Reproduce

Enable logging and stop the ConsoleReferencePublisher using CTRL+C and wait for at least five seconds.

A fork demonstrating that behavior is here.

Environment

- OS: Windows 11
- Runtime: .NET 8
- Nuget Version: 1.05.374
- Component: Opc.Ua.PubSub

Anything else?

Could be achieved by

  • before disconnecting the MQTT client in the MqttPubSubConnection, its disconnect handler is deregistered
  • setting the IsRunning flag to false before disconnecting the MQTT client and checking the flag before publishing network messages

I can provide a PR for that.

Metadata

Metadata

Assignees

Labels

enhancementAPI or feature enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions