是BUG?golang os.Process 在不同環境下的詭異Panic

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。#### 現象相同代碼在MacOS 13.3下與CentOS 6.5下運行結果不同。#### 問題代碼```go// 這段代碼是封裝了一個MySQL Client,對傳入的host、user、port等參數做處理// 這裡起一個協程監聽singnal傳給子進程,保證MySQL Clinet能夠正常監聽singalgo func() {for {sig := <-scswitch sig {case syscall.SIGINT:// MySQL Clinet會通過stdin監聽SIGINT,再次傳入會造成兩次kill// proc.Signal(syscall.SIGINT)default:err := proc.Signal(sig)if err != nil {Error.Fatalln(err)}}}}()```本來這段代碼一開始Debug正常,也打成包線上上用了一段時間了。最近想給它添加點新功能,結果本地編譯啟動並執行時候就panic了。#### Panic```panic: runtime error: invalid memory address or nil pointer dereference[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x40886ce]goroutine 18 [running]:os.(*Process).signal(0x0, 0x428a880, 0xc420132000, 0x0, 0x0) /usr/local/go/src/os/exec_unix.go:56 +0x2eos.(*Process).Signal(0x0, 0x428a880, 0xc420132000, 0x0, 0x0) /usr/local/go/src/os/exec.go:129 +0x3fmain.main.func1(0xc420072240, 0xc42000e058) /Users/Lepx/work/code/go/src/ku/ku.go:64 +0x80created by main.main /Users/Lepx/work/code/go/src/ku/ku.go:56 +0x33d```根據painc資訊往下找可以看到是這段代碼出的問題```gofunc (p *Process) signal(sig Signal) error {if p.Pid == -1 {//這裡就是 /usr/local/go/src/os/exec_unix.go:56return errors.New("os: process already released")}if p.Pid == 0 {return errors.New("os: process not initialized")}p.sigMu.RLock()defer p.sigMu.RUnlock()if p.done() {return errFinished}s, ok := sig.(syscall.Signal)if !ok {return errors.New("os: unsupported signal type")}if e := syscall.Kill(p.Pid, s); e != nil {if e == syscall.ESRCH {return errFinished}return e}return nil}```然而這段代碼拉到CentOS上編譯運行就正常。第一次遇到Golang相同代碼在不同環境下編譯運行狀態不一致的問題,不知道這是不是一個bug。這個問題目前在1.9.2與1.10上都有發現。464 次點擊  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.