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

reconnect() no longer disconnects already connected socket #243

Open
vladak opened this issue Feb 9, 2025 · 5 comments
Open

reconnect() no longer disconnects already connected socket #243

vladak opened this issue Feb 9, 2025 · 5 comments

Comments

@vladak
Copy link
Contributor

vladak commented Feb 9, 2025

In the past reconnect() disconnected on TCP level first and then created new connection to the broker. This is no longer the case and some MQTT broker implementations will (correctly, see below) refuse such reconnect attempts due to duplicate client ID. Previously it worked because _get_connect_socket() used by connect() did self._sock.close() so I assume this changed with the conversion to the connection manager.

The spec says in 3.1.4 Response:

If the ClientId represents a Client already connected to the Server then the Server MUST disconnect the existing Client [MQTT-3.1.4-2].

@vladak
Copy link
Contributor Author

vladak commented Feb 9, 2025

The fix should be accompanied by reconnect test, including the resubscribe capability.

@justmobilize
Copy link
Collaborator

@vladak do you know a server setup that will cause this to happen? Happy to fix this, but would like real world repro steps if possible

@vladak
Copy link
Contributor Author

vladak commented Feb 9, 2025

This happened with a custom from-scratch written implementation of a MQTT broker which actually followed the spec, haven't checked any mainline implementations yet. I have a fix in the works, the tests are a bit tricky.

vladak added a commit to vladak/Adafruit_CircuitPython_MiniMQTT that referenced this issue Feb 9, 2025
vladak added a commit to vladak/Adafruit_CircuitPython_MiniMQTT that referenced this issue Feb 9, 2025
@vladak
Copy link
Contributor Author

vladak commented Feb 10, 2025

Also, one interesting tidbit: when connect() is called with a particular session_id, subsequent reconnect() actually grabs new socket because it does not supply the original session ID and hence the connection manager does not match it.

With the change proposed in PR #244 this will no longer be a problem in terms of wasted socket, however the original session ID should be used for the reconnect.

@justmobilize
Copy link
Collaborator

@vladak this should be tested on an ESP32SPI. When I added session support to ConnectionManager, the ESP32SPI would have issues if it was already connected to a host

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants