Skip to content

Commit

Permalink
domains: deprecate domain.dispose().
Browse files Browse the repository at this point in the history
Follows @isaacs's recommendations in nodejs/node-v0.x-archive#5018. Includes some
updates to documentation but not examples.

Conflicts:
	lib/domain.js
  • Loading branch information
othiym23 authored and isaacs committed Aug 27, 2013
1 parent 645418e commit d86814a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 56 deletions.
22 changes: 6 additions & 16 deletions doc/api/domain.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -420,21 +420,11 @@ without exiting the domain.

### domain.dispose()

The dispose method destroys a domain, and makes a best effort attempt to
clean up any and all IO that is associated with the domain. Streams are
aborted, ended, closed, and/or destroyed. Timers are cleared.
Explicitly bound callbacks are no longer called. Any error events that
are raised as a result of this are ignored.

The intention of calling `dispose` is generally to prevent cascading
errors when a critical part of the Domain context is found to be in an
error state.

Once the domain is disposed the `dispose` event will emit.

Note that IO might still be performed. However, to the highest degree
possible, once a domain is disposed, further errors from the emitters in
that set will be ignored. So, even if some remaining actions are still
in flight, Node.js will not communicate further about them.
Stability: 0 - Deprecated. Please recover from failed IO actions
explicitly via error event handlers set on the domain.

Once `dispose` has been called, the domain will no longer be used by callbacks
bound into the domain via `run`, `bind`, or `intercept`, and a `dispose` event
is emitted.

[EventEmitter]: events.html#events_class_events_eventemitter
43 changes: 3 additions & 40 deletions lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ var events = require('events');
var EventEmitter = events.EventEmitter;
var inherits = util.inherits;

// methods that are called when trying to shut down explicitly bound EEs
var endMethods = ['end', 'abort', 'destroy', 'destroySoon'];

// communicate with events module, but don't require that
// module to have to load this one, since this module has
// a few side effects.
Expand Down Expand Up @@ -259,53 +256,19 @@ Domain.prototype.bind = function(cb, interceptError) {
return b;
};

Domain.prototype.dispose = function() {
Domain.prototype.dispose = util.deprecate(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 (util.isFunction(m[method])) {
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'
// mark this domain as 'no longer relevant'
// so that it can't be entered or activated.
this._disposed = true;
};
});

0 comments on commit d86814a

Please sign in to comment.