-->

How to forcibly keep a Node.js process from termin

2019-03-09 05:58发布

问题:

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"?

回答1:

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();


回答2:

Although it looks like there's no right answer to this question, so far we have 3 possible hacks and I honestly think my approach is the less intrusive one:

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

This will set an interval that will fire approximately once every 12 days.

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

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

Joe's:

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