Skip to content
This repository was archived by the owner on Nov 10, 2020. It is now read-only.
This repository was 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

Description

@lufia

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".

Activity

  1. self-assigned this
    on Dec 4, 2015
  2. ankoh commented on Jan 6, 2016

    @ankoh

    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
    }
  3. jcw commented on Jan 20, 2016

    @jcw

    +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).

  4. zhenjl commented on Jan 25, 2016

    @zhenjl
    Contributor

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

  5. godblesshugh commented on Nov 7, 2016

    @godblesshugh

    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.

  6. godblesshugh commented on Nov 7, 2016

    @godblesshugh

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

  7. lufia commented on Apr 4, 2020

    @lufia
    Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions