Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Process never exits after statsd use #71

Open
bryanburgers opened this issue Nov 17, 2017 · 4 comments
Open

Process never exits after statsd use #71

bryanburgers opened this issue Nov 17, 2017 · 4 comments

Comments

@bryanburgers
Copy link

bryanburgers commented Nov 17, 2017

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 bryanburgers changed the title Process never exits after statsd-use Process never exits after statsd use Nov 17, 2017
@zaccharles
Copy link

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

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

@rgitzel
Copy link

rgitzel commented Mar 12, 2018

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.

@sandinmyjoints
Copy link

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

@RichardWright
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants