Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Domain binding on defining event listeners #3646

Closed
wants to merge 4 commits into from

4 participants

Ovidiu Cherecheș Ben Noordhuis isaacs Luke Arduini
Ovidiu Cherecheș

Premise: Domains work best only in a synchronous flow. To cover async situations, though, timers bind current domain to their callbacks and event emitters store it in their constructor. (maybe other stuff as well...)

Problem: Async callbacks returned from low-level libraries, like in the case of net streams, are not covered. If the event emitter was not defined inside a certain domain, nor will listeners added to it get bound to any present domain.

Solution: Binding domains on adding event listeners and activating them on emit. (similar to how intervals handle domains)

Ben Noordhuis

LGTM.

@isaacs You should review this.

isaacs
Owner

This is a pretty significant semantics change. Also, if I'm reading this right, you're going to get into trouble if you do things like:

d1 = domain.create()
d2 = domain.create()
d1.enter()
ee1.on('foo', handle)
d1.exit()
d2.enter()
ee2.on('foo', handle)
d2.exit()
ee1.emit('foo')

Since the relevant domain becomes the one on the function rather than the EventEmitter, it's going to be confusing: I've added an EE to a given domain, but something else was active when the ee.on() was called, and now the ee's domain is not the one it's bound to all the time.

The idea is that a domain "owns" the emitter object itself. If a single EE object is switching domains for different handlers, then that's strange. Also, a single event handler could be applied at different times to different emitters (this is quite common, in fact), and you'll get really odd crosstalk, since the last one to attach it to any event is the one that's going to catch errors it throws.

Ovidiu Cherecheș

How about if we inverted the order of loading the domains? That is, if the event emitter has already been added to a domain then that one will be activated, otherwise, fallback on the one present when the listener was defined (if any).

This way it won't change any present behavior, but instead cover more domain "holes."

lmk

isaacs
Owner

Can you explain the "hole" (ie, the use case that you expect to be covered, which isn't already) in a message to the nodejs-dev list?

I'm not against it in principle, but it should be discussed, since it's a change in semantics. Let's first get some agreement about the problem and the expected behavior.

Thanks.

Luke Arduini

Looks like this went stale, is it still up for discussion?

isaacs isaacs closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 70 additions and 0 deletions.
  1. +17 −0 lib/events.js
  2. +53 −0 test/simple/test-domain-stream.js
17 lib/events.js
View
@@ -79,6 +79,9 @@ EventEmitter.prototype.emit = function() {
if (this.domain) {
this.domain.enter();
}
+ if (handler.domain) {
+ handler.domain.enter();
+ }
switch (arguments.length) {
// fast cases
case 1:
@@ -97,6 +100,9 @@ EventEmitter.prototype.emit = function() {
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
handler.apply(this, args);
}
+ if (handler.domain) {
+ handler.domain.exit();
+ }
if (this.domain) {
this.domain.exit();
}
@@ -112,7 +118,13 @@ EventEmitter.prototype.emit = function() {
var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
+ if (listeners[i].domain) {
+ listeners[i].domain.enter();
+ }
listeners[i].apply(this, args);
+ if (listeners[i].domain) {
+ listeners[i].domain.exit();
+ }
}
if (this.domain) {
this.domain.exit();
@@ -131,6 +143,11 @@ EventEmitter.prototype.addListener = function(type, listener) {
if (!this._events) this._events = {};
+ // Attach current domain to listener
+ if (process.domain) {
+ listener.domain = process.domain;
+ }
+
// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
this.emit('newListener', type, typeof listener.listener === 'function' ?
53 test/simple/test-domain-stream.js
View
@@ -0,0 +1,53 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+// Tests of domain covering stream callbacks.
+
+var assert = require('assert');
+var common = require('../common');
+var domain = require('domain');
+var events = require('events');
+var net = require('net');
+
+var server = net.createServer();
+var dom = domain.create();
+
+dom.on('error', function(error) {
+ console.log('Exception caught by domain!');
+});
+
+dom.run(function() {
+ server.on('connection', function(socket) {
+ socket.end();
+ server.close();
+ throw new Error('Exception inside callback inside domain!');
+ });
+});
+
+var port = common.PORT;
+server.listen(port, function() {
+ net.createConnection(port);
+});
+
+process.on('exit', function() {
+ console.log('ok');
+});
Something went wrong with that request. Please try again.