douguachan2879 2015-10-30 20:17
浏览 138
已采纳

为什么Go的Logger无法关闭与syslog的连接?

I’m having trouble with a service i’m writing. I have it logging to syslog like this: http://technosophos.com/2013/09/14/using-gos-built-logger-log-syslog.html

but when i kill the service via CTRL-C something is leaving connections open to syslogd. I can’t see anyway to initiate a cleanup. What am I missing

e.g. syslog.Writer.Close() seems inaccessible to me but i can say the cleanup isn’t happening. I’m seeing connections stuck in a CLOSE_WAIT state and my syslogd starts bogging down and becoming uncooperative.

example:

package main

import (
    "github.com/davecgh/go-spew/spew" // DEBUG
    "log"
    "log/syslog"
    "os"
    "os/signal"
    "syscall"
)

func main() {
    logWriter, err := syslog.New(syslog.LOG_NOTICE, "mybrokenprog")
    spew.Dump(logWriter) // DEBUG
    spew.Dump(err)       // DEBUG
    if err == nil {
        log.SetOutput(logWriter)
    }

    chansigs := make(chan os.Signal, 1)
    signal.Notify(chansigs, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL)
    log.Print("writing to syslog. waiting for shutdown signal")
    for {
        sig := <-chansigs // we just wait here for a signal to shutdown
        log.Print("signal received...shutting down")
        logWriter.Close() // but this throws the panic excerpted below
        if sig == os.Interrupt {
            os.Exit(0)
        }
        os.Exit(1)
    }
}

EDIT: the above code fixes the problem. see the comments. Rebooting to clear the stuck connections to syslog fixed the problem. Once they're there, they're persistent. Even OS X's instructions for retarting syslogd did not help. EDIT: this was the error: panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x0 pc=0xcb4fe]

goroutine 1 [running]: log/syslog.(*Writer).Close(0x0, 0x0, 0x0) /usr/local/Cellar/go/1.5.1/libexec/src/log/syslog/syslog.go:177 +0x10e main.main(0x3)

I've no doubt I'm doing it wrong. Just a newb who doesn't yet see where.

  • 写回答

1条回答 默认 最新

  • dongxiao0449 2016-01-22 15:24
    关注

    The problem was with the way the program was originally structured. The main() thread was exiting before the logging goroutine and thus orphaning connections. After refactoring to prevent that, the problem went away.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了