Skip to content
This repository

events: Don't assume .domain is a Domain instance #3998

Closed
wants to merge 1 commit into from

4 participants

Ben Taber Ben Noordhuis Nodejs Jenkins Isaac Z. Schlueter
Ben Taber

EventEmitter assumes that anything with a .domain property is an instance of a
Domain.Domain. This clobbers any EventEmitter instances/inheriters that make
use of the fairly generalized property name. Change is backwards compatible
with existing interface. New test included.

Fixes #3922

I know pull request 3932 is already open, but this would allow this to be fixed
in the next v0.8 release as opposed to 1.0.

#3932

Ben Taber bentaber events: Don't assume .domain is a Domain instance
EventEmitter assumes that anything with a .domain property is an instance of a
Domain.Domain.  This clobbers any EventEmitter instances/inheriters that make
use of the fairly generalized property name.  Change is backwards compatible
with existing interface.  New test included.

Fixes #3922
9d4b1c2
Nodejs Jenkins
Collaborator

Can one of the admins verify this patch?

Isaac Z. Schlueter
Collaborator

This has been resolved by other issues, and besides, does not merge cleanly. Re-submit if it's still a thing.

Isaac Z. Schlueter isaacs closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 11, 2012
Ben Taber bentaber events: Don't assume .domain is a Domain instance
EventEmitter assumes that anything with a .domain property is an instance of a
Domain.Domain.  This clobbers any EventEmitter instances/inheriters that make
use of the fairly generalized property name.  Change is backwards compatible
with existing interface.  New test included.

Fixes #3922
9d4b1c2
This page is out of date. Refresh to see the latest.
14 lib/events.js
@@ -22,6 +22,10 @@
22 22 var isArray = Array.isArray;
23 23 var domain;
24 24
  25 +function isDomain(ee) {
  26 + return domain && ee.domain && ee.domain instanceof domain.Domain;
  27 +}
  28 +
25 29 function EventEmitter() {
26 30 if (exports.usingDomains) {
27 31 // if there is an active domain, then attach to it.
@@ -53,7 +57,7 @@ EventEmitter.prototype.emit = function() {
53 57 if (!this._events || !this._events.error ||
54 58 (isArray(this._events.error) && !this._events.error.length))
55 59 {
56   - if (this.domain) {
  60 + if (isDomain(this)) {
57 61 var er = arguments[1];
58 62 er.domain_emitter = this;
59 63 er.domain = this.domain;
@@ -76,7 +80,7 @@ EventEmitter.prototype.emit = function() {
76 80 if (!handler) return false;
77 81
78 82 if (typeof handler == 'function') {
79   - if (this.domain) {
  83 + if (isDomain(this)) {
80 84 this.domain.enter();
81 85 }
82 86 switch (arguments.length) {
@@ -97,13 +101,13 @@ EventEmitter.prototype.emit = function() {
97 101 for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
98 102 handler.apply(this, args);
99 103 }
100   - if (this.domain) {
  104 + if (isDomain(this)) {
101 105 this.domain.exit();
102 106 }
103 107 return true;
104 108
105 109 } else if (isArray(handler)) {
106   - if (this.domain) {
  110 + if (isDomain(this)) {
107 111 this.domain.enter();
108 112 }
109 113 var l = arguments.length;
@@ -114,7 +118,7 @@ EventEmitter.prototype.emit = function() {
114 118 for (var i = 0, l = listeners.length; i < l; i++) {
115 119 listeners[i].apply(this, args);
116 120 }
117   - if (this.domain) {
  121 + if (isDomain(this)) {
118 122 this.domain.exit();
119 123 }
120 124 return true;
52 test/simple/test-domain-clobber-property.js
... ... @@ -0,0 +1,52 @@
  1 +// Copyright Joyent, Inc. and other Node contributors.
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining a
  4 +// copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to permit
  8 +// persons to whom the Software is furnished to do so, subject to the
  9 +// following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be included
  12 +// in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15 +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17 +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18 +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19 +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20 +// USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +
  22 +// Make sure using domains doesn't clobber eventemitters with .domain properties
  23 +// https://github.com/joyent/node/issues/3922
  24 +
  25 +var assert = require('assert');
  26 +var events = require('events');
  27 +var util = require('util');
  28 +
  29 +var Emitter = function() {
  30 + this.domain = 'abc.com';
  31 +};
  32 +util.inherits(Emitter, events.EventEmitter);
  33 +
  34 +
  35 +function test(callback) {
  36 + var emitter = new Emitter();
  37 +
  38 + emitter.on('test', function() {
  39 + assert.equal('abc.com', emitter.domain);
  40 + if (callback) callback();
  41 + });
  42 +
  43 + assert.doesNotThrow(function() {
  44 + emitter.emit('test');
  45 + });
  46 +}
  47 +
  48 +test(function() {
  49 + // require ('domain') and try it again
  50 + var domain = require('domain');
  51 + test();
  52 +});

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.