question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unref new connections

See original GitHub issue

Node process stays live as long as there are open connections, so when running a short-lived process, the Redis connection prevents the process from exiting when there’s no more work to be done.

Right now we use this:

  client.on('connect', function() {
    client.connector.stream.unref();
  });

A proper API, connection option, or default behavior would help here.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:7
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
wallingcommented, Apr 7, 2016

I could definitely use this feature. unrefWhenDrain would also be fitting for my use-case.

1reaction
assafcommented, May 26, 2015

‘unref’ is not protocol aware, it can’t infer intent. It’s up to the application developer to use it correctly.

If you’re running an HTTP server, you want to keep it alive while the socket is open, so you’re not going to unref. If you have a socket for logging, you don’t want that socket to keep the process from exiting, so you’re going to unref it.

Redis, like raw sockets, supports many different use cases. If you’re using it for storage/cache, primarily read/write operations, then you probably want to unref. If you’re using Redis for queue processing, or listening to pub/sub messages, you don’t want to unref.

So this could be a connection open or API call on the connection, and let the application developer decide when to use this option based on use case. Or it could be handled by ioredis depending on which command is currently executing (e.g. unref for get and ref for brpop).


There’s a different issue with get. From looking at the code, it looks like every command that executes will block until something happens on the socket. If the Redis server goes away (crash, network issue) nothing happens on the socket, and the command will hang forever.

A server that shuts down will send a FIN packet which tells Node to close the connection. But if the server goes away there’s no chance of that happening. TCP can handle that by detecting no activity on the socket — the server can be configured to always send something (keep-alive), and the client has a timeout — but that seems to be turned off.

But even when used, socket timeouts are not good enough. You want the timeout long enough so it doesn’t disconnect during periods of inactivity, but you don’t clients waiting forever, so you need a quick way to detect crashed servers, by having a shorter timeout on the command.

Commonly this is done with setTimeout, so you have a timer running while executing a command and waiting for it to complete, which keeps the process running until the timeout is cleared (command completed, successfully or not). So that would fix the problem with get.

brpop is a bit more tricky, because you want it to wait, possibly forever, but only if the server is alive. So one option is to let TCP detect a crashed server using keep-alive and timeout, but I’m not sure if Redis supports that. The other option is to use timeouts, then do some noop to see if Redis still responding, and then repeat the command.

That mechanism will likely also use setTimeout, so if you have a way of dealing with crashed servers that uses timeouts, you can unref the underlying socket.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unblocking Node With Unref() - HTTP Toolkit
It holds a queue of tasks to run, and runs them, one by one. New tasks appear on the queue while it runs,...
Read more >
net.Socket.unref JavaScript and Node.js code examples
Best JavaScript code snippets using net.Socket.unref(Showing top 11 results out of 315) · src/transporters/tcp/tcp-writer.js/TcpWriter/connect · gramjs/extensions ...
Read more >
Net | Node.js v19.3.0 Documentation
Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the server is finally closed when all connections ......
Read more >
Class: Mongos
new Mongos(servers, options){Mongos} ... Number of connections in the connection pool for each server instance, set to 5 as default ... Unref all...
Read more >
[NODE-2469] Add a message to unref - MongoDB Jira
The Node.js driver's support for unref'ing its connections' sockets ... of the different `unref`s I can find (topology, s, connection, db), ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found