-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix MQTTv5 reconnection race condition #10705
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
Conversation
Fixes: spring-projects#10697 Address race condition in `Mqttv5PahoMessageHandler` when multiple threads attempt to connect simultaneously. Changes: - Add `RetryTemplate` with configurable retry count for connection attempts - Implement `MqttExceptionMatcher` to handle specific MQTT error codes - Add `setRetryConnectionCount` method to configure retry behavior - Create test suite validating retry logic The retry mechanism specifically handles `REASON_CODE_CONNECT_IN_PROGRESS` and `REASON_CODE_CLIENT_CONNECTED` errors, allowing the client to recover gracefully when connection establishment is already in progress. Default retry count is set to 5 with 100ms delay between attempts.
|
Good. I’ll take a look into your idea in the morning. Meanwhile , please , check this where we give a retry option already: https://docs.spring.io/spring-integration/reference/handler-advice.html |
Enhance `Mqttv5PahoMessageHandler` to support custom `RetryTemplate` configuration for connection retry logic. Previously, the retry behavior was hardcoded with a fixed policy. Users can now provide their own `RetryTemplate` for more flexible retry strategies. Changes: - Add `connectionRetryTemplate` field to store custom retry template - Add `setConnectionRetryTemplate` setter for configuration - Initialize `connectionRetryTemplate` in `onInit` if not already set - Rename `getRetryTemplate` to `getDefaultConnectionRetryTemplate` - Update Javadoc for `setRetryConnectionCount` to note custom template takes precedence - Remove NOSONAR comment from token wait call
|
Good point. I thought about allowing the user set the retry policy but thought it was overkill. But after your comment above, I reconsidered and added that capability. |
Provide a lightweight while to check for connection vs heavyweight retrytemplate
artembilan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great!
Thank you!
...src/test/java/org/springframework/integration/mqtt/Mqttv5ReconnectionRaceConditionTests.java
Outdated
Show resolved
Hide resolved
...src/test/java/org/springframework/integration/mqtt/Mqttv5ReconnectionRaceConditionTests.java
Outdated
Show resolved
Hide resolved
...src/test/java/org/springframework/integration/mqtt/Mqttv5ReconnectionRaceConditionTests.java
Outdated
Show resolved
Hide resolved
...src/test/java/org/springframework/integration/mqtt/Mqttv5ReconnectionRaceConditionTests.java
Outdated
Show resolved
Hide resolved
...tt/src/main/java/org/springframework/integration/mqtt/outbound/Mqttv5PahoMessageHandler.java
Outdated
Show resolved
Hide resolved
* It assumed multi-thread issue which is not always correct * Update documentation to remove multi-thread connection association * Cleanup based on code review
artembilan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Will merge when build is green
Fixes: #10697
Address race condition in
Mqttv5PahoMessageHandlerwhen multiple threads attempt to connect simultaneously.Changes:
RetryTemplatewith configurable retry count for connection attemptsMqttExceptionMatcherto handle specific MQTT error codessetRetryConnectionCountmethod to configure retry behaviorThe retry mechanism specifically handles
REASON_CODE_CONNECT_IN_PROGRESSandREASON_CODE_CLIENT_CONNECTEDerrors, allowing the client to recover gracefully when connection establishment is already in progress. Default retry count is set to 5 with 100ms delay between attempts.