joyent/node — Issue #2223: socket.on('error') with socket.destroy() emits 'close' event twice
Author: baalexander
Created: 2011-11-29 21:49:49+00:00
State: open

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.

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

var socket = new Socket();

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

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

socket.connect(9000, 'localhost');
```

Output:

```bash
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.