← Back to team overview

mosquitto-users team mailing list archive

Re: What happens if the broker loses its network connection?

 

Note still, mqtt keepalive should catch this too. Had you disabled keepalives? or was your mqtt keepalive set even longer?

On 12/14/2013 07:09 PM, Rene Kogels wrote:
Hi Karl,

I did some more tests, and you are right, it takes 16 minutes before the client
discovers it lost the connection with eh broker and disconnects.

This is what I see on the client when I use netstat -an:
[
Active Internet connections (servers and established)
      Proto Recv-Q Send-Q Local Address           Foreign Address         State
      tcp        0      0 0.0.0.0:22 <http://0.0.0.0:22>              0.0.0.0:*
               LISTEN
      tcp        0      0 192.168.5.3:22 <http://192.168.5.3:22>
192.168.5.50:65489 <http://192.168.5.50:65489>      ESTABLISHED
--->tcp        0  13061 192.168.5.3:33225 <http://192.168.5.3:33225>
192.168.5.5:1883 <http://192.168.5.5:1883>        ESTABLISHED

I send subscribe messages every 10 secs, the Send-Q is getting bigger as nothing
gets acknowledged by the broker.

After 16 minutes I see this:
netstat -an
Active Internet connections (servers and established)
      Proto Recv-Q Send-Q Local Address           Foreign Address         State
      tcp        0      0 0.0.0.0:22 <http://0.0.0.0:22>              0.0.0.0:*
               LISTEN
  -->tcp        0      1 192.168.5.3:33226 <http://192.168.5.3:33226>
192.168.5.5:1883 <http://192.168.5.5:1883>        SYN_SENT

Connection gets a reset, and then the loop() becomes something else than 0 and
escapes.

The settings on my Linux client determine how long it takes before a connection
is considered lost. It looks I might have to tweak these settings a bit
to detect a lost connection sooner.

Thanks,
Rene


On Fri, Dec 13, 2013 at 5:51 PM, Karl Palsson <karlp@xxxxxxxxxxxx
<mailto:karlp@xxxxxxxxxxxx>> wrote:


    How long are you waiting before deciding that it doesn't work?  You might
    have to wait for a
    TCP connection to timeout before the loop will fail, depending on your OS
    and network stack.

    There've been a couple of changes (long ago) that helped propagate this up,
    but pulling the
    plug on an intermediate cable, so no link detection triggers socket cleanup
    or anything can
    really take a while.

    Cheers,
    Karl P


    On Fri, Dec 13, 2013 at 04:49:48PM +0100, Rene Kogels wrote:
     > Thanks Nick. But still, loop() will not detect this. At least not with my
     > code, it will stay in the mosq_client.loop==0 loop when the connection to
     > the broker is lost. When I shut down the broker normally (service mosquitto
     > stop) the loop is stopped, and it tries to reconnect.
     >
     > Code (part):
     >
     > #Setup mosquitto client config
     > mosq_client = mosquitto.Mosquitto("client1")
     >
     > #Connect mosquitto client; if we cannot connect, we will notice later with
     > mosq_client.loop!=0
     > mosq_client.connect("some host")
     >
     > #Endless loop
     > while 1:
     >
     >   while mosq_client.loop()==0:
     >       #Put some code here to read serial port to get some data
     >
     >       #Publish it to the broker
     >       mosq_client.publish("serial/data", serial_data)
     >
     >   #If we are here, we lost the connection with the mosquitto broker, so
     > sleep first and then try to reconnect.
     >   print("No connection with broker. Sleeping before retrying...")
     >   time.sleep(10)
     >
     >   print("Trying to reconnect")
     >   mosq_client.connect("somehost")
     >
     >
     > Rene
     >
     >
     > On Fri, Dec 13, 2013 at 4:17 PM, Nicholas O'Leary  wrote:
     >
     > > Hi Rene,
     > >
     > > if the broker goes away, the tcp layer will notify the client side that
     > > the connection has been lost and the usual connection-lost processing will
     > > occur within the client.
     > >
     > > Regards,
     > > Nick
     > >
     > >
     > > On 12 December 2013 20:09, Rene Kogels  wrote:
     > >
     > >> Hi all,
     > >>
     > >> I have a question about the behaviour of a python program that publishes
     > >> data, but to a broker that lost its network connection after a while. The
     > >> loop() function will probably not detect this as it will only detect
     > >> changes on the network socket caused by the broker. In this case the
    broker
     > >> could not tell the publisher it lost the connection.
     > >>
     > >> Will the publisher keep sending data, assuming there is a broker? Or will
     > >> it detect somehow the broker is gone (TCP does not get an ack)? Will the
     > >> publish function block?
     > >>
     > >> Thanks,
     > >> Rene
     > >>
     > >>
     > >>
     > >> --
     > >> Mailing list: https://launchpad.net/~mosquitto-users
     > >> Post to     : mosquitto-users@xxxxxxxxxxxxxxxxxxx
    <mailto:mosquitto-users@xxxxxxxxxxxxxxxxxxx>
     > >> Unsubscribe : https://launchpad.net/~mosquitto-users
     > >> More help   : https://help.launchpad.net/ListHelp
     > >>
     > >>
     > >

     > --
     > Mailing list: https://launchpad.net/~mosquitto-users
     > Post to     : mosquitto-users@xxxxxxxxxxxxxxxxxxx
    <mailto:mosquitto-users@xxxxxxxxxxxxxxxxxxx>
     > Unsubscribe : https://launchpad.net/~mosquitto-users
     > More help   : https://help.launchpad.net/ListHelp




Follow ups

References