mosquitto icon indicating copy to clipboard operation
mosquitto copied to clipboard

mosquitto_loop return MOSQ_ERR_INVAL

Open xieyuyang opened this issue 6 years ago • 9 comments

Our program use mosquitto library to connect MQTT server. When we call mosquitto_loop(), this function may returns MOSQ_ERR_INVAL. And we find that mosq->sock is bigger than FD_SETSIZE(1024).

if(mosq->sock >= FD_SETSIZE || mosq->sockpairR >= FD_SETSIZE){
	return MOSQ_ERR_INVAL;
}

can you fix this issue?

Thanks!

xieyuyang avatar May 30 '19 08:05 xieyuyang

Are you using >1024 open files/sockets elsewhere? What platform are you running on?

ralight avatar May 31 '19 21:05 ralight

Are you using >1024 open files/sockets elsewhere? What platform are you running on?

yes, socket fd > 1024. and our system is centos6.5.

xieyuyang avatar Jun 06 '19 11:06 xieyuyang

Any updates on this one? I have the same problem.

I have two scenarios:

  • one with sending data with size ~66KB - works fine - no errors
  • one with sending big data ~310KB - MOSQ_ERR_INVAL - calling mosquitto_socket says socket is 1119.

OS: Ubuntu 18.04

Any ideas?

Regards, Nedim

nedo99 avatar Feb 21 '20 12:02 nedo99

@nedo99 are you using a lot of fds in your user application? are you using mosquitto_loop()? If you are using a lot of fds in your own app, at least a workaround would be to use the mosquitto_loop_read, mosquitto_loop_write and mosquitto_loop_misc calls from your own fd handling mainloop (presumably then using (e)poll or one of the event libs)

if you're not using a lot of fds in your application, could you provide some more details?

karlp avatar Feb 21 '20 14:02 karlp

@karlp yes we are using quite some fds. Not sure about the exact number. I am using the mosquitto_loop and as mentioned for smaller data round 65KB it works fine, no errors, but with for instance 310KB, then I get invalid arguments error. Does this mean that for more data mosquitto opens even more fds in order to parallelize the transfer?

The only problem with read and write loops is that they are blocking calls and if there is a lot of data we cannot afford being blocked since sending data is not the highest priority.

nedo99 avatar Feb 21 '20 14:02 nedo99

Any other input?

nedo99 avatar Feb 24 '20 08:02 nedo99

I found a workaround for my case. I just put mosquitto initialization to be one of the first things in the code to get a valid socket (<1024).

IMO, this is the bug in the mosquitto_connect method since this method should not return success if the socket does not comply with FD_SETSIZE. The network loop will not work. At least some warning or something should be issued.

nedo99 avatar Feb 24 '20 10:02 nedo99

Hello, I am also experiencing this issue (app based on Intel DPDK, using LOTS of fds under-the-hood). Thread started by mosquitto_loop_start will die silently, and spinning my own thread and calling mosquitto_loop I realized it returned MOSQ_ERR_EINVAL. Securing several low fds (by way of dup(1) before initializing the DPDK library and closing them just before connecting to MQTT broker) allowed me to work around this issue, but it seems fragile (e.g. if my app loses its connection to broker and needs to re-connect). It seems like it could be fixed by switching the pselect API for epoll_pwait.

erkoln avatar Apr 07 '23 15:04 erkoln

It’s really annoying. Please replace select/pselect with poll.

cyanide-burnout avatar Nov 18 '25 11:11 cyanide-burnout