Skip to content

Conversation

Namoshek
Copy link
Collaborator

@Namoshek Namoshek commented Sep 30, 2025

This pull request enhances the MQTT client's connection reliability by introducing logic to detect and handle situations where the broker does not respond to ping requests within the configured keep alive interval. The changes ensure the client can automatically reconnect or throw an exception if the connection is deemed dead, improving fault tolerance and connection management.

Connection reliability improvements:

  • Added a new property pingResponseExpectedUntil to track the deadline for receiving a ping response from the broker.

  • In the main loop (loopOnce), implemented logic to check if a ping response was received in time. If not, the client logs a warning, closes the socket, resets ping tracking, and either reconnects automatically or throws a DataTransferException depending on settings.

  • Updated the ping() method to set pingResponseExpectedUntil whenever a ping request is sent, establishing a deadline for the broker's response. The expected deadline depends on the keep alive interval as specified in the MQTT documentation:

    If a client does not receive a PINGRESP message within a Keep Alive time period after sending a PINGREQ, it should close the TCP/IP socket connection.

  • Modified handleMessage() to clear pingResponseExpectedUntil when a ping response (PINGRESP) is received, confirming the broker's responsiveness.

Socket and connection state management:

  • Ensured that both disconnect() and closeSocket() methods set $this->socket to null and $this->connected to false for consistent connection state tracking. [1] [2]

Fixes #209

@Copilot Copilot AI review requested due to automatic review settings September 30, 2025 17:43
@Namoshek Namoshek added the enhancement New feature or request label Sep 30, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request enhances the MQTT client's connection reliability by implementing ping response timeout handling to detect dead connections and automatically recover.

Key changes include:

  • Added timeout tracking for ping responses to detect unresponsive brokers
  • Implemented automatic reconnection or exception throwing when ping responses are not received within the keep alive interval
  • Improved connection state management consistency across socket operations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link

github-actions bot commented Sep 30, 2025

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@Namoshek Namoshek merged commit 3d14184 into master Sep 30, 2025
21 checks passed
@Namoshek Namoshek deleted the feature/ping-timeout-handling branch September 30, 2025 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disconnects are not detected
1 participant