caolan / async

Async utilities for node and the browser
http://caolan.github.io/async/
MIT License
28.18k stars 2.41k forks source link

Process exit on queue.drain() #1833

Closed RageYL closed 2 years ago

RageYL commented 2 years ago

Version 3.2.3 NodeJS, Typescript 4.6.4

import { queue } from 'async';

async function bug() {
  process.on('exit', (...args) => {
    console.log('EXIT', args);
  });

  const progressQueue = queue(() => {
    console.log('progress');
  });

  console.log('before');
  await progressQueue.drain();
  console.log('after');
}

bug();

See 'before' and 'after' being printed'. Instead I got:

before
EXIT [ 0 ]

The process exit without any error after the call to drain. I was able to reproduce on a linux laptop and a macos laptop.

RageYL commented 2 years ago

I guess it's because there is nothing on the event loop anymore: https://stackoverflow.com/questions/46914025/node-exits-without-error-and-doesnt-await-promise-event-callback

Sounds like it's not a bug =)

RageYL commented 2 years ago

Actually, would it be possible to have a check in drain() to return immediately if the queue is empty? This should prevent this problem.

hargasinski commented 2 years ago

This is behaving as expected. When q.drain() is called without any arguments, it returns a promise that resolves when the queue drains. Since the queue is never started, it never drains, so the promise is never resolved and console.log('after'); is not called.

I don't think it makes sense to return immediately if the queue is empty as the queue could be loaded asynchronously.