diff --git a/benchmark/next-tick-2.js b/benchmark/next-tick-2.js new file mode 100644 index 00000000000..44a2b41b543 --- /dev/null +++ b/benchmark/next-tick-2.js @@ -0,0 +1,41 @@ +// 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. + +var count = 2e6, + left = count, + start; + +function onNextTick() { + if (--left) { + process.nextTick(onNextTick); + } else { + finalize(); + } +} + +function finalize() { + var duration = (new Date()).getTime() - start, + ticksPerSec = count / duration * 1000; + console.log("nextTick callbacks per second: " + Math.round(ticksPerSec)); +} + +start = (new Date()).getTime(); +process.nextTick(onNextTick); diff --git a/doc/about/index.html b/doc/about/index.html index 4e67c9e3364..f3a33554f48 100644 --- a/doc/about/index.html +++ b/doc/about/index.html @@ -1,7 +1,8 @@ - - - + + - node.js @@ -109,16 +109,16 @@ console.log('Server running at http://127.0.0.1:1337/'); is a trademark of Joyent, Inc. - - - + + + - - - - + + @@ -18,7 +18,6 @@ type="application/rss+xml" title="node blog" href="http://feeds.feedburner.com/nodejs/123123123"> - node.js @@ -148,16 +147,16 @@ is a trademark of Joyent, Inc. - - - - + + \ No newline at end of file diff --git a/doc/index.html b/doc/index.html index f687c1495c3..fab47811bbd 100644 --- a/doc/index.html +++ b/doc/index.html @@ -1,7 +1,8 @@ - + - - - + + - node.js @@ -200,16 +200,16 @@ server.listen(1337, "127.0.0.1"); is a trademark of Joyent, Inc. - - - + + + - - - - - diff --git a/lib/buffer.js b/lib/buffer.js index ebeab7d5226..a0210b9b876 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -698,7 +698,7 @@ Buffer.prototype.readInt8 = function(offset, noAssert) { }; function readInt16(buffer, offset, isBigEndian, noAssert) { - var neg; + var neg, val; if (!noAssert) { assert.ok(typeof (isBigEndian) === 'boolean', @@ -729,7 +729,7 @@ Buffer.prototype.readInt16BE = function(offset, noAssert) { }; function readInt32(buffer, offset, isBigEndian, noAssert) { - var neg; + var neg, val; if (!noAssert) { assert.ok(typeof (isBigEndian) === 'boolean', diff --git a/lib/net.js b/lib/net.js index d57d4c43e92..b6697136d2f 100644 --- a/lib/net.js +++ b/lib/net.js @@ -443,6 +443,8 @@ Socket.prototype.write = function(data, arg1, arg2) { Socket.prototype._write = function(data, encoding, cb) { timers.active(this); + if (!this._handle) throw new Error('This socket is closed.'); + // `encoding` is unused right now, `data` is always a buffer. var writeReq = this._handle.write(data); diff --git a/lib/timers.js b/lib/timers.js index 35085e0c15a..7a57d342776 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -108,8 +108,8 @@ var unenroll = exports.unenroll = function(item) { list.close(); delete lists[item._idleTimeout]; } - //if active is called later, then we want to make sure not to insert again - delete item._idleTimeout; + // if active is called later, then we want to make sure not to insert again + item._idleTimeout = -1; }; @@ -151,17 +151,18 @@ exports.setTimeout = function(callback, after) { if (after <= 0) { // Use the slow case for after == 0 timer = new Timer(); + timer._callback = callback; if (arguments.length <= 2) { timer._onTimeout = function() { - callback(); - timer.close(); + this._callback(); + this.close(); } } else { var args = Array.prototype.slice.call(arguments, 2); timer._onTimeout = function() { - callback.apply(timer, args); - timer.close(); + this._callback.apply(timer, args); + this.close(); } } diff --git a/test/simple/test-net-write-after-close.js b/test/simple/test-net-write-after-close.js new file mode 100644 index 00000000000..5b100b66265 --- /dev/null +++ b/test/simple/test-net-write-after-close.js @@ -0,0 +1,49 @@ +// 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. + +var common = require('../common'); +var assert = require('assert'); +var net = require('net'); + +var gotError = false; + +process.on('exit', function() { + assert(gotError); +}); + +var server = net.createServer(function(socket) { + setTimeout(function() { + assert.throws( + function() { + socket.write('test'); + }, + /This socket is closed/ + ); + server.close(); + gotError = true; + }, 250); +}); + +server.listen(common.PORT, function() { + var client = net.connect(common.PORT, function() { + client.end(); + }); +});