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

ctrl + c not working #156

Open
maunier opened this issue Sep 4, 2020 · 6 comments
Open

ctrl + c not working #156

maunier opened this issue Sep 4, 2020 · 6 comments

Comments

@maunier
Copy link

maunier commented Sep 4, 2020

version: v3.4.0

my code:

const spinner = ora({discardStdin: false})
spinner.start('start')
try {
    myTask()
    spinner.succeed('end')
} catche (e) {
    spinner.fail('failed')
}

When i press ctrl + c during myTask exection , nothing happened.
After I remove code relative to ora, all is well.

I have upgraded to v4.0.4 and don't help

@sindresorhus
Copy link
Owner

4.0.4 should have fixed that issue. What OS and Node.js version?

// @stroncium

@maunier
Copy link
Author

maunier commented Sep 7, 2020

4.0.4 should have fixed that issue. What OS and Node.js version?

// @stroncium

macos 10.12.6
node v12.16.2

@vmfunc
Copy link

vmfunc commented Sep 23, 2020

works for me, node V14.2.0

@alan-agius4
Copy link

I did experience the same issue on 5.1.0.

What worked for me was to set both hideCursor and discardStdin to false.

@davidfirst
Copy link

davidfirst commented Feb 8, 2021

Same here. Tried on the latest version - 5.3.0. Ctrl+C doesn't work. node v12.20.0.

Here is how it can be reproduced quickly. (@sindresorhus , if you want to give it a try).

const spinner = ora({ spinner: 'dots' });
spinner.start('writing files...')
writeSync();
function writeSync(count = 80000) {
  for (let i = 0; i < count; i+=1) {
    fs.writeFileSync(`test.txt`, 'hello world');
  }
}

Run this, it'll create the same file "test.txt" multiple times, during the run, Ctrl+C won't respond until Ora is removed.

I also tried spinner.clear() and spinner.stop(); before calling the writeSync method and it didn't help.

The @alan-agius4 suggestion is the only way I could get it worked.

davidfirst added a commit to teambit/bit that referenced this issue Feb 8, 2021
Currently, it's mostly noticeable during bit build command at the typescript compilation step.
It seems to be a bug in Ora package, see here for more info: sindresorhus/ora#156
I tried the suggested workaround there and it's working.
@sindresorhus
Copy link
Owner

This is not so much an issue with Ora as it is with JavaScript being single-threaded. Both the cursor hiding and stdin discarding features have to register a listener for when Ctrl+C is pressed so they can clean up on exit. However, the fs calls in the loop keep the thread blocked and the SIGTERM (Ctrl+C) event doesn't come through to Ora until after the blocking operations are done.

Possible solutions for you:

  1. Use async calls instead.
  2. Run the blocking code in a worker_thread.
  3. Run the blocking code in a child_process.

nodejs/tooling#42 might fix this. So please do vote up and comment on that issue.

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