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

Help stops before finishing to write in Node v6.1.0 #497

Closed
nicola opened this issue May 9, 2016 · 16 comments
Closed

Help stops before finishing to write in Node v6.1.0 #497

nicola opened this issue May 9, 2016 · 16 comments

Comments

@nicola
Copy link

nicola commented May 9, 2016

var yargs = require('yargs')
const program = yargs
  .usage('usage: $0 [options]')

// program.option('hello', {description: 'test option'})
for (var i = 0; i < 20; i++) {
  program.option('hello_' + i, {description: 'this is a medium sized description, good!'})
}

program
  .help('help')
  .wrap(null)

const argv = program.argv

if (argv._.length < 1) {
  yargs.showHelp()
}

Here is my little command:

$ node example2.js
usage: example2.js [options]

Options:
  --hello_0   this is a medium sized description, good!
  --hello_1   this is a medium sized description, good!
  --hello_2   this is a medium sized description, good!
  --hello_3   this is a medium sized description, good!
  --hello_4   this is a medium sized description, good!
  --hello_5   this is a medium sized description, good!
  --hello_6   this is a medium sized description, good!
  --hello_7   this is a medium sized description, good!
  --hello_8   this is a medium sized description, good!
  --hello_9   this is a medium sized description, good!
  --hello_10  this is a medium sized description, good!
  --hello_11  this is a medium sized description, good!
  --hello_12  this is a medium sized description, good!
  --hello_13  this is a medium sized description, good!
  --hello_14  this is a medium sized description, good!
  --hello_15  this is a medium sized description, good!
  --hello_16  this is a medium sized description, good!
  --hello_17  this is a medium sized description, good!
  --hello_18  this is a medium sized description, good!
  --hello_19  this is a medium sized description, good!
  --help      Show help  [boolean]

All correct, but when I use the flag --help, it breaks:

$ node example2.js --help
usage: example2.js [options]

Options:
  --hello_0   this is a medium sized description, good!
  --hello_1   this is a medium sized description, good!
  --hello_2   this is a medium sized description, good!
  --hello_3   this is a medium sized description, good!
  --hello_4   this is a medium sized description, good!
  --hello_5   this is a medium sized description, good!
  --hello_6   this is a medium sized description, good!
  --hello_7   this is a medium sized description, good!
  --hello_8   this is a medium sized description, good!
  --hello_9   this is a medium sized description, good!
  --hello_10  this is a medium sized description, good!
  --hello_11  this is a medium sized description, good!
  --hello_12  this is a medium sized description, good!
  --hello_13  this is a medium sized description, good!
  --hello_14  this is a medium sized description, good!
  --hello_15  this is a medium sized description, good!
  --hello_16  this is a medium sized description, good!
  --hello_17 %

This is the same problem facing tj/commander.js#530 right now,
do you have a fix for this planned?

@bcoe
Copy link
Member

bcoe commented May 9, 2016

@nicola interesting, is there a chance that printing the output is taking more than one tick? this would be strange behavior, but should be fixable; looks like we might need to do some thorough testing debugging on Node@6.x.

@nicola
Copy link
Author

nicola commented May 9, 2016

hey @bcoe thanks for your quick reply, Node v6 has done several updates to Buffer, worth looking into it

@lpinca
Copy link

lpinca commented May 10, 2016

Posting this here for reference: nodejs/node#6456.

@eljefedelrodeodeljefe
Copy link

console.log is not blocking for a while now. We are seeing lots of behaviour like this in similar cases and repos, because recent libuv changes have made this problem visible. We are working on workarounds in node core.

The problem often arises when looping over stdout or console in combination with with process.on('exit', handlers), this could be an immediate fix. I keep you posted.

@bcoe
Copy link
Member

bcoe commented May 10, 2016

@nicola @eljefedelrodeodeljefe I've been chatting with @othiym23, and have a plan of action for a hacky patch; @nicola could you submit a failing test case in test/integration?

@eljefedelrodeodeljefe
Copy link

Just to throw in some: I assume it's about the recursive call of parseArgs and the process.exit(0) here.

At best the hotfix is revertable in the future though. The private process APIs are likely to change and are a terrible hack - if you consider them.

@eljefedelrodeodeljefe
Copy link

I have tried replacing all the process.exit() with

ee.on('custom_exit', function () {
  // no-op
})

and a respective ee.emit('custom_exit', 0) and at least that prints everything. You would have to decide whether that is breaking the APIs

@eljefedelrodeodeljefe
Copy link

Exploring this here #498

@bcoe
Copy link
Member

bcoe commented May 10, 2016

@eljefedelrodeodeljefe would custom_exit be emitted immediately, or would the consumer code be able to execute in the current tick before our event fires; something we should test :)

@eljefedelrodeodeljefe
Copy link

hmm. Can you think of a test case? When I see this right node core defer most events to the nextTick. In this case I would suppose it wouldn't matter because when you return from top scope and there is still something on the event loop that will execute. But, tbh, I am not sure.

bcoe pushed a commit that referenced this issue May 14, 2016
bcoe added a commit that referenced this issue May 15, 2016
* test: add failing test for #497

* fix: created shim that sets stdout/stderr to blocking when we know we're about to exit

* fix: OSX tests need a bit more running time

* fix: for the sake of Browserify, also check for _handle

* chore: pulled setBlocking shim into its own module
@bcoe
Copy link
Member

bcoe commented May 15, 2016

@nicola please give this a spin:

npm i yargs@next

I've written this shim: https://github.com/yargs/set-blocking

@bcoe
Copy link
Member

bcoe commented May 16, 2016

@nicola bump; somewhat worried about this change, so would love to have the second set of eyes before it gets promoted to latest.

@nexdrew
Copy link
Member

nexdrew commented May 16, 2016

@bcoe FWIW, using @nicola's script and running with --help on Node 6.1.0, I was able to consistently see the problem with yargs@4.7.0 and cannot reproduce the problem with yargs@4.7.1, so it looks good to me. 👍

@bcoe
Copy link
Member

bcoe commented May 17, 2016

@nicola this should now be fixed on latest.

@nicola
Copy link
Author

nicola commented May 17, 2016

edit: all great it works! thank you so much for the quick fix!

@bcoe
Copy link
Member

bcoe commented May 17, 2016

@nicola awesome, I can close this out?

@bcoe bcoe closed this as completed May 17, 2016
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

5 participants