Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

socket.on('error') with socket.destroy() emits 'close' event twice #2223

Closed
baalexander opened this issue Nov 29, 2011 · 2 comments
Closed
Labels

Comments

@baalexander
Copy link

The following code will trigger the 'close' event twice on a socket in node v0.6.x. The close event was only fired once with v0.4.x. This was tested with Ubuntu and Mac OS X.

var net    = require('net')
  , Socket = net.Socket;

var socket = new Socket();

socket.on('error', function(exception) {
  console.log('SOCKET ERROR');
  socket.destroy();
})

socket.on('close', function(exception) {
  console.log('SOCKET CLOSED');
})

socket.connect(9000, 'localhost');

Output:

SOCKET ERROR
SOCKET CLOSED
SOCKET CLOSED

I can easily just not call socket.destroy() and the 'close' event will only be emitted once. However, I thought it was worth bringing up as this wasn't the case in v0.4.x and may be a regression issue.

@bnoordhuis
Copy link
Member

Thanks for the report. It's fixed in 03eb41c.

@baalexander
Copy link
Author

Thanks @bnoordhuis for the quick response!

alexkwolfe pushed a commit to alexkwolfe/node that referenced this issue Dec 23, 2011
Don't allow `socket.destroy()` to run twice. The self-destruct sequence itself
is idempotent but it makes the 'close' and 'error' events fire more than once,
which may confuse listeners.

Fixes nodejs#2223.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants