-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Nodejs does not wait for promise resolution - exits instead #22088
Comments
If I am not mistaken, Node.js does not wait for ever-pending Promises: 'use strict';
(async function main() {
await new Promise((resolve) => { console.log(42); });
})();
|
In other words, the mere existence of a Promise won't keep the process alive. You actually need to put something on the event loop (e.g. create a timer in the processData method). By the way, even if the process stayed alive because of some other I/O, I think the expected output would be
because the promise Executor is invoked imediately. |
The answers above are correct. Going to close this out. |
@jiripospisil yes you are right, I edited issue. Of course that @apapirovski but why the node end the process in the middle of nowhere? It has other code to execute after that await. There is |
For example: 'use strict';
(async function main() {
await new Promise((resolve) => { console.log(42); });
console.log(`
This will never be executed is this expected?
Also node exits as everything is ok with exit code 0...
`);
})(); |
I agree it's a bit weird when you encounter it for the first time (and I have certainly been bitten by this in the past) but the way to think about it is that when you await new Promise((resolve) => { console.log(42); setTimeout(resolve, 2000) }); then Node.js would see there's a timer and stayed alive. When the timer goes off, Node.js invokes the callback and V8 jumps back to the place from which it jumped out off and starts executing the rest of the function. |
Maybe we could have warning: "Process is exited, but you have pending promises, created on the |
This bit me pretty hard and has been extremely difficult to understand the behavior implications. I have a bundler which operates in a giant async Promise chain. Once a task completes it moves on to the next. I've added the ability for "loaders" to pre-process an AST and wanted to use a browserify plugin. I wrote the following code: transform: async ({ state, source }) => {
return new Promise((resolve, reject) => {
const stream = combynify(state.path, {});
stream.on('data', console.log);
stream.resume();
stream.on('finish', function(d) {
resolve(d);
});
stream.on('error', err => reject(err));
}); This is injected into the Promise chain. Since I'm doing something wrong in this code, there is nothing on the event loop and immediately after this code executes, my bundler dies. This seems incredibly bad for an end user. How would they know what was wrong when they write such a loader? My bundler exits with no errors, exit code Should it be my bundlers responsibility to set a huge long timer before any loader, and once the loader is done, call |
This is how I've been able to "solve" the problem from my bundler's perspective: if (loaderChain.length) {
const warningTimer = setTimeout(() => {
console.log('Loader has not resolved within 5 seconds');
}, 5000);
const timer = setTimeout(() => {}, 999999);
code = await handleTransform({ code, loaderChain });
clearTimeout(timer);
clearTimeout(warningTimer);
} |
@tbranyen as long as the stream you wrap is doing things it will work. if the stream absolutely stops but doesn't end or error, nothing will be keeping the process open anymore. |
@devsnek I believe I covered that in my initial comment:
The point here is that something was wrong, and given that everything in the chain was a Promise which either resolves, rejects, or hangs, this behavior feels out-of-place. If you were using my tool and there was no indication that the loader was successful or not (since it never resolves or rejects), and the tool simply dies with a non-error exit code, that's pretty bad for debugging. In a browser, Promises can just hang indefinitely since there is no "process" to close in the web page. In Node, I can see how this behavior is ambiguous since there is a distinction made in Node as to whether v8 or libuv scheduled events determine if the Node process is out of work or not. |
conceptionally speaking this is not good. IMO node should definitely wait until all promises resolved/rejected. I guess it is this way, because it is very difficult to implement? As promises are a V8 internal thing and the node.js mother process doesn't know about them? |
@axkibe It's possible, see #29355 (comment) for a PoC. |
I just got bitten by this as well and it makes sense. I was able to solve it with a dummy timeout of 0 duration: await new Promise(resolve => {
setTimeout(() => null, 0);
}) This actually waits indefinitely or until I call the resolve function. Edit: I was wrong, the dummy timeout with 0 duration does not keep it alive. |
Also pretty sure I am experiencing this while running 80 or so child processes in a promise pool, 4 at a time in parallel. After getting to about 60 processes, or 2 minutes in, the script just exits. I'm going to try putting a massive timeout before running the promise pool and see what happens.
Edit: the above didn't work, but I will share what did. Node doesn't wait for this...
However if you don't return the promise and instead resolve it with await, it will consistently stay alive:
Very interesting engine quirk. |
Just get into this issue. It is frustrated to debug without an message printed to console, telling the programmer that the progress is exited without an ever-pending Promise being resolved. A warning message printed to the console would have saved my day. |
There is the 'beforeExit' event described in node docs
|
This is obviously an issue for a LOT of people, including myself today. There should be a standard and intuitive pattern (e.g. doesn't require explanation of V8 internals during a code review) or builtin that facilitates safe exec of As it stands now, using Python's https://docs.python.org/3/library/asyncio-task.html#running-an-asyncio-program |
To understand this topic we need to understand what "deadlock" is, and how it applies to javascript. In javascript their are multiple fibers running, and they may The reason for deadlock is always design and/or programmer error. At least the probability of any other reason is so small that it is not worth persueing. Instead - redesign and/or solve the programmer error. We can set a callback with
Meanwhile, we set
|
This has all little to do with the issue at hand. There are some promise that can resolve the node core isn't aware of (in my case as far I can remember it has something to do console input, or it was some other library which control IO was a promise. it's long ago, don't ask me for details) , yet it kills if there are only promises left. As you can see above people in this situations create dummy I/O for the node core to not quit, as did I. Yes if there are only promises left whose resolve calls depend on each other, it's a dead lock and a coding error. IMO the node core doesn't need to keep track of that, an coding error is a coding error. And as far I recall in this case it used to exit without error message and exit code 0, which is certainly what no one wants. If you think you can never miss any callback from an extensions library ever then by all means exit with an approperiate error message please. |
This doesn't work because Number.MAX_SAFE_INTEGER is 2^53 − 1 (9007199254740991) and setTimeout only allows a 32-bit integer. (you should see a warning in the console I used this: |
What kind of operations are you using that force you to keep the process alive with |
OK, I digged up my old code when this was an issue and the great gist-hack from ben (above) solved it back then. This isn't active code base for me anymore, since I changed some bigger things an that whole branch got cut off anyway. (It was related to piping a readStream through a json parser and writing async handlers) I cannot reproduce it anymore with my current node (14.13) so whatever it was, that made node exit prematurely back then, I think it has been fixed by now. And from the original post, yes that was a coding error.. |
Note a critical subtlety in both versions of your code:
blah() is awaiting a promise that will never resolve. You dropped the resolution callback on the floor (intentionally? I'm a little confused about all the different cases thrown around in this issue) This slight variant
prints I think it would be better if the first version of the program hung forever instead of exiting (regardless of exit code) but what I, personally, care about more is: Is it guaranteed that version 2 of the program will print |
The point of this issue is the first case. You don't always have control over what promises are in the chain (think middleware). Someone passes in a promise that isn't properly resolved, now the node process completely dies with no indication why. Sure the fix is obvious in your case, but if you have 50 middleware in a massive application, this could be completely dumbfounding. |
Anyway, as long nothing listens anymore (including all the middlewares) to any network event, file event, timer event etc. and there is nothing in the "nextTick" stack either etc. and the code passes control back to the event loop.. other than dying would be halting/zombing forever. AFAIK, there was a rare bug in some cases, where node died, albeit there was still something incoming from a stream.. but it has been fixed. |
This is precisely what happens in the browser and allows you to easily inspect the stack and current runtime, not sure why this would be controversial for Node on the command line. If you were to try and use |
Does it really close while you have an --inspect debugger enabled, because in that case it shouldn't, since its an event source (debugger attaches). If it does, you should file a bug. If it doesn't, its IMO fine. As said, otherwise it would be a zombie process. |
I think @tbranyen is saying JS in the browser stays around so you can attach a debugger. Of course node is not a browser. A I'm not going to reopen this issue - too much meandering discussion - but if someone wants to open a new issue and back-link to this one, go ahead. |
And generally speaking, I can see a point for having node hanging around for a debugger to connect even after it is finished in conventional sense .. don't need a flag for promises for this, generally speaking inspecting data structures even after it's done. On the other hand, this is easy to workaround, just make a dummy listener to any port and it will keep running. With this on the head of the root file, @tbranyen should get what they want. I agree, I don't see any issue as of today. |
We ran into this with the test runner today. I think we should revisit our choice to end the process with promises pending. In our case - we had a test that spawned a child process and transformed its stdout. Its stdout was transformed with async code which meant the parent process exited and did not wait to validate the output. This means code like const result = await fs.createReadStream("./file.txt").map(async (x) => { return await transformAsync(x); }).toArray();
// code below never runs, because nothing is keeping the process alive between the stream being finished
// and the transforms/toArray which run after a microtick.
doSomethingWith(result); |
benajmingr, your code seems faulty, since doSomething() is executed right away, there is nothing waiting for the stream you created to finish. And "result" is a promise you need to wait somewhere for.
Works fine. |
Yeah the "this code doesn't run" bit was obviously missing an await :) |
Currently, when there is an error inside a lazy loaded component contructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ````ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a set closer to address angular#33642
Currently, when there is an error inside a lazy loaded component contructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ````ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a step closer to address angular#33642
Currently, when there is an error inside a lazy loaded component contructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ````ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a step closer to address angular#33642
Currently, when there is an error inside a lazy loaded component constructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ```ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a step closer to address angular#33642
Currently, when there is an error inside a lazy loaded component constructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ```ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a step closer to address angular#33642
Currently, when there is an error inside a lazy loaded component constructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ```ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a step closer to address angular#33642
I know this discussion is closed, but I'm working on a Nest.js application and creating a custom ConfigModule similar to the one provided by the framework, as I need additional custom logic. I ran into this issue when trying to resolve a Promise from outside of its scope. The strange thing is, in the existing Nest.js ConfigModule, there's a similar Promise that does work: Does anyone know why it works there? I'm very confused. |
The code bellow will end with output:
and then it ends. The expected output is:
and the node should not exit
If the
if
statment is set toif (false)
then the process is outputting:and never ends...
The text was updated successfully, but these errors were encountered: