How to forcibly keep a Node.js process from terminating?
Asked Answered
W

6

73

TL;DR

What is the best way to forcibly keep a Node.js process running, i.e., keep its event loop from running empty and hence keeping the process from terminating? The best solution I could come up with was this:

const SOME_HUGE_INTERVAL = 1 << 30;
setInterval(() => {}, SOME_HUGE_INTERVAL);

Which will keep an interval running without causing too much disturbance if you keep the interval period long enough.

Is there a better way to do it?

Long version of the question

I have a Node.js script using Edge.js to register a callback function so that it can be called from inside a DLL in .NET. This function will be called 1 time per second, sending a simple sequence number that should be printed to the console.

The Edge.js part is fine, everything is working. My only problem is that my Node.js process executes its script and after that it runs out of events to process. With its event loop empty, it just terminates, ignoring the fact that it should've kept running to be able to receive callbacks from the DLL.

My Node.js script:

var
    edge = require('edge');

var foo = edge.func({
    assemblyFile: 'cs.dll',
    typeName: 'cs.MyClass',
    methodName: 'Foo'
});

// The callback function that will be called from C# code:
function callback(sequence) {
    console.info('Sequence:', sequence);
}

// Register for a callback:
foo({ callback: callback }, true);

// My hack to keep the process alive:
setInterval(function() {}, 60000);

My C# code (the DLL):

public class MyClass
{
    Func<object, Task<object>> Callback;

    void Bar()
    {
        int sequence = 1;

        while (true)
        {
            Callback(sequence++);
            Thread.Sleep(1000);
        }
    }

    public async Task<object> Foo(dynamic input)
    {
        // Receives the callback function that will be used:
        Callback = (Func<object, Task<object>>)input.callback;

        // Starts a new thread that will call back periodically:
        (new Thread(Bar)).Start();

        return new object { };
    }
}

The only solution I could come up with was to register a timer with a long interval to call an empty function just to keep the scheduler busy and avoid getting the event loop empty so that the process keeps running forever.

Is there any way to do this better than I did? I.e., keep the process running without having to use this kind of "hack"?

Wiper answered 13/5, 2014 at 2:50 Comment(6)
You could start a bogus listener on a socket on the node side to keep your process alive.Kizzykjersti
JXcore (a node.js distro) has process.keepAlive and process.release . You could call process.keepAlive() prior to everything else and finally process.release() whenever the application needs to be closed.Pretorius
Thanks, @NurayAltin. I don't plan migrating to JXCore now, but it's good to know it has an API to do that.Wiper
Unless I'm mistaken, you are looking for Number.POSITIVE_INFINITY or simply the Infinity global. I don't believe there is a POSITIVE_INFINITY property in the global Math object.Suppurative
You're right, @richremer. Just fixed it, thanks.Wiper
Updated the question to reflect the fact that Number.POSITIVE_INFINITY can't be used anymore (see docs here and here).Wiper
W
61

The simplest, least intrusive solution

I honestly think my approach is the least intrusive one:

setInterval(() => {}, 1 << 30);

This will set a harmless interval that will fire approximately once every 12 days, effectively doing nothing, but keeping the process running.

Originally, my solution used Number.POSITIVE_INFINITY as the period, so the timer would actually never fire, but this behavior was recently changed by the API and now it doesn't accept anything greater than 2147483647 (i.e., 2 ** 31 - 1). See docs here and here.


Comments on other solutions

For reference, here are the other two answers given so far:

Joe's (deleted since then, but perfectly valid):

require('net').createServer().listen();

Will create a "bogus listener", as he called it. A minor downside is that we'd allocate a port just for that.

Jacob's:

process.stdin.resume();

Or the equivalent:

process.stdin.on("data", () => {});

Puts stdin into "old" mode, a deprecated feature that is still present in Node.js for compatibility with scripts written prior to Node.js v0.10 (reference).

I'd advise against it. Not only it's deprecated, it also unnecessarily messes with stdin.

Wiper answered 23/11, 2017 at 13:33 Comment(7)
Just a quick note that while setInterval is "less intrusive", it does require SIGINT to be sent to the process. The stdin solutions allow you to exit normally by simply sending EOF via ctrl+d.Cristinecristiona
Are you sure about that? Cause AFAIK setInterval is handled internally by libuv. There should be no need for external signals to handle timers whatsoever.Wiper
I am assuming that no code was written to clear the timer. Which means the way most users would stop the program is ctrl+c, which sends SIGINT.Cristinecristiona
Right, but we may be talking about different things. My question was about preventing Node's event loop from running empty and thus finishing the process. It would be fine if the user decided to terminate the process in whatever way though. I just don't want the process to terminate by itself.Wiper
Sweet! I just thought of something though. Im not an expert on Libuv or event loops (I do know a little) but this solution may still not be as good as if there were a natural way for direct access to the event loop (which node doesnt seem to provide). By direct access I mean: a way to either directly configure the behavior of the event loop or a way to add a custom "referenced" Libuv handle directly to the event loop (e.g. as can be done via Neovim's vim.loop). Whereas this solution may be requiring an additional comparison each turn of the loop (in order to check if timer should fire yet)Schlock
Hey @tmillr, I'm no expert on Libuv either, but I'm pretty sure they must use some sort of clever data structure for that, like a priority queue. No matter how many events are scheduled, the algorithm only needs to check the top one (i.e,, the one with the higher priority). So you can rest assured it won't incur any performance penalties in your application.Wiper
@LucioPaiva I suppose I was envisioning a program which is completely idle, and then comparing the performance cost of adding a timer to such a program (& now the timer needs to be checked/polled by the event loop, or perhaps Libuv timers are handled by the OS instead? but I don't think thats the case & even then the loop would need to poll for events from the OS). Anyway Im just realizing now that such a program (one that is completely asleep/idle) is a useless program & isn't a valid hypothetical program to compare against as it would be totally useless & no different from a non-running one.Schlock
G
41

Use "old" Streams mode to listen for a standard input that will never come:

// Start reading from stdin so we don't exit.
process.stdin.resume();
Gyp answered 13/5, 2014 at 3:59 Comment(1)
"...to listen for a standard input that will never come" mmmmm watchya sayyyyyyyyy (sorry I had to)Schlock
R
2

Here is IFFE based on the accepted answer:

(function keepProcessRunning() {
  setTimeout(keepProcessRunning, 1 << 30);
})();

and here is conditional exit:

let flag = true;
(function keepProcessRunning() {
  setTimeout(() => flag && keepProcessRunning(), 1000);
})();
Racehorse answered 9/5, 2022 at 13:29 Comment(1)
Why not just use setInterval()?Schlock
S
0

You could use a setTimeout(function() {""},1000000000000000000); command to keep your script alive without overload.

Satisfy answered 14/3, 2021 at 22:9 Comment(0)
I
0

spin up a nice repl, node would do the same if it didn't receive an exit code anyway:

import("repl").then(repl=>
repl.start({prompt:"\x1b[31m"+process.versions.node+": \x1b[0m"}));
Isherwood answered 9/8, 2022 at 14:45 Comment(0)
W
-3

I'll throw another hack into the mix. Here's how to do it with Promise:

new Promise(_ => null);

Throw that at the bottom of your .js file and it should run forever.

Wafer answered 25/3, 2019 at 18:21 Comment(2)
This is incorrect; explanation.Difference
Unfortunately this doesn't work. The process just exits immediately.Wiper

© 2022 - 2024 — McMap. All rights reserved.