Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes the Broken Pipe Error?

Tags:

c

broken-pipe

I know that broken pipe error is thrown when the socket on the peer side is closed.

But, in my test I have noted that an immediate 'send' call on this side when the peer side is closed doesn't always lead to a broken pipe error.

E.g.:

After closing the socket on peer side (I have tried clean closing by calling close and also abnormal closing by killing the peer), if I try to send 40 bytes, then I don't get a broken pipe, but, if I try to send 40000 bytes then it immediately gives broken pipe error.

What exactly causes broken pipe and can it's behavior be predicted?

like image 528
Jay Avatar asked Jan 03 '11 13:01

Jay


People also ask

What causes broken pipe error in C?

If you ignore the SIGPIPE signal, then the functions will return EPIPE error on a broken pipe - at some point when the broken-ness of the connection is detected.

What causes a broken pipe error Python?

"Broken pipe" is essentially an IOError error (short for input/output error), which happened at the Linux system level. It usually occurs when reading and writing files, or in other words, doing file input/output or network input/output (via sockets).

What's a broken pipe error?

A broken Pipe Error is generally an Input/Output Error, which is occurred at the Linux System level. The error has occurred during the reading and writing of the files and it mainly occurs during the operations of the files.

How do you fix a broken pipe error in Python?

Properly catching IOError to avoid Broken pipe error As a Broken pipe error is an IOError error, we can place a try/catch block in order to catch it, as shown in the following snippet of code: Syntax: import sys, errno.


2 Answers

It can take time for the network close to be observed - the total time is nominally about 2 minutes (yes, minutes!) after a close before the packets destined for the port are all assumed to be dead. The error condition is detected at some point. With a small write, you are inside the MTU of the system, so the message is queued for sending. With a big write, you are bigger than the MTU and the system spots the problem quicker. If you ignore the SIGPIPE signal, then the functions will return EPIPE error on a broken pipe - at some point when the broken-ness of the connection is detected.

like image 80
Jonathan Leffler Avatar answered Sep 20 '22 00:09

Jonathan Leffler


The current state of a socket is determined by 'keep-alive' activity. In your case, this is possible that when you are issuing the send call, the keep-alive activity tells that the socket is active and so the send call will write the required data (40 bytes) in to the buffer and returns without giving any error.

When you are sending a bigger chunk, the send call goes in to blocking state.

The send man page also confirms this:

When the message does not fit into the send buffer of the socket, send() normally blocks, unless the socket has been placed in non-blocking I/O mode. In non-blocking mode it would return EAGAIN in this case

So, while blocking for the free available buffer, if the caller is notified (by keep-alive mechanism) that the other end is no more present, the send call will fail.

Predicting the exact scenario is difficult with the mentioned info, but I believe, this should be the reason for you problem.

like image 30
Vikram.exe Avatar answered Sep 22 '22 00:09

Vikram.exe