下面这段话用一个例子解释了什么时候会出现Broken Pipe. 简单的说就是,当peer socket 关闭时,可是你还是再往这个socket上发送数据的时候,Broken Pipe就会产生。
When you run type rvm | head -1
, bash executes type rvm
in one process, head -1
in another.1 The stdout of type
is connected to the "write" end of a pipe, the stdin of head
to the "read" end. Both processes run at the same time.
The head -1
process reads data from stdin (usually in chunks of 8 kB), prints out a single line (according to the -1
option), and exits, causing the "read" end of the pipe to be closed. Since the rvm
function is quite long (around 11 kB after being parsed and reconstructed by bash), this means thathead
exits while type
still has a few kB of data to write out.
At this point, since type
is trying to write to a pipe whose other end has been closed – a broken pipe – the write() function it caled will return an EPIPE error, translated as "Broken pipe". In addition to this error, the kernel also sends the SIGPIPE signal to type
, which by default kills the process immediately.
(The signal is very useful in interactive shells, since most users do not want the first process to keep running and trying to write to nowhere. Meanwhile, non-interactive services ignore SIGPIPE – it would not be good for a long-running daemon to die on such a simple error – so they find the error code very useful.)
However, signal delivery is not 100% immediate, and there may be cases where write() returns EPIPE and the process continues to run for a short while before receiving the signal. In this case, type
gets enough time to notice the failed write, translate the error code and even print an error message to stderr before being killed by SIGPIPE. (The error message says "-bash: type:" since type
is a built-in command of bash itself.)
This seems to be more common on multi-CPU systems, since the type
process and the kernel's signal delivery code can run on different cores, literally at the same time.
In other words, it's nothing you can fix, it's also nothing to be concerned about, and it is not related to your rvm
installation in any way.
转自:http://superuser.com/questions/554855/how-can-i-fix-a-broken-pipe-error