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

Node stopping execution prematurely even though promise is still pending. #25504

Closed
ealmansi opened this issue Jan 14, 2019 · 5 comments
Closed
Labels
promises Issues and PRs related to ECMAScript promises.

Comments

@ealmansi
Copy link

ealmansi commented Jan 14, 2019

  • Version: v10.14.2
  • Platform: Linux 4.15.0-43-generic Ubuntu SMP Thu Dec 6 14:45:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux.
  • Subsystem: JavaScript engine.

Upon executing the following code, Node finishes running even though a pending promise is left over.

let promise = Promise.resolve().then(
  async () => {
    console.log('1', promise) // 1 Promise { <pending> }
    await promise
    console.log('2', promise) // - Never executed -
  }
)

I get the following output, and then the Node process stops running.

→ node index.js 
1 Promise { <pending> }

I would expect Node to get stuck and run forever, since the promise in the example will never resolve. When running this in Firefox, I get indeed a never-resolving promise.

Additional note: this looks like a contrived toy example, but is actually just a minimal reproduction of a problem I have in a real codebase.

@mscdex
Copy link
Contributor

mscdex commented Jan 14, 2019

FWIW node master, v10.x, and v8.x all behave the same.

@addaleax
Copy link
Member

@ealmansi Do you mean that an unresolved Promise alone should keep the process running? That would be a significant breaking change and hard to implement (Promises are a language feature, the event loop a Node.js/libuv one).

I think somebody has floated the idea of (optionally?) giving information on unresolved promises when the process exits. I’m not sure what that would look like, but maybe this would help you?

@addaleax addaleax added the promises Issues and PRs related to ECMAScript promises. label Jan 14, 2019
@ealmansi
Copy link
Author

@mscdex @addaleax Thanks for your replies.

Turns out I was working on the wrong assumption that Node would never normally exit the process if there were any pending promises. I now understand that this is not the case.

I got this misunderstanding from the fact that this very often seems to be the actual behaviour, but in reality Node only waits on promises to get resolved when they are wrapping something like a timeout or a network or IO request.

@devsnek
Copy link
Member

devsnek commented Jan 15, 2019

Just for future reference: there's nothing about an unresolved promise that would actually keep a process running. They aren't executing any code or waiting for anything, they're just a fancy way to call a callback.

@Farenheith
Copy link
Contributor

Farenheith commented Aug 6, 2020

@devsnek @addaleax

I found out this issue as I was having the same assumption of @ealmansi, but I want to exemplify that this problem may be quite difficult to figure out when you combine promises, events and async/await features. Take this code:

const { EventEmitter } = require('events');
const emitter1 = new EventEmitter();
const emitter2 = new EventEmitter();

emitter2.on('data', () => emitter1.emit('data', 'emitted'));

const promise = new Promise((resolve) => {
  emitter1.on('data', (value) => {
    console.log('emitted');
    resolve();
  });
}).then(() => console.log('finished'));


async function waitPromise() {
  console.log('starting');
  await promise;
  console.log('awaited');
}

waitPromise();

Output:

starting
Promise { <pending> }

I know it's working as intended but, because of the language features used, especially the async/await, is quite counterintuitive to figure out what's happening, but I have no idea how it could behave differently, as async/await is just syntax sugar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
promises Issues and PRs related to ECMAScript promises.
Projects
None yet
Development

No branches or pull requests

5 participants