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
Related resources
Related: Bash Cheat Sheet Β· Python Cheat Sheet Β· Pip Install Error Fix Β· Sveltekit Load Error Fix Β· Python Stopiteration Fix