eclipse / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
8.84k stars 2.36k forks source link

mosquitto_loop return MOSQ_ERR_INVAL #1299

Open xieyuyang opened 5 years ago

xieyuyang commented 5 years ago

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!

ralight commented 5 years ago

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

xieyuyang commented 5 years ago

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

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

nedo99 commented 4 years ago

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

I have two scenarios:

OS: Ubuntu 18.04

Any ideas?

Regards, Nedim

karlp commented 4 years ago

@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?

nedo99 commented 4 years ago

@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 commented 4 years ago

Any other input?

nedo99 commented 4 years ago

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.

erkoln commented 1 year ago

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.