Coder Social home page Coder Social logo

Unhandled ConnectionRefusedError about gmqtt HOT 9 OPEN

wialon avatar wialon commented on July 23, 2024 1
Unhandled ConnectionRefusedError

from gmqtt.

Comments (9)

d21d3q avatar d21d3q commented on July 23, 2024 2

Hmmm, my use case is to embed mqtt client in IoT device and let it go into the wild, and report some data.
In such case I would except from program (device), to warn me about bad configuration, but keep retrying to connect without crashing. I am configuring device once (asserting that it is working), and letting him live its own life (like gps trackers :) ).
But there are other factors that might fail and device should not care about them. Imagine that your device reboots during such conditions

  • broker goes offline (crash/update) - it is temporary unavailable, but connect fails due to ConnectionRefusedError
  • network goes down - temporarily, but connect fails due to socket.gaierror
  • somebody deletes (mqtt) user credentails on server, and later restore them,

So if device reboots and goes through connect while one of above edge cases occur, then program would crash.
However when all exceptions on connect are handled and reconnect is manually rescheduled then it works (as suggesed).
That is why I am suggesting that connect should reconnect automatically but notify rest of program about connection failure through some callback.

from gmqtt.

Lenka42 avatar Lenka42 commented on July 23, 2024 1

Hey @d21d3q
I added kwarg raise_exc=True to client.connect method. Together with unlimitted reconnects this should work in your case. LEt me know if it helps 🙂

from gmqtt.

NICOLASGON avatar NICOLASGON commented on July 23, 2024 1

http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718033

If a server sends a CONNACK packet containing a non-zero return code it MUST then close the Network Connection [MQTT-3.2.2-5]

gmqtt doesn't implement the MQTT standard for this particular point.

from gmqtt.

Lenka42 avatar Lenka42 commented on July 23, 2024

Actually this is expected behavior. Reconnects are good when you client is working, but connection at the moment becomes broken and after some time your client automatically reconnects. If the first connection attempt was unsuccessful, it probably means that configuration is bad, host, port or authorization method should be fixed.

from gmqtt.

skewty avatar skewty commented on July 23, 2024

@d21d3q does this resolve your problem? Can this issue be closed?

from gmqtt.

d21d3q avatar d21d3q commented on July 23, 2024

I will have a look at it on Monday. Thank you.

from gmqtt.

mfrlin avatar mfrlin commented on July 23, 2024

No, this is not the correct way of handling this. If you take a look at the reconnect function you will see that it handles OSError and connect function does not. This is why the client explodes when broker is not online and you try to connect.

Workaround is something like this:

client = MQTTClient("client_id")
while True:
    try:
        await client.connect(broker_host)
    except OSError:
        print(f'Can not connect to the broker at {broker_host}')
        await asyncio.wait([asyncio.sleep(5), STOP.wait()], return_when=asyncio.FIRST_COMPLETED)
        if STOP.is_set():
            sys.exit()
    else:
        break

await STOP.wait()
await client.disconnect()

from gmqtt.

Lenka42 avatar Lenka42 commented on July 23, 2024

@NICOLASGON Specification tells server must close the connection after it sends bad CONNACK package. gmqtt is about client relaization and docs do not specify any required actions after receiving bad connack code.

from gmqtt.

mfanara avatar mfanara commented on July 23, 2024

@Lenka42 - Using the current master branch (as of 25 FEB 2022), what is the correct answer?

This works:

try:
await lLocalMRClient.connect("localhost", 1884, keepalive=60)
except ConnectionRefusedError:
asyncio.ensure_future(lLocalMRClient.reconnect(delay=True))

as does:

try:
await lLocalMRClient.connect("localhost", 1884, keepalive=60, raise_exc=True)
except:
asyncio.ensure_future(lLocalMRClient.reconnect(delay=True))

And I see above the suggestion to catch OSError

from gmqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.