Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process suspended when stdout and stderr are redirected and the process is daemonized #3

Open
pablopunk opened this issue Apr 26, 2017 · 3 comments

Comments

@pablopunk
Copy link

pablopunk commented Apr 26, 2017

So trying to fix this issue I found that in this line of term-size stops the execution:

const size = execa.sync('resize', ['-u']).stdout.match(/\d+/g);

Reproduce

With the vendor/resize binary, execute a script:

#!/usr/bin/env node
const path = require('path')
const execa = require('execa')
const size = execa.shellSync(path.join(__dirname, 'resize'), ['-u']).stdout.match(/\d+/g)
console.log(size)

And executing like this:

$ ./index.js >log.txt 2>&1 &
[2] 15736
[2]  + 15736 suspended (tty output)  ./index.js > log.txt 2>&1
$ cat log.txt

$ 

This doesn't happen when you don't run the process in the background (so, without the last &):

$ ./index.js >log.txt 2>&1
$ cat log.txt
[ '111', '25' ]
$

This could be an error in execa or in the resize binary, I have no clue.

@pablopunk
Copy link
Author

Okay, I just realized that it says suspended, and I can see the process resize in top with status sleeping:

screen shot 2017-04-26 at 6 05 19 pm

@sindresorhus
Copy link
Owner

I looked into this. It's definitely resize's fault, and not execa. Same thing happened with child_process.execFileSync(). From what I could understand, this happens because resize tries to take control of the TTY while in the background, and that's not allowed.

You could do this workaround: http://stackoverflow.com/a/24056296/64949 But I don't consider that a real fix.

I think I've found a solution though, by detecting whether we are in the background or not, and only execute resize when in the foreground:

#!/usr/bin/env node
const path = require('path')
const execa = require('execa')
const resizeBin = path.join(__dirname, 'resize');
const size = execa.shellSync(`case $(ps -o stat= -p $$) in *+*) "${resizeBin}" -u ;; esac`).stdout.match(/\d+/g)
console.log(size)
❯ ./index.js >log.txt 2>&1 &
[21] 34870
[21]    done       ./t.js > log.txt 2>&1

// @qix In case you have anything to add.

@dankegel
Copy link

dankegel commented Oct 8, 2017

See also yeoman/update-notifier#125

npm itself uses update-notifier which uses an old version of boxen which uses an old version of term-size which has the hang bug.

I'm lucky, I can work around it with npm install -g npm@3.10.8, but sure would be nice to not have to.

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

No branches or pull requests

3 participants