caolan / async

Async utilities for node and the browser

Home Page:http://caolan.github.io/async/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Process exit on queue.drain()

RageYL opened this issue · comments

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.

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 =)

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

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.