eclipse-paho / paho.mqtt.golang

Other
2.77k stars 534 forks source link

client hangs when trying to wait for workers to complete #593

Closed DenisDes closed 2 years ago

DenisDes commented 2 years ago

I'm trying to use the client with default settings.

This is the example code:

       ...
        opts := mqtt.NewClientOptions()
    opts.AddBroker(url)
    opts.SetClientID("some_client_id")
    opts.SetUsername("some_user")
    opts.SetPassword("some_password")

        opts.SetConnectionLostHandler(func(c mqtt.Client, err error) {
        fmt.Println("connection to mqtt broker lost")
    })

    mqtt.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
    mqtt.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
    mqtt.WARN = log.New(os.Stdout, "[WARN]  ", 0)
    mqtt.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0)

        client := mqtt.NewClient(opts)
        ...

After 3-5 minutes in the logs, I see this:

[DEBUG] [pinger]   ping check 5.000680024
[DEBUG] [pinger]   ping check 10.004374699
[DEBUG] [pinger]   ping check 15.003375079                                                                                                                                                                                            
[DEBUG] [net]      startIncoming Received Message                                                                                                                                                                                     
[DEBUG] [net]      startIncomingComms: got msg on ibound                                                                                                                                                                              
[DEBUG] [net]      startIncomingComms: received publish, msgId: 29                                                                                                                                                                    
[DEBUG] [pinger]   ping check 20.00043674                                                                                                                                                                                             
[DEBUG] [pinger]   ping check 25.00431087                                                                                                                                                                                             
[DEBUG] [pinger]   ping check 30.003872424                                                                                                                                                                                            
[DEBUG] [pinger]   keepalive sending ping                                                                                                                                                                                             
[DEBUG] [net]      startIncoming Received Message                                                                                                                                                                                     
[DEBUG] [pinger]   ping check 4.999550466                                                                                                                                                                                             
[DEBUG] [pinger]   ping check 9.999484459                                                                                                                                                                                             
[DEBUG] [pinger]   ping check 14.999642242                                                                                                                                                                                            
[CRIT] [pinger]   pingresp not received, disconnecting                                                                                                                                                                                
[DEBUG] [client]   internalConnLost called                                                                                                                                                                                            
[DEBUG] [client]   stopCommsWorkers called                                                                                                                                                                                            
[DEBUG] [client]   internalConnLost waiting on workers                                                                                                                                                                                
[DEBUG] [client]   stopCommsWorkers waiting for workers 

in client.go:672: c.workers.Wait() will never complete:

go func() {
        DEBUG.Println(CLI, "stopCommsWorkers waiting for workers")
        c.workers.Wait() 

        // Stopping the workers will allow the comms routines to exit; we wait for these to complete
        DEBUG.Println(CLI, "stopCommsWorkers waiting for comms")
        <-c.commsStopped // wait for comms routine to stop

        DEBUG.Println(CLI, "stopCommsWorkers done")
        close(doneChan)
    }()
MattBrittan commented 2 years ago

Please share a minimum, reproducible, example. The most likely issue is as follows (from common problems), but you have not shared enough code to confirm this:

A MessageHandler (called when a new message is received) must not block (unless ClientOptions.SetOrderMatters(false) set). If you wish to perform a long-running task, or publish a message, then please use a go routine (blocking in the handler is a common cause of unexpected pingresp not received, disconnecting errors).

DenisDes commented 2 years ago

Please share a minimum, reproducible, example. The most likely issue is as follows (from common problems), but you have not shared enough code to confirm this:

A MessageHandler (called when a new message is received) must not block (unless ClientOptions.SetOrderMatters(false) set). If you wish to perform a long-running task, or publish a message, then please use a go routine (blocking in the handler is a common cause of unexpected pingresp not received, disconnecting errors).

you were right, the problem was with the blocking of the subscription handler, I apologize for the inconvenience and thank you very much!

MattBrittan commented 2 years ago

Closing as per OPs comment.