Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I recently reinstalled RVM (following the instructions at http://rvm.io) after a fresh install of Ubuntu 12.10 when I got an SSD Drive.

Now, when I type: type rvm | head -1

I receive the following error:

rvm is a function
-bash: type: write error: Broken pipe

But if I immediately repeat the command then I only receive:

rvm is a function

And it appears everything is ok? What's happening? What can I do to fix it? It doesn't happen always. It appears to be more sporadic. I've tried to find some kind of pattern to it but haven't yet.

share|improve this question

2 Answers 2

up vote 10 down vote accepted

Seeing "Broken pipe" in this situation is rare, but normal.

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 that head 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.

It would be possible to remove this message by patching the type builtin (in bash's source code) to immediately exit when it receives an EPIPE from the write() function.

However, it's nothing to be concerned about, and it is not related to your rvm installation in any way.

share|improve this answer
    
Thank you! I was worried about it. I did about an hours worth of googling last night troubleshooting my rvm install and doing repairs to it trying to fix it. I'd just swapped out with an SSD drive and use LVM and encrypted the hard drive so there was a lot of variables coming into play and I just wasn't sure what might have went sideways. Thank you for putting my mind at ease! –  Jason Shultz Feb 20 '13 at 16:43

You can fix a broken pipe at the expense of another process by inserting tail +1 in your pipe, like this:

type rvm | tail +1 | head -1

The +1 option tells tail to print the first line of input and everything that follows. Output will be exactly the same as if tail +1 wasn't there, but the program is smart enough to check standard output and closes the pipe down cleanly. No more broken pipes.

share|improve this answer
    
Nice trick. I've used in a different situation than the one provided here. Thanks! –  Somebody still uses you MS-DOS Sep 14 at 23:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.