1 回答
TA贡献2080条经验 获得超4个赞
返回的错误不是普通的syscall.Error,而是包含在*os.PathError代码中的以下变体中:
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
// open previously created named pipe (`mkfifo /tmp/test`)
pipe, _ := os.OpenFile("/tmp/test", os.O_WRONLY, 0)
for {
n, err := pipe.Write([]byte("foo\n"))
fmt.Printf("write: n=%v, err=(%T) %[2]v\n", n, err)
if err == syscall.EPIPE {
fmt.Println("EPIPE error")
} else if perr, ok := err.(*os.PathError); ok {
fmt.Printf("op: %q; path=%q; err=(%T) %[3]q\n",
perr.Op, perr.Path, perr.Err)
if perr.Err == syscall.EPIPE {
fmt.Println("os.PathError.Err is EPIPE")
}
}
}
}
在mkfifo /tmp/test; head /tmp/test其他地方做完之后运行这个给我:
write: n=4, err=(<nil>) <nil>
[… repeated nine more times, as the head command reads ten lines …]
write: n=0, err=(*os.PathError) write /tmp/test: broken pipe
op: "write"; path="/tmp/test"; err=(syscall.Errno) "broken pipe"
os.PathError.Err is EPIPE
[… above three lines repeated nine more times …]
signal: broken pipe
Exit 1
在单个文件上返回十个管道错误后,Go runtine 停止捕获/阻塞 SIGPIPE 并让它通过您的程序杀死它。我不相信 Go 运行时让您捕获或忽略 SIGPIPE,因为它在内部使用该信号本身。
所以有两件事:一,寻找syscall.EPIPE你需要检查*os.PathError如图所示,二,err != nil当你没有真正处理错误时不要继续。
我不知道Go为什么这样处理SIGPIPE的细节;也许搜索 Go 的错误跟踪器和/或 go-nuts 列表可能有助于回答这个问题。随着 Go 1.5 对os/signal包的添加signal.Reset(syscall.SIGPIPE)(在任何signal.Notify调用之前)改变了这种行为。
- 1 回答
- 0 关注
- 147 浏览
添加回答
举报