Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connect:packets.ReadPacket block when network is unstable #354

Closed
foxxnuaa opened this issue Sep 4, 2019 · 5 comments
Closed

connect:packets.ReadPacket block when network is unstable #354

foxxnuaa opened this issue Sep 4, 2019 · 5 comments

Comments

@foxxnuaa
Copy link

foxxnuaa commented Sep 4, 2019

in our project of IOT with edge and server. when network is unstable, we found the packets.ReadPacket blocked.
from log,we can see output "connect started",then no execution.

i think, ReadPacket calls io.ReadFull will block if the server cannot response.
so if we can add some timeout to check or avoid.
orelse we need to put the client connect procedure in the go func().

@foxxnuaa
Copy link
Author

foxxnuaa commented Sep 4, 2019

if we can call SetReadDeadline on c.conn when call packets.ReadPacket?

@chertov
Copy link

chertov commented Jan 2, 2020

I have the same problem.. https://github.com/eclipse/paho.mqtt.golang/blob/master/packets/packets.go#L105
block on line 105 appears only 3G network
after few seconds i have reconnecting, but messages do not receive!
if i connecting via ethernet it is ok! but via 3G modem i have the block

# ./mqttc_armv5te
1 ReadFull start
1 ReadFull end
3 ReadFull start
3 ReadFull end
2 ReadFull start
2 ReadFull end
OnConnect
1 ReadFull start
1 ReadFull end
3 ReadFull start
3 ReadFull end
2 ReadFull start
2 ReadFull end
1 ReadFull start
1 ReadFull end
3 ReadFull start
3 ReadFull end
2 ReadFull start
2 ReadFull end
1 ReadFull start
1 ReadFull end
3 ReadFull start
3 ReadFull end
2 ReadFull start
2 ReadFull end
1 ReadFull start
2020/01/02 05:28:53 client online:  web_jgoozmu85hchpqwgpxodm7
2020/01/02 05:28:55 nals:  3355
... blocked here!

@chertov
Copy link

chertov commented Jan 2, 2020

I found a solution
i have ubuntu in Digital Ocean cloud
append to both server and client linux pc
/etc/sysctl.conf

net.ipv4.ip_no_pmtu_disc=1
net.ipv4.tcp_low_latency=1

and i add SetNoDelay(true) https://github.com/eclipse/paho.mqtt.golang/blob/master/client.go#L259

func openConnection(uri *url.URL, tlsc *tls.Config, timeout time.Duration, headers http.Header) (net.Conn, error) {
	conn, err := openConnection1(uri, tlsc, timeout, headers) // old openConnection
	if err != nil { return nil, err }
	fmt.Println("linux settigs for socket")
	tcpConn := conn.(*net.TCPConn)
	err = tcpConn.SetNoDelay(true)
	if err != nil {
		fmt.Println("error in SetNoDelay for the connection!")
		return nil, err
	}
	return conn, nil
}

@zm33y
Copy link

zm33y commented Jan 17, 2020

We are experiencing the same issue right now. Due to some weird network settings, sometimes TLS data flow fails to start. Thus, the ReadPacket() in connect() never returns, and the client gets stuck forever without trying to reconnect.

If we set deadline before calling ReadPacket() it helps. ReadPacket returns error so the client retries in 5 seconds and eventually succeeds.

func (c *client) connect() (byte, bool) {
	DEBUG.Println(NET, "connect started")

	c.conn.SetReadDeadline(time.Now().Add(c.options.ConnectTimeout))
	ca, err := packets.ReadPacket(c.conn)
	c.conn.SetReadDeadline(time.Time{})
...

Same applies to incoming() function.

@MattBrittan
Copy link
Contributor

I believe this was fixed by PR #603 which set a deadline for the handshake. Closing this for now (as it's been a while) but if anyone is still experiencing issues please feel free to reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants