Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does domain.dispose() do in nodejs? Are there any hooks?

Reading the documentation at http://nodejs.org/api/domain.html makes it a little vague: "makes a best effort attempt to clean up any and all IO that is associated with the domain". It mentions that timers are shutdown, which isn't exactly IO. It would be very nice to know the comprehensive list of things domain.dispose does. Does anyone have that list?

Also, is there any way to hook into that functionality - ie allow some custom clean up code to be called when domain.dispose() is run?

like image 716
B T Avatar asked Apr 10 '13 04:04

B T


People also ask

What are hooks in node JS?

Introduction. Async Hooks are a core module in Node. js that provides an API to track the lifetime of asynchronous resources in a Node application. An asynchronous resource can be thought of as an object that has an associated callback.

What is dispose in JavaScript?

Summary. Releases all resources used by the DocumentViewer and deletes the object.

What is domain in node JS?

Node. js domain module is used to intercept unhandled error. These unhandled error can be intercepted using internal binding or external binding. If errors are not handled at all, then they will simply crash the Node application. Internal Binding − Error emitter is executing its code within the run method of a domain.

What is meant by deprecated in node JS?

js deprecation warning is that it points out that in future versions unhandled promise rejection will also lead to process termination. That's a really important piece of information. So, never forget to catch the errors! This logging aligns with Chrome, which shows a console message for not handled promise rejections.


1 Answers

The dispose function calls the exit and dispose functions, removes all listeners, removes all error handlers, and attempts to kill all members of the domain. The function the checks if the domain has a parent, and if it does, then it is removed from the domain. The domain is then set for garbage collection, and the marked as disposed.

From the Node documentation:

Once the domain is disposed the dispose event will emit.

I would go more in-depth on the topic, but the Node source is already nicely annotated.

The timer you are talking about would be here, where the members of the domain are being iterated through.

this.members.forEach(function(m) {
  // if it's a timeout or interval, cancel it.
  clearTimeout(m);
});

Here's from the Node source:

Domain.prototype.dispose = function() {
  if (this._disposed) return;

  // if we're the active domain, then get out now.
  this.exit();

  this.emit('dispose');

  // remove error handlers.
  this.removeAllListeners();
  this.on('error', function() {});

  // try to kill all the members.
  // XXX There should be more consistent ways
  // to shut down things!
  this.members.forEach(function(m) {
    // if it's a timeout or interval, cancel it.
    clearTimeout(m);

    // drop all event listeners.
    if (m instanceof EventEmitter) {
      m.removeAllListeners();
      // swallow errors
      m.on('error', function() {});
    }

    // Be careful!
    // By definition, we're likely in error-ridden territory here,
    // so it's quite possible that calling some of these methods
    // might cause additional exceptions to be thrown.
    endMethods.forEach(function(method) {
      if (typeof m[method] === 'function') {
        try {
          m[method]();
        } catch (er) {}
      }
    });

  });

  // remove from parent domain, if there is one.
  if (this.domain) this.domain.remove(this);

  // kill the references so that they can be properly gc'ed.
  this.members.length = 0;

  // finally, mark this domain as 'no longer relevant'
  // so that it can't be entered or activated.
  this._disposed = true;
};
like image 88
hexacyanide Avatar answered Oct 04 '22 19:10

hexacyanide