sindresorhus / time-grunt

Display the elapsed execution time of grunt tasks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ctrl+C not working

erikdubbelboer opened this issue · comments

When using time-grunt you can't Ctrl+C to kill grunt anymore. It just continues and ignores the event completely and exits when it's done.

Ubuntu 14.04.3 LTS
node v0.12.1
grunt-cli v0.1.13
grunt v0.4.5

Can't reproduce. Are you sure it's time-grunt? Try upgrading Node.

I found the issue. Most tasks in grunt are synchronous. In our case our default task is a big synchronous task which does a lot of sync file IO (grunt.file.write etc).
In node when nothing catches SIGINT it just terminates the process immediately. But when time-grunt catches SIGINT node will dispatch the javascript code to run for it.
The problem is that our big task (any many others like grunt-uglify or grunt-contrib-cssmin) are all synchronous and don't yield for the SIGINT code to run.

This can be seen with this simple Gruntfile:

module.exports = function(grunt) {
  require('time-grunt')(grunt);

  grunt.registerTask('default', function() {
    console.log('testing...');
    var end = Date.now() + 4000;
    while (Date.now() < end) {
      grunt.file.write('test', 'test');
    }
    console.log('done');
  });
};

Try killing it with Ctrl+C and you'll see it only gets killed after the task completes. Now remove time-grunt to remove the SIGINT handler, try killing it again, and you'll see it is killed immediately.

So in conclusion: because most grunt tasks are written in a synchronous way there is nothing time-grunt can do about this issue. Except for maybe document this behavior.

I would suggest removing

time-grunt/index.js

Lines 147 to 149 in d850113

process.on('SIGINT', function () {
process.exit();
});
seeing as people who use Ctrl+C probably aren't interested in seeing the elapsed execution time anyways.

For now the workaround we're using is:

  var processOn = process.on;
  process.on = function(what,cb) {if(what!=='SIGINT'){processOn.call(process,what,cb);}};
  require('time-grunt')(grunt);
  process.on = processOn;

Closing as this project is deprecated because Grunt is no longer actively maintained, so it doesn't make sense for me to maintain this project either.