mcuadros / go-syslog

Syslog server library for go.
http://godoc.org/gopkg.in/mcuadros/go-syslog.v2
MIT License
523 stars 143 forks source link

ListenUnixgram runtime error #26

Closed jonnsl closed 8 years ago

jonnsl commented 8 years ago

I'm trying to receive logs from nginx using unix sockets, but when nginx sends the first message, nothing happens, on the second message, the program crashes.

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x28 pc=0x473ce6]

goroutine 6 [running]:
gopkg.in/mcuadros/go-syslog%2ev2.(*Server).goReceiveDatagrams.func1(0xc82008c090, 0x7feea6e0a448, 0xc82002e030)
    /home/jonnathan/go/src/gopkg.in/mcuadros/go-syslog.v2/server.go:275 +0x116
created by gopkg.in/mcuadros/go-syslog%2ev2.(*Server).goReceiveDatagrams
    /home/jonnathan/go/src/gopkg.in/mcuadros/go-syslog.v2/server.go:288 +0x11e

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0xc82008c0cc)
    /usr/lib/go/src/runtime/sema.go:43 +0x26
sync.(*WaitGroup).Wait(0xc82008c0c0)
    /usr/lib/go/src/sync/waitgroup.go:126 +0xb4
gopkg.in/mcuadros/go-syslog%2ev2.(*Server).Wait(0xc82008c090)
    /home/jonnathan/go/src/gopkg.in/mcuadros/go-syslog.v2/server.go:241 +0x2d
main.main()
    /home/jonnathan/go/src/github.com/jonnsl/ssd/ssd.go:35 +0x311

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /usr/lib/go/src/runtime/asm_amd64.s:1721 +0x1

goroutine 5 [chan receive]:
gopkg.in/mcuadros/go-syslog%2ev2.(*Server).goParseDatagrams.func1(0xc82008c090)
    /home/jonnathan/go/src/gopkg.in/mcuadros/go-syslog.v2/server.go:299 +0xc5
created by gopkg.in/mcuadros/go-syslog%2ev2.(*Server).goParseDatagrams
    /home/jonnathan/go/src/gopkg.in/mcuadros/go-syslog.v2/server.go:312 +0x92

goroutine 7 [chan receive]:
main.main.func1(0xc820014180)
    /home/jonnathan/go/src/github.com/jonnsl/ssd/ssd.go:30 +0x5d
created by main.main
    /home/jonnathan/go/src/github.com/jonnsl/ssd/ssd.go:33 +0x303

Here is my code:

    package main

    import (
        "fmt"
        "gopkg.in/mcuadros/go-syslog.v2"
        "log"
        "os"
    )

    const socket_path = "/var/run/ssd/ssd.sock"

    func main() {
        channel := make(syslog.LogPartsChannel)
        handler := syslog.NewChannelHandler(channel)

        removeSocket()
        defer removeSocket()

        server := syslog.NewServer()
        server.SetFormat(syslog.Automatic)
        server.SetHandler(handler)
        server.ListenUnixgram(socket_path)
        server.Boot()

        if err := os.Chmod(socket_path, 0222); err != nil {
            log.Fatal(err)
        }

        go func(channel syslog.LogPartsChannel) {
            for logParts := range channel {
                fmt.Println(logParts)
            }
        }(channel)

        server.Wait()
    }

    func removeSocket() {
        os.Remove(socket_path)
    }

ListenUDP works though

abligh commented 8 years ago

server.go:288 is actually a close brace, and the source of the exception is within kill. Perhaps you do not have an up to date go-syslog. Could you try updating to master?

jonnsl commented 8 years ago

without using gopkg.in, here is the new error message:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x28 pc=0x4749e9]

goroutine 6 [running]:
github.com/mcuadros/go-syslog.(*Server).goReceiveDatagrams.func1(0xc820092090, 0x7f0695cf94c0, 0xc82002e030)
    /home/jonnathan/go/src/github.com/mcuadros/go-syslog/server.go:326 +0x179
created by github.com/mcuadros/go-syslog.(*Server).goReceiveDatagrams
    /home/jonnathan/go/src/github.com/mcuadros/go-syslog/server.go:339 +0x11e

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0xc8200920cc)
    /usr/lib/go/src/runtime/sema.go:43 +0x26
sync.(*WaitGroup).Wait(0xc8200920c0)
    /usr/lib/go/src/sync/waitgroup.go:126 +0xb4
github.com/mcuadros/go-syslog.(*Server).Wait(0xc820092090)
    /home/jonnathan/go/src/github.com/mcuadros/go-syslog/server.go:292 +0x2d
main.main()
    /home/jonnathan/go/src/github.com/jonnsl/ssd/ssd.go:35 +0x33e

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /usr/lib/go/src/runtime/asm_amd64.s:1721 +0x1

goroutine 5 [chan receive]:
github.com/mcuadros/go-syslog.(*Server).goParseDatagrams.func1(0xc820092090)
    /home/jonnathan/go/src/github.com/mcuadros/go-syslog/server.go:350 +0xc5
created by github.com/mcuadros/go-syslog.(*Server).goParseDatagrams
    /home/jonnathan/go/src/github.com/mcuadros/go-syslog/server.go:363 +0x92

goroutine 7 [chan receive]:
main.main.func1(0xc820014180)
    /home/jonnathan/go/src/github.com/jonnsl/ssd/ssd.go:30 +0x5d
created by main.main
    /home/jonnathan/go/src/github.com/jonnsl/ssd/ssd.go:33 +0x330
abligh commented 8 years ago

Thanks for the report.

I think I have fixed this (my fault) on this branch: https://github.com/abligh/go-syslog/tree/fix-null-datagram-address

You can try the fix locally with this patch: https://github.com/abligh/go-syslog/commit/429245c3f9acbb7769551bea54800f5ae0342be9

If you confirm it fixes this issue then I will send a pull request.

jonnsl commented 8 years ago

It works perfectly, thanks!

abligh commented 8 years ago

Thanks. Pull Request #27 generated.