mosquitto icon indicating copy to clipboard operation
mosquitto copied to clipboard

"Bad socket read/write on client: Invalid arguments provided" when client keepalive 0

Open emanuele-dedonatis opened this issue 3 years ago • 7 comments

broker: mosquitto 2.0.14 (docker image eclipse-mosquitto:latest) client: npm mqtt 4.2.6

A client with keepalive=0 is unable to connect, the CONNACK will be returned with status code 2 (Connection Refused, identifier rejected). image

Same client and same config is working with mosquitto 2.0.11 (docker image eclipse-mosquitto:2.0.11) image

The issue appears from mosquitto 2.0.12

emanuele-dedonatis avatar Feb 18 '22 10:02 emanuele-dedonatis

I'm not sure if this is the same issue, but there is a fix in 2.0.13 touching the area: Fix max_keepalive option not being able to be set to 0.

I have a memory about clients receiving bad client-id due to some change that required configuration to allow for keep-alive 0. Did you have a max_keepalive in your config?

Daedaluz avatar Feb 18 '22 12:02 Daedaluz

no, this is my config:

listener 8883

persistence true
persistence_location /mosquitto/data/

allow_anonymous false
password_file /etc/mosquitto/passwd

keyfile /opt/ssl/server.key
certfile /opt/ssl/server.crt
cafile /opt/ssl/ca.crt

log_type all

I didn't mention that obviously changing only client keepalive to a value greater than 0 will solve the issue with mosquitto > 2.0.11, but I think it should be backward compatible

emanuele-dedonatis avatar Feb 18 '22 12:02 emanuele-dedonatis

problem is, a keepalive value of 0 is technically longer than any other number because it turns the functionality off. Let's assume you want a maximum keep alive of 60 seconds on your broker, and put max_keepalive to 60 in your config. You'd expect clients only to be able to specify a maximum keepalive of 60, not be able to turn the whole functionality off and have an "infinite" keep alive.

~~To be backward compatible you'd either have to allow clients to use 0 and be able to turn the keepalive functionality off (which isn't what you'd expect), or introduce another config line like allow_disable_keep_alive <true|false>.~~

The fix was to either allow configuring max_keepalive to 0 or introduce another config line like allow_disable_keep_alive <true|false>.

To be backwards compatible you'd have to allow clients to use keep alive of 0 and be able to turn the functionality off (which isn't what you'd expect).

Both options require a configuration change and the sane default is to not allow for keepalive 0.

Daedaluz avatar Feb 18 '22 13:02 Daedaluz

Well, actually if I don't declare max_keepalive in the broker config I suppose that I'm allowing any keepalive value and it was indeed the behaviour of mosquitto < 2.0.12.

In my opinion, to be backward compatible, if no max_keepalive declared in the config it should allow 0.

Default max_keepalive should be 0 (instead of 65535) which means "infinite" value. If I don't want to allow keepalive 0 I'll set max_keepalive to a value different from 0, which means "finite" value.

emanuele-dedonatis avatar Feb 18 '22 13:02 emanuele-dedonatis

May be. I don't know the exact details to why it was done this way.

Perhaps to avoid too much confusion in the config docs when the default suddenly change from 65535 to 0. One can obviously include both behaviors in the docs though.

Maybe someone else can shed some light to why.

I guess for now, you just need to have the max_keepalive 0 if you want clients to connect with keep alive 0.

Daedaluz avatar Feb 18 '22 15:02 Daedaluz

Thanks! I am setting up an mqtt client using the lwip library; this interfaces with mosquitto version 2.0.15 I am compiling the code from the example here; https://www.nongnu.org/lwip/2_1_x/group__mqtt.html

I was pulling my hair out trying to figure out why my client kept getting rejected from the broker, mosquitto broker terminal window

1675447083: mosquitto version 2.0.15 starting 1675447083: Config loaded from /home/gregor/opt/mosquitto/mosquitto.conf. 1675447083: Opening ipv4 listen socket on port 1883. 1675447083: Opening ipv6 listen socket on port 1883. 1675447083: mosquitto version 2.0.15 running

1675446958: New connection from 192.168.1.81:53746 on port 1883. 1675446958: New client connected from 192.168.1.81:53746 as lwip_test (p2, c1, k0). 1675446958: Bad socket read/write on client lwip_test: Invalid arguments provided.

Setting up the max_keepalive to the default in the *.config file AND setting up a parameter in the client code to set the keep alive worked like a charm;

client code;

ci.keep_alive = 250;


void example_do_connect(mqtt_client_t *client)
{
  struct mqtt_connect_client_info_t ci;
  err_t err;
  
  /* Setup an empty client info structure */
  memset(&ci, 0, sizeof(ci));
  
  /* Minimal amount of information required is client identifier, so set it here */ 
  ci.client_id = "lwip_test";
  ci.keep_alive = 250;
  
  /* Initiate client and connect to server, if this fails immediately an error code is returned
     otherwise mqtt_connection_cb will be called with connection result after attempting 
     to establish a connection with the server. 
     For now MQTT version 3.1.1 is always used */

  	ip_addr_t serverIP;
	IP_ADDR4(&serverIP,192,168,1,66);
  
  err = mqtt_client_connect(client, &serverIP, MQTT_PORT, mqtt_connection_cb, 0, &ci);
  
  /* For now just print the result code if something goes wrong */
  if(err != ERR_OK) {
    printf("mqtt_connect return %d\n", err);
  }
}

mosquitto.config;

max_keepalive 65535

terminalObserver avatar Feb 03 '23 18:02 terminalObserver

thank you @terminalObserver; the mosquitto server had I running on a Synology NAS had been updated and I then hit this issue. I was using lwip/mqtt on a Pi Pico

mbwhite avatar Dec 10 '23 09:12 mbwhite