Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process suddenly terminating with exit code 0 #34

Open
papb opened this issue Aug 31, 2020 · 9 comments
Open

Process suddenly terminating with exit code 0 #34

papb opened this issue Aug 31, 2020 · 9 comments

Comments

@papb
Copy link

papb commented Aug 31, 2020

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?

@sindresorhus
Copy link
Owner

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

@papb
Copy link
Author

papb commented Sep 3, 2020

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

@papb
Copy link
Author

papb commented Sep 6, 2020

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)

@papb
Copy link
Author

papb commented Sep 6, 2020

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...

@sindresorhus
Copy link
Owner

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.

@papb
Copy link
Author

papb commented Nov 23, 2020

@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.

@papb
Copy link
Author

papb commented Nov 23, 2020

This is a little scary... 😮

@papb papb changed the title Process suddenly terminating with exit code 0 with simple usage with fs.createReadStream Process suddenly terminating with exit code 0 Nov 23, 2020
@papb
Copy link
Author

papb commented Nov 23, 2020

For now, I am working around it with:

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

@EvanHahn
Copy link

I can no longer reproduce this on the latest version. Here's a modified version of the original report that works fine for me:

import { pEventIterator } from "p-event";
import * as fs from "node:fs";

(async () => {
  try {
    console.log("A");
    const asyncIterator = pEventIterator(
      fs.createReadStream("package.json"),
      "data",
      {
        resolutionEvents: ["close"],
      },
    );
    console.log("B");
    for await (const event of asyncIterator) {
      console.log("C", event.length);
    }
    console.log("D");
  } catch {
    console.log("E");
  }
})();

This logs:

A
B
C 6113
D

I think this issue can be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants