Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Error: write EIO when trying to write to closed terminal #6612

Closed
vkurchatkin opened this issue Dec 1, 2013 · 2 comments
Closed

Error: write EIO when trying to write to closed terminal #6612

vkurchatkin opened this issue Dec 1, 2013 · 2 comments

Comments

@vkurchatkin
Copy link

Test code:

//index.js
var fs = require('fs')

process.on('uncaughtException', function (err) {
  fs.writeFileSync('./uncaughtException.txt', err.stack)
})

setInterval(ping, 1000)

function ping () {
  console.log('ping')
}

Then :

node index.js &
exit

Result (uncaughtException.txt) :

Error: write EIO
    at errnoException (net.js:770:11)
    at Object.afterWrite (net.js:594:19)

Shouldn't this be handled gracefully (e.g. not to crash)?

@vkurchatkin
Copy link
Author

Found an approach to work this issue around:

var isatty = require('tty').isatty

var log = console.log

console.log = function () {
  if (!isatty(process.stdout)) return
  log.apply(this, arguments)
}

but it feels wrong to do that

@tjfontaine
Copy link

Unfortunately what is happening here is that the controlling process is closing the stdout after you exit. If you want to handle this case, you should listen for process.stdout.on('close') and then either cease to log or change how you're logging at that point.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants