epipebomb icon indicating copy to clipboard operation
epipebomb copied to clipboard

console.log no longer EPIPEs in Node@14 (but process.stdout.write does)

Open anko opened this issue 4 years ago • 1 comments

I got here from https://github.com/nodejs/node-v0.x-archive/issues/3211. Thanks for making this.

By default, the following example code from the readme does not throw EPIPE when piped into head, unlike advertised:

;(function log() {
  console.log('tick')
  process.nextTick(log)
})()

Instead, it just loops infinitely writing into an internal buffer until it eats all of your memory. According to https://github.com/nodejs/node/pull/9744 and https://github.com/nodejs/node/issues/11568 this seems to be the intended behaviour.

Anyway, using process.stdout.write instead of console.log does still throw EPIPE:

;(function log() {
  process.stdout.write('tick\n')
  process.nextTick(log)
})()
$ node -v
v14.1.0

This module still fixes both classes of problem:

  • causes the console.log program to exit instead of deathlooping, and
  • causes the process.stdout.write program to exit happy instead of exit sad.

The readme example and prose could use an update to reflect this.

anko avatar May 06 '20 19:05 anko

We do see EPIPEs on console.log on Node v19, when stdout is redirected to a truncating sink like head / grep when running node with npx clinic flame

node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at afterWriteDispatched (node:internal/stream_base_commons:160:15)
    at writeGeneric (node:internal/stream_base_commons:151:3)
    at Socket._writeGeneric (node:net:901:11)
    at Socket._write (node:net:913:8)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at console.value (node:internal/console/constructor:300:16)
    at console.log (node:internal/console/constructor:377:26)
    at status (/home/murtaza/bravehub/serverless-dns/node_modules/clinic/bin.js:376:15)
Emitted 'error' event on Socket instance at:
    at Socket.onerror (node:internal/streams/readable:785:14)
    at Socket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -32,
  code: 'EPIPE',
  syscall: 'write'
}

Node.js v19.0.1

ignoramous avatar Feb 09 '23 00:02 ignoramous