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

Panic on lost connection #415

Closed
junavarg opened this issue May 3, 2020 · 2 comments
Closed

Panic on lost connection #415

junavarg opened this issue May 3, 2020 · 2 comments

Comments

@junavarg
Copy link

junavarg commented May 3, 2020

Hello, I am developing a program for unattended systems in remote locations (3G/4G connected) that has to publish events to MQTT broker. I have encountered an unexpected behaviour when testing lost of connectivity conditions of such system in a Ubuntu Server 18.04.2 (on VirtualBox 6.1.1 on Windows 10)

Once first connection is stablished wiht broker,
if a loss of connectivity arise (for example shutting down MQTT broker, or deleting default router to internet, or shutting down Ethernet IF)
Linux process unexpectedly ends showing:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x496b78]

It occurs with all transport protocols: tcp, ssl, ws, wss.
It also ocurss in Ubuntu 16.04
However same code compiled in ARM (Raspbian) works correctly.

This is a minimal testing code that reproduce the error. SetKeepAlive and SetPingTimeout are intentionally settled to short times for fast showing the effect. Note than no client activity is performed (other than a dot print).

package main

import (
	"fmt"
	"os"
	"time"
	"github.com/eclipse/paho.mqtt.golang"
)
func onConnectHandler(c mqtt.Client){
	fmt.Println("¡¡ conexión!! ")
}

func onConnetionLostHandler(c mqtt.Client, er error ){
	fmt.Println("¡¡ conexión perdida !! ", er)
}

func main() {
	opts := mqtt.NewClientOptions()
	opts.AddBroker("tcp://broker.hivemq.com:1883")
	opts.SetClientID("50695833")
	opts.SetKeepAlive(2 * time.Second)
	opts.SetPingTimeout(2 * time.Second)
	opts.SetOnConnectHandler(onConnectHandler)	
	opts.SetConnectionLostHandler(onConnetionLostHandler)	
	c := mqtt.NewClient(opts)
	fmt.Println("Conexión inicial...")
	if token := c.Connect(); token.Wait() && token.Error() != nil {
		fmt.Println("Error de conexión inicial", token.Error())
		os.Exit(-1)
	}
	for i := 0; ; i++ {
		time.Sleep(time.Second*5)
		fmt.Printf(".")
	}
}

This is the output few second later you, for any way, you cause a lack (with enough duration) of connectivity.

$ ./pruebamq
Conexion inicial...
¡¡ conexión!!
..................................¡¡ conexión perdida !! EOF
¡¡ conexión!!
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x496b78]

goroutine 36 [running]:
io.ReadAtLeast(0x0, 0x0, 0xc00001c598, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:310 +0x58
io.ReadFull(...)
/usr/local/go/src/io/io.go:329
github.com/eclipse/paho.mqtt.golang/packets.ReadPacket(0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
/home/juan/go/src/github.com/eclipse/paho.mqtt.golang/packets/packets.go:105 +0x94
github.com/eclipse/paho%2emqtt%2egolang.startIncoming.func1(0x0, 0x0, 0xc000093060, 0xc000093050, 0xc000083140)
/home/juan/go/src/github.com/eclipse/paho.mqtt.golang/net.go:105 +0x11c
created by github.com/eclipse/paho%2emqtt%2egolang.startIncoming
/home/juan/go/src/github.com/eclipse/paho.mqtt.golang/net.go:103 +0x131

Notice that after disconnection (¡¡ conexión perdida !! ) a callback OnConnect is ¿erroneously? called.

Thank in advance.

@MattBrittan
Copy link
Contributor

Apologies @junavarg; this was caused by a bug in a PR #381 (I suspect there may be a few more issues in there; it was a fairly major change that I had intended to test further). It should be corrected by PR #416 - when retrying the connection the code checks if Disconnect() has been called and, if so, drops out of the loop - unfortunately it was checking for the reverse...

@junavarg
Copy link
Author

junavarg commented May 4, 2020

Thank you @MattBrittan.
I see that the matter is under control.
I take this opportunity to thank you for your good work.

Saludos.

@junavarg junavarg closed this as completed May 4, 2020
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

2 participants