Skip to content

Migrating from object.ref()/object.unref() to process.ref()/process.unref() #53266

Open
@jasnell

Description

@jasnell

Node.js has long had a pattern of attaching a ref() and unref() method to various i/o objects that are bound to the event loop. Calling thing.unref() makes it so the thing does not keep the event loop alive.

For instance,

const i = setInterval(() => {}, 1000);
i.unref();

While this has worked effectively for Node.js specific APIs, it's rather cumbersome with web platform standard APIs. I'd like to propose a change: Introduce new process.unref(thing) and process.ref(thing) API that would accept multiple different kinds of ref'able types.

const i = setInterval(() => {}, 1000);
process.unref(i);
// etc

A number of API objects that currently have .ref()/.unref() methods:

  • dgram.Socket
  • net.Socket
  • net.Server
  • child_process.ChildProcess
  • child_process.Control
  • StatWatcher
  • FSWatcher
  • MessagePort
  • Timeout
  • Interval
  • Worker
  • Http2Session
  • BroadcastChannel

The idea would be to mark the existing object-specific ref() and unref() methods as legacy and depend on the process.ref()/process.unref() as the supported mechanism moving forward.

/cc @mcollina

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions