node-statsd icon indicating copy to clipboard operation
node-statsd copied to clipboard

Process never exits after statsd use

Open bryanburgers opened this issue 7 years ago • 4 comments

The code

'use strict'

setInterval(() => {
    console.log('Still alive')
}, 1000).unref()

setTimeout(() => {
    console.log('Done')
}, 500)

prints "Done" then exits.

$ node index.js
Done
$

However, once I introduce a statsd client, the process never exists.

'use strict'

const StatsD = require('node-statsd')
const statsd = new StatsD()

setInterval(() => {
    console.log('Still alive')
}, 1000).unref()

setTimeout(() => {
    statsd.increment('my_counter')
    console.log('Done')
}, 500)

The output is

$ node index.js
Done
Still alive
Still alive
Still alive
Still alive
Still alive
Still alive
Still alive
Still alive
Still alive
...

Is there a way to tell statsd that it should cleanup and not keep the node process alive?

bryanburgers avatar Nov 17 '17 14:11 bryanburgers

statsd.close(); should resolve this. Or, if you want:

statsd.increment('my_counter', null, null, null, function() {
    statsd.close();
});

zaccharles avatar Dec 19 '17 16:12 zaccharles

I'm just new to this library today, and maybe I'm doing something wrong, but adding a call to close() seems to result in no packets being sent.

Here's my (Typescript) code:

import {StatsD} from "node-statsd";

const client = new StatsD(
    {
        host: "127.0.0.1",
        port: 8125
    }
);

client.increment("" + process.pid);
console.log(process.pid);

// client.close();

With the last line commented out, I see the increment message on my Statsd server, but the process doesn't end, as the OP found.

When I uncomment the close() call, the process ends, but no message is sent.

This is on OSX, Node 8.6.0, and node-statsd 0.1.1.

rgitzel avatar Mar 12 '18 16:03 rgitzel

@rgitzel Move the call to close inside of a callback from client.increment -- see the way @zaccharles wrote it in the post above yours. That way the client won't be closed until after the packet is sent.

sandinmyjoints avatar Oct 05 '18 17:10 sandinmyjoints

Hi everyone, if using this lib in aws lambda, the event loop never becomes empty and it doesn't exist until time out. Would it be possible to capture this requirement somewhere in the docs? I would be happy to do so if someone would advise on my pr?

Thanks

RichardWright avatar Jan 31 '19 14:01 RichardWright