net.Conn = nil panicing with logentries and gin-gonic

225 views
Skip to first unread message

mnbb...@gmail.com

unread,
Nov 19, 2014, 1:33:51 AM11/19/14
to golan...@googlegroups.com
I'm trying to use logentries (via https://github.com/bsphere/le_go) with my 
application that is using the gin web framework (https://github.com/gin-gonic/gin) and it keeps panicing when checking if the logger.conn (net.Conn) is nil.

2014/11/18 22:31:34 PANIC: runtime error: invalid memory address or nil pointer dereference
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/runtime/panic.c:552 (0x147cd)
panicstring: runtime·panic(err);
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/runtime/os_darwin.c:454 (0x1346e)
sigpanic: runtime·panicstring("invalid memory address or nil pointer dereference");
/Users/mnbbrown/Code/go/src/github.com/bsphere/le_go/le.go:68 (0xd2999)
(*Logger).isOpenConnection: if logger.conn == nil {
/Users/mnbbrown/Code/go/src/github.com/bsphere/le_go/le.go:93 (0xd2c39)
(*Logger).ensureOpenConnection: if !logger.isOpenConnection() {
/Users/mnbbrown/Code/go/src/github.com/bsphere/le_go/le.go:173 (0xd339f)
(*Logger).Write: if err := logger.ensureOpenConnection(); err != nil {
/Users/mnbbrown/Code/go/src/github.com/bsphere/le_go/le.go:122 (0xd2ec7)
(*Logger).Output: _, err := logger.Write([]byte(s))
/Users/mnbbrown/Code/go/src/github.com/bsphere/le_go/le.go:158 (0xd32e0)
(*Logger).Println: logger.Output(2, fmt.Sprintln(v...))
/Users/mnbbrown/Code/go/src/bitbucket.org/mnbbrown/sewca/sewca.go:65 (0x3644)
SendEmail: logger.Println(l)
/Users/mnbbrown/Code/go/src/github.com/gin-gonic/gin/context.go:106 (0x35796)
(*Context).Next: c.handlers[c.index](c)
/Users/mnbbrown/Code/go/src/github.com/gin-gonic/gin/logger.go:19 (0x3b3a0)
func.007: c.Next()
/Users/mnbbrown/Code/go/src/github.com/gin-gonic/gin/context.go:106 (0x35796)
(*Context).Next: c.handlers[c.index](c)
/Users/mnbbrown/Code/go/src/github.com/gin-gonic/gin/recovery.go:96 (0x3bc8e)
func.010: c.Next()
/Users/mnbbrown/Code/go/src/github.com/gin-gonic/gin/context.go:106 (0x35796)
(*Context).Next: c.handlers[c.index](c)
/Users/mnbbrown/Code/go/src/github.com/gin-gonic/gin/logger.go:46 (0x3b4c9)
func.008: c.Next()
/Users/mnbbrown/Code/go/src/github.com/gin-gonic/gin/context.go:106 (0x35796)
(*Context).Next: c.handlers[c.index](c)
/Users/mnbbrown/Code/go/src/github.com/gin-gonic/gin/gin.go:197 (0x3b1aa)
func.004: c.Next()
/Users/mnbbrown/Code/go/src/github.com/julienschmidt/httprouter/router.go:276 (0x11f156)
(*Router).ServeHTTP: handle(w, req, ps)
/Users/mnbbrown/Code/go/src/github.com/gin-gonic/gin/gin.go:125 (0x37d59)
(*Engine).ServeHTTP: engine.router.ServeHTTP(w, req)
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/net/http/server.go:1673 (0x5954f)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/net/http/server.go:1174 (0x5702e)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/runtime/proc.c:1445 (0x18540)
goexit: runtime·goexit(void)

Has this got something to do with access net.Conn from multiple goroutines?
Is there a more efficient way to stream text to a tcp connection?
Should the le_go library be written differently? 

cm...@golang.org

unread,
Nov 19, 2014, 3:53:14 PM11/19/14
to golan...@googlegroups.com, mnbb...@gmail.com
You are permitted to access a net.Conn from multiple goroutines: https://code.google.com/p/go/source/browse/src/pkg/net/net.go?name=release#59.

Either way, `logger.conn == nil` could only panic if logger were nil.  I can't see the code in the SendEmail function that calls logger.Println, but I'd guess that logger used there is probably nil. It only panics on the line you see because that's the first time in the chain of method calls that you access a specific field.

cm...@golang.org

unread,
Nov 19, 2014, 3:55:59 PM11/19/14
to golan...@googlegroups.com, mnbb...@gmail.com
Actually, looking slightly closer at

/Users/mnbbrown/Code/go/src/bitbucket.org/mnbbrown/sewca/sewca.go:65 (0x3644)
SendEmail: logger.Println(l)

I doubt `0x3644` is a valid address. Could you share that details of how that method creates a logger?

Dave Cheney

unread,
Nov 19, 2014, 4:58:10 PM11/19/14
to golan...@googlegroups.com
It is likely that when the logger was created the error was not checked.

mnbb...@gmail.com

unread,
Nov 20, 2014, 1:45:31 AM11/20/14
to golan...@googlegroups.com
That address does look strange.

I am checking for an error on creation and I write to the logger outside the request context. It seems like it only panics inside the context of a gin or http request.
conf_logentries_token := os.Getenv("LOGENTRIES_TOKEN")

logger
, err := le_go.Connect(conf_logentries_token)
if err != nil {
 log
.Fatalln(err)
}

logger.Println("Starting Application!")

The le_go.Connect function looks like this:

func Connect(token string) (*Logger, error) {
 logger
:= Logger{
 token
: token,
 
}


 
if err := logger.openConnection(); err != nil {
 
return nil, err
 
}


 
return &logger, nil
}

and logger.openConnection() looks like this:

func (logger *Logger) openConnection() error {
conn, err := net.Dial("tcp", "data.logentries.com:80")
if err != nil {
return err
}

logger.conn = conn

return nil
}

All this comes from logentries' 'suggested' library: https://github.com/bsphere/le_go 

Dave Cheney

unread,
Nov 20, 2014, 1:53:47 AM11/20/14
to mnbb...@gmail.com, golang-nuts
I would avoid that package, it is not safe for use by multiple goroutines.
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/golang-nuts/15ke4tuKXfw/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

mnbb...@gmail.com

unread,
Nov 20, 2014, 1:59:58 AM11/20/14
to golan...@googlegroups.com, mnbb...@gmail.com
Do you have an another package that I could model off to create a package that can handle the multiple goroutines?
Something that is streaming data over a TCP/UDP?

Cheers.
Reply all
Reply to author
Forward
0 new messages