Closed eest closed 6 months ago
// Comment out either of these and the errors at shutdown go away
go messagePublisher(ctx, &wg, pubPayloadChan, logger, mqttCM)
go messageSubscriber(&wg, subMsgChan, logger)
I cannot replicate this bit (the error is output whether these lines are there or commented out). Note that if you comment out either of these but do not comment out your pubPayloadChan <- []byte("test payload")
then your code will end up blocking somewhere (so behaviour will be quite different).
The cause of the message is that paho.Disconnet()
closes the network connection. This leads to the incoming
goroutine receiving an error whilst receiving on the connection; leading to the logging of the error you are seeing.
I note that the error does not reach your OnClientError
function (this should will be prevented by autopaho.errorHandler
).
As such, I believe what you are seeing is the library working as intended (the debug logs exist for debugging purposes and the message is useful in that context). However I may be missing something.
Hello,
Interesting that you can not reproduce it... Just to verify I copy-pasted my own code above to main.go, on ctrl+c i got:
{"level":"debug","service":"pahotest","paho_logger":"debug","time":"2024-02-22T13:23:23Z","message":"mainLoop: server connection handler exiting due to context: context canceled\n"}
{"level":"debug","service":"pahotest","paho_logger":"debug","time":"2024-02-22T13:23:23Z","message":"mainLoop: connection manager has terminated\n"}
{"level":"info","service":"pahotest","time":"2024-02-22T13:23:23Z","message":"messagePublisher: exiting"}
{"level":"debug","service":"pahotest","paho_logger":"paho_debug","time":"2024-02-22T13:23:23Z","message":"error called: read tcp 127.0.0.1:52814->127.0.0.1:1883: use of closed network connection\n"}
{"level":"debug","service":"pahotest","paho_logger":"debug","time":"2024-02-22T13:23:23Z","message":"handleError received extra error: read tcp 127.0.0.1:52814->127.0.0.1:1883: use of closed network connection"}
{"level":"info","service":"pahotest","time":"2024-02-22T13:23:23Z","message":"messageSubscriber: exiting"}
... then changing the code to:
// Comment out either of these and the errors at shutdown go away
//go messagePublisher(ctx, &wg, pubPayloadChan, logger, mqttCM)
go messageSubscriber(&wg, subMsgChan, logger)
... which leads to the following logs:
{"level":"debug","service":"pahotest","paho_logger":"debug","time":"2024-02-22T13:25:31Z","message":"mainLoop: server connection handler exiting due to context: context canceled\n"}
{"level":"debug","service":"pahotest","paho_logger":"debug","time":"2024-02-22T13:25:31Z","message":"mainLoop: connection manager has terminated\n"}
{"level":"info","service":"pahotest","time":"2024-02-22T13:25:31Z","message":"messageSubscriber: exiting"}
... and going the other way:
// Comment out either of these and the errors at shutdown go away
go messagePublisher(ctx, &wg, pubPayloadChan, logger, mqttCM)
//go messageSubscriber(&wg, subMsgChan, logger)
... leads to:
{"level":"debug","service":"pahotest","paho_logger":"debug","time":"2024-02-22T13:28:54Z","message":"mainLoop: server connection handler exiting due to context: context canceled\n"}
{"level":"debug","service":"pahotest","paho_logger":"debug","time":"2024-02-22T13:28:54Z","message":"mainLoop: connection manager has terminated\n"}
{"level":"info","service":"pahotest","time":"2024-02-22T13:28:54Z","message":"messagePublisher: exiting"}
I know the code is not really functional without both of them, I was just trying to zero in on what might be throwing the errors as I wasnt sure how having both of them would really differ from having just one of each (since the code as-is does not really do anything). If they are benign informational errors then we can close this ticket as I just wanted to make sure I was not doing anything obviously broken or silly :).
... I can add that I am building the code with go version go1.22.0 darwin/arm64
if that makes a difference.
Please add a time.Sleep(time.Second)
at the end of main
. My guess is that the difference is one of timing; the wg.Wait()
at the end of your code is waiting for the goroutines to exit; having an additional go routine running will increase the time this takes.
When the error is detected a goroutine is started to handle it (i.e. go c.error(err)
) so there is a race between the goroutine running (and logging the message) and the program ending. This means the results are unpredictable (and it's unsurprising that our results differ). As long as the client itself is not receiving this error (it's only logged by the library) then it's operating as expected.
Hah, nice catch! With...
// Comment out either of these and the errors at shutdown go away
//go messagePublisher(ctx, &wg, pubPayloadChan, logger, mqttCM)
go messageSubscriber(&wg, subMsgChan, logger)
and...
// Wait for handlers to exit.
wg.Wait()
time.Sleep(time.Second)
I now see:
{"level":"debug","service":"pahotest","paho_logger":"debug","time":"2024-02-23T10:44:21Z","message":"mainLoop: server connection handler exiting due to context: context canceled\n"}
{"level":"debug","service":"pahotest","paho_logger":"debug","time":"2024-02-23T10:44:21Z","message":"mainLoop: connection manager has terminated\n"}
{"level":"info","service":"pahotest","time":"2024-02-23T10:44:21Z","message":"messageSubscriber: exiting"}
{"level":"debug","service":"pahotest","paho_logger":"paho_debug","time":"2024-02-23T10:44:21Z","message":"error called: read tcp 127.0.0.1:41998->127.0.0.1:1883: use of closed network connection\n"}
{"level":"debug","service":"pahotest","paho_logger":"debug","time":"2024-02-23T10:44:21Z","message":"handleError received extra error: read tcp 127.0.0.1:41998->127.0.0.1:1883: use of closed network connection"}
As you mentioned I am not getting any errors returned to the client so I'll close this, thanks for the help!
Describe the bug I am writing a tool that will be a subscriber and publisher on the same queue with the same client. When enabling debug messages I noticed I was getting errors on shutdown and I am not sure why.
To reproduce I have cut down on my code to try to get as small as an example as possible, here it is:
Debug output Startup:
After hitting ctrl+c:
Expected behaviour I noticed the two error messages mentioning
use of closed network connection
and have been trying to figure out why this is. I have noticed that if I comment either of the lines:... then the error goes away. As can be seen the code neither receives or publishes anything at this point.
Software used:
mosquitto
version2.0.11-1ubuntu1.1
github.com/eclipse/paho.golang v0.20.0
Additional context I was somewhat thinking this could be related to the reuse of
ctx
inside themessagePublisher()
goroutine but even if i comment out the whole call toPublish()
the error still remains.