Skip to content
This repository has been archived by the owner on Nov 10, 2020. It is now read-only.

Client.Ping method raises network disconnection: "use of closed network connection" #27

Closed
lufia opened this issue Dec 4, 2015 · 6 comments · May be fixed by #40
Closed

Client.Ping method raises network disconnection: "use of closed network connection" #27

lufia opened this issue Dec 4, 2015 · 6 comments · May be fixed by #40

Comments

@lufia
Copy link

lufia commented Dec 4, 2015

STEPS TO REPRODUCE

$ cd $GOPATH/src/github.com/surgemq/surgemq/examples/surgemq
$ go run *.go

then, go run this code with -logtostderr -vv 3:

package main

import (
    "flag"
    "log"

    "github.com/surgemq/message"
    "github.com/surgemq/surgemq/service"
)

func main() {
    flag.Parse()
    c := &service.Client{}
    cmsg := message.NewConnectMessage()
    cmsg.SetVersion(4)
    cmsg.SetCleanSession(true)
    cmsg.SetKeepAlive(10)
    if err := c.Connect("tcp://localhost:1883", cmsg); err != nil {
        log.Fatalln("Connect:", err)
    }

    fn := func(msg, ack message.Message, err error) error {
        log.Println("Pong:", err)
        return err
    }
    done := make(chan struct{})
    if err := c.Ping(service.OnCompleteFunc(fn)); err != nil {
        log.Fatalln("Ping:", err)
        close(done)
    }
    <-done
}

RESULTS

I1204 23:16:44.348778   13936 sendrecv.go:103/sender] (1/) Starting sender
I1204 23:16:44.348781   13936 process.go:46/processor] (1/) Starting processor
I1204 23:16:44.348883   13936 sendrecv.go:58/receiver] (1/) Starting receiver
I1204 23:16:44.349183   13936 service.go:210/stop] (1/) closing this.conn
I1204 23:16:44.349223   13936 sendrecv.go:100/func1] (1/) Stopping sender
E1204 23:16:44.349252   13936 sendrecv.go:76/receiver] (1/) error reading from connection: read tcp [::1]:61472->[::1]:1883: use of closed network connection
I1204 23:16:44.349364   13936 sendrecv.go:55/func1] (1/) Stopping receiver
I1204 23:16:44.349375   13936 service.go:220/stop] (1/) Received 16 bytes in 2 messages.
I1204 23:16:44.349383   13936 service.go:221/stop] (1/) Sent 6 bytes in 2 messages.

REASONS

In case of Client.Ping(), ackmsg.Msgbuf is empty in service.processAcked().
As a result, msg.Decode(ackmsg.Msgbuf) panics with "slice bounds out of range".

@zhenjl zhenjl self-assigned this Dec 4, 2015
@ankoh
Copy link

ankoh commented Jan 6, 2016

Yep I saw that as well a while ago in your code. The problem is, that a closing socket does NOT return EOF but *net.OpError.

More information can be found here:
http://comments.gmane.org/gmane.comp.lang.go.general/76607

I decided to go for this dirty hack that was proposed in the googlegroups:

func IsEOF(err error) bool {
    if err == nil {
        return false
    } else if err == io.EOF {
        return true
    } else if oerr, ok := err.(*net.OpError); ok {
        if oerr.Err.Error() == "use of closed network connection" {
            return true
        }
    } else {
        if err.Error() == "use of closed network connection" {
            return true
        }
    }
    return false
}

@jcw
Copy link

jcw commented Jan 20, 2016

+1

Same issue here, and the IsEOF mentioned above fixes it for me as well. I changed 2 cases in sendRecv.go and 2 more in process.go - from what I can tell, the current surgemq HEAD won't work without this workaround (this is on Mac OSX 10.11.2 with Go 1.5.3, FWIW).

@zhenjl
Copy link
Contributor

zhenjl commented Jan 25, 2016

Thanks @jcw @ankoh ... Would either of you be willing to submit a PR?

@godblesshugh
Copy link

here is a discussion for that error: errClosing.
and maybe
oerr, ok := err.(*net.OpError)
is not necessary, just need
if err.Error() == "use of closed network connection" { return true }
as @ankoh said.

@godblesshugh
Copy link

sorry.
if err.Error() == "use of closed network connection" { return true }
is not necessary, need
oerr, ok := err.(*net.OpError)

@lufia
Copy link
Author

lufia commented Apr 4, 2020

Unfortunately this repo is labeled as Unmaintained. I will reopen the issue if the status comes back to Maintained.

@lufia lufia closed this as completed Apr 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants