gulp seems to hang with async task

Created on 16 Jan 2014  路  19Comments  路  Source: gulpjs/gulp

hi,

I might be missing something here, but let's say I define a task as follows.

gulp.task('aaa', function(cb){
  http.get('http://www.google.com/index.html', function(res){
    gutil.log('in result');
    cb();
  });
});

If I run this task from default and then invoke gulp from the command line I get

prompt$ gulp
[gulp] Running 'default'...
[gulp] Running 'aaa'...
[gulp] Finished 'default' in 4.9 ms
[gulp] in result
[gulp] Finished 'aaa' in 134 ms

But the process hangs there and I don't get back to the prompt.

However, If redefine the task as follows, and run it

gulp.task('aaa', function(cb){
  setTimeout(cb, 2000);
});
prompt$ gulp
[gulp] Running 'default'...
[gulp] Running 'aaa'...
[gulp] Finished 'default' in 833 渭s
[gulp] Finished 'aaa' in 2 s
prompt$

This works as expected and the process terminates.

Not sure what I am missing, but any pointers would be great.

All 19 comments

This is curious. Can you post your entire gulpfile.js for both scenarios?

hi @robrich, thanks for taking a look:

prompt$ gulp -v
[gulp] CLI version 3.3.4
[gulp] Local version 3.3.4

Scenario 1 (not working as expected)

var gulp = require('gulp')
  , gutil = require('gulp-util')
  , http = require('http')
;

gulp.task('aaa', function(cb){
  http.get('http://www.google.com/index.html', function(res){
    gutil.log('in result');
    cb();
  });
});

gulp.task('default', function(){
  gulp.run('aaa');
});

Scenario 2 (working as expected)

var gulp = require('gulp')
  , gutil = require('gulp-util')
;

gulp.task('aaa', function(cb){
  setTimeout(cb, 2000);
});

gulp.task('default', function(){
  gulp.run('aaa');
});

I'm not able to reproduce the hang with the gulpfile you provided, but this one hung nicely:

var gulp = require('gulp'),
  gutil = require('gulp-util'),
  http = require('http');

gulp.task('aaa', function(cb){
  http.get('http://www.google.com/index.html', function(res){
    gutil.log('in result');
    console.log(res);
    cb();
  }).on('error', function(e) {
    gutil.log('in error');
    cb(e);
  });
});

gulp.task('default', function(){
  gulp.run('aaa');
});

Even more interesting: it successfully completed and called the orchestration's done callback, but then it just hung waiting for "nothing". I suspect console.log(res) starts accidentally reading the res stream. The below solved it for me, which is definitely overkill for the task at hand:

gulp.on('stop', function () {
  process.nextTick(function () {
    process.exit(0);
  });
});

This also nicely solved it and avoids presuming ending an orchestration means killing the program:

var gulp = require('gulp'),
  gutil = require('gulp-util'),
  http = require('http');

gulp.task('aaa', function(cb){
  http.get('http://www.google.com/index.html', function(res){
    gutil.log('in result');
    console.log(res);
    res.on('end', function () {
      console.log('res.end');
      cb();
    });
    res.on('data', function () {
      console.log('res.data');
    });
  }).on('error', function(e) {
    gutil.log('in error');
    cb(e);
  });
});

gulp.task('default', function(){
  gulp.run('aaa');
});

hi @robrich, that is interesting you were not able to reproduce with the gulpfile in scenario 1 above. Maybe an environment difference?

# OSX 10.8.5
prompt$ node -v
v0.10.24

In any case, the suggestion using process.exit did work for me; however, the second suggestion still results in gulp hanging. I agree that gulp seems to be waiting on nothing, everything works, but it just does not exit.

Thanks again for digging into this.

I just had the same thing happen to me. Except there is nothing async in the task that is hanging.

I can confirm that this is still happening (Ubuntu LTS 12.04 64bit). Tried the exact same gulpfile as @ethul
Using node v0.10.25 and gulp v3.6.0

I also have a box (Ubuntu saucy 13.10) where this is happening.

node 0.10.15
npm 1.4.7
gulp 3.5.6 (and same thing happening with gulp 3.6.1)
gulp-clean 0.2.4

Linux nameless 3.11.0-13-generic #20-Ubuntu SMP Wed Oct 23 07:38:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

Here's one of my hanging tasks:

var gulp = require('gulp');
var clean = require('gulp-clean');

gulp.task('clean', function() {
    return gulp.src([
        './tmp/',
        './build/',
    ], {
        // looking takes longer
        read: false,
    }).pipe(clean());
})

which I call with:

kanzure@whatever$ ./node_modules/.bin/gulp clean

and I get:

kanzure@whoever$ ./node_modules/.bin/gulp clean
[gulp] Using gulpfile /opt/cia.gov/projects/dashboard/gulpfile.js
[gulp] Starting 'clean'...
[gulp] Finished 'clean' after 8.71 ms

and here's what strace is saying:

clock_gettime(CLOCK_MONOTONIC, {8073242, 515537921}) = 0
read(8, "\1\0\0\0\0\0\0\0", 1024)       = 8
futex(0x7f63600008c8, FUTEX_WAKE_PRIVATE, 1) = 1
clock_gettime(CLOCK_MONOTONIC, {8073242, 517692284}) = 0
futex(0x7f63600008c8, FUTEX_WAKE_PRIVATE, 1) = 1
write(1, "[\33[32mgulp\33[39m] Finished '\33[36m"..., 66[gulp] Finished 'clean' after 14 ms
) = 66
gettimeofday({1397704402, 791479}, NULL) = 0
gettimeofday({1397704402, 791658}, NULL) = 0
futex(0x7f63600008c8, FUTEX_WAKE_PRIVATE, 1) = 1
clock_gettime(CLOCK_MONOTONIC, {8073242, 519619772}) = 0
epoll_wait(5, 

This is also happening on another box (Debian jessie).

node 0.10.15
npm 1.4.6
gulp 3.6.1
gulp-clean 0.2.4

Linux clueless 3.10-3-amd64 #1 SMP Debian 3.10.11-1 (2013-09-10) x86_64 GNU/Linux

Perhaps late to the party, but just saying anyway: the script is exiting properly in my OSX shell (darwin), but indeed fails on some Linux distros over here. The process.exit workaround (obviously) makes gulp-watch also exit (making it useless). I really would like to know what exactly cause the issue.

I have found that these problems go away when I make a custom build of nodejs instead of using the Ubuntu-specific or Debian-specific packages.

here's a solution:

var isWatching = false;

gulp.task('watch', [....], function() {
  isWatching = true;
 .... ...
});

gulp.on('stop', function() {
    if (!isWatching) {
        process.nextTick(function() {
            process.exit(0);
        });
    }
});

process.exit happens only if you are not watching.

Having the same problem when I run a gulp-istanbul task. @nidheeshdas, your solution works great for me. Would love to understand what I'm not seeing however.

gulp.task('test', ['env:test', 'jshint'], function () {
    var deferred = Q.defer();
    gulp
        .src(['app/**/*.js', '!app/**/*_test.js', 'config/**/*.js'])
        .pipe(istanbul()) // Covering files
        .pipe(istanbul.hookRequire()) // Force `require` to return covered files
        .on('finish', function () {
            return gulp.src(['app/**/*_test.js', 'spec/**/*.js'])
                .pipe(mocha())
                .pipe(istanbul.writeReports({
                    reporters: ['lcov', 'cobertura', 'text']
                })) // Creating the reports after tests run
                .pipe(istanbul.enforceThresholds({
                    thresholds: {
                        global: {
                            branches: 75,
                            functions: 80,
                            lines: 80,
                            statements: 80
                        }
                    }
                }))
                .on('error', deferred.reject)
                .on('end', deferred.resolve);
        });
    return deferred.promise;
});

Hello,
I am seeing hanging behavior specifically when using the gulp-shell plugin in when it is used to call gulp recursively to process different subdirectories. It hangs on in my Mac OSX but runs without hanging on my Ubuntu box.

nidheeshdas solution worked for me, I don't understand how or why as I've just got into Gulp.
any further comment to explain it would be nice

my watch task is below:-

/* Notes:
   - gulp/tasks/browserify.js handles js recompiling with watchify
   - gulp/tasks/browserSync.js watches and reloads compiled files
*/

var gulp     = require('gulp');
var config   = require('../config');

var isWatching = false;

gulp.task('watch', ['watchify','browserSync'], function() {
  isWatching = true;
  gulp.watch(config.sass.src,   ['sass']);
  gulp.watch(config.images.src, ['images']);
  gulp.watch(config.markup.src, ['markup']);
  // Watchify will watch and recompile our JS, so no need to gulp.watch it
});

gulp.on('stop', function() {
    if (!isWatching) {
        process.nextTick(function() {
            process.exit(0);
        });
    }
});

This happens to me as well, Ubuntu 14.10. I need to manually put process.exit(0), otherwise my gulpfile will never finish, even after all my tasks are completed.

Same problem here.

just got the same with an async task which is kinda long (external build process, ~50s+).
could be related to a timeout maybe ?

I periodically experience a hang when running gulp tasks. It's happened during watch task as well as a simple build task. When I hit Ctrl+c it seems to suddenly catch up (displaying messages it should have displayed if not for the hang) and then, of course, immediately shuts down. I know it's not just the messages that are being delayed because I've checked the files that are building. It's as if, when I hit Ctrl+c, I stop some hanging process that the rest of the gulp task is waiting on, then the rest of the gulp task runs, quickly, before the program shuts down.

N茫o sei se essa ajuda ainda 茅 valida mas, no meu caso estava chamando uma task em forma de loop, ocorrendo esse evento:

[17:42:48] Using gulpfile ~/www/consorcio/gulpfile.js

e minha task estava assim:

gulp.task('ex1', ['ex2'], () => {
return del(config.ex);
});

gulp.task('ex2', ['ex1'], () => {
return del(config.ex2);
});

o que fazia entrar em loop na hora do build default

e no meu watch:

gulp.task('sync', () => {
gulp.watch(config.ex, ['ex1']);
gulp.watch(config.ex2, ['ex2']);
});

resolvir apos remover a dependencia de ambos, e o gulp voltou a funcionar normamente.
espero ter ajudado de alguma forma.

@nidheeshdas nice solution.

You can monkey patch watch for cleaner code.

const watch = gulp.watch;
gulp.watch = function (...args) {
  watching = true;
  return watch(...args);
};

gulp.on('stop', function () {
  if (!watching) {
    process.nextTick(function () {
      process.exit(0);
    });
  }
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

silverskyvicto picture silverskyvicto  路  3Comments

aaronroberson picture aaronroberson  路  4Comments

tom10271 picture tom10271  路  4Comments

amio picture amio  路  3Comments

lehni picture lehni  路  4Comments