sindresorhus / p-event

Promisify an event by waiting for it to be emitted

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Process suddenly terminating with exit code 0

papb opened this issue · comments

I was experimenting with pEvent.iterator with fs.createReadStream. See this very simple example:

// what.js
const pEvent = require('p-event');
const fs = require('fs');

(async () => {
	try {
		console.log('A');
		const asyncIterator = pEvent.iterator(fs.createReadStream('.npmrc'), 'data');
		console.log('B');
		for await (const event of asyncIterator) {
			console.log('C', event.toString());
		}
		console.log('D');
	} catch {
		console.log('E');
	}
})();

Executing node what.js && echo success gives:

A
B
C package-lock=false
success

It does not log D nor E, and even exits with code 0! Do you have any idea what is going on here?

What Node.js version? Can you try with the absolute latest Node.js version?

Sorry I didn't include the version. It's node 12.18.0, on windows. I will try with the absolute latest soon.

Hi @sindresorhus, I just tried all 9 possible combinations of Windows, Linux and Mac with Node 14, 12, and 10. Each one of them gave the exact same output as above...

😮

(tried with github actions)

Is it even possible that this behavior is p-event's fault? Looks like a really weird bug with for await to me... Or with fs.createReadStream... But I can't imagine how p-event would be causing this...

I have no idea, to be honest, but why are you even using p-event for this? Node.js readable streams can be iterated natively with for-await.

@sindresorhus Sorry for the delay. Indeed I could be iterating natively instead, thanks for the reminder! However, I just got into a similar situation, without Node.js readable streams. Here is a minimal example:

// what.js
const pEvent = require('p-event');
const Emittery = require('emittery');
const emitter = new Emittery();

(async () => {
	setTimeout(() => {
		emitter.emit('data', 'foobar');
	}, 500);

	try {
		console.log('A');
		const asyncIterator = pEvent.iterator(emitter, 'data');
		console.log('B');
		for await (const event of asyncIterator) {
			console.log('C', event);
		}
		console.log('D');
	} catch {
		console.log('E');
	}
})();

Again, executing node what.js && echo success gives:

A
B
C foobar
success

In this example, I expected the process to hang indefinitely after printing C foobar.

This is a little scary... 😮

For now, I am working around it with:

const keepAlive = setTimeout(() => {}, (2 ** 31) - 1);
// ...
clearTimeout(keepAlive);