BrokenPipeError: [Errno 32] Broken pipe
write EPIPE
IOError: [Errno 32] Broken pipe
You’re writing data to a pipe or socket, but the other end already closed. The reader is gone.
Fix 1: Piped Output Closed Early
# ❌ head closes after 10 lines, but your script keeps writing
python3 generate_data.py | head -10
# ✅ Handle SIGPIPE in Python
import signal
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
Fix 2: Client Disconnected Mid-Response
# ❌ Flask/Django — client closed browser mid-download
@app.route('/big-file')
def download():
return send_file('huge.csv')
# ✅ Catch the error
@app.route('/big-file')
def download():
try:
return send_file('huge.csv')
except BrokenPipeError:
pass # Client left, nothing to do
Fix 3: Writing to a Closed Socket
# ❌ Server closed the connection
sock.sendall(data) # 💥 Broken pipe
# ✅ Check connection before writing
try:
sock.sendall(data)
except BrokenPipeError:
print("Connection closed by remote host")
sock.close()
Fix 4: Node.js EPIPE
// ❌ Writing to a stream that ended
response.write(data); // 💥 EPIPE if client disconnected
// ✅ Check if writable
if (!response.writableEnded) {
response.write(data);
}
// Or handle the error
response.on('error', (err) => {
if (err.code === 'EPIPE') return; // Client left
throw err;
});
Fix 5: SSH/SCP Broken Pipe
# ❌ SSH connection drops during long operations
# ✅ Keep the connection alive
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 user@host
# Or in ~/.ssh/config
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
Fix 6: Docker Logs Broken Pipe
# ❌ Piping docker logs to head/grep
docker logs container | head -100 # 💥 Broken pipe
# ✅ Use --tail instead
docker logs --tail 100 container