Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/transports/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ module.exports = class Connection extends EventEmitter {
this.debugOut('socketError() ' + err.message);
this.last_socket_error = err;
// this.emit('error', err);

// With a SOCKS proxy there is no socket until createConnection() resolves, so a
// rejection here has nothing bound to it: no 'close' is emitted, Connection's
// socketClose() never runs, and no reconnect is ever scheduled. Emit the close
// ourselves so the normal backoff applies, as the direct path already gets from
// _onSocketCreate().
if (!this.socket) {
this.onSocketClose();
}
}

onSocketTimeout() {
Expand Down
28 changes: 28 additions & 0 deletions test/net.transport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,34 @@ describe('src/transports/net.js', function() {

assert.equal(conn.last_socket_error, err);
});

it('should emit close when there is no socket', function() {
// The SOCKS path assigns this.socket only once createConnection() resolves, so a
// rejection arrives with nothing bound to emit 'close' for it. Without this the
// connection sits in SOCK_CONNECTING forever and never reconnects.
const conn = new Connection({});
const spy = sinon.spy();
const err = new Error('Proxy connection timed out');
conn.on('close', spy);
conn.socket = null;
conn.onSocketError(err);

expect(spy).to.have.been.calledOnce;
expect(spy).to.have.been.calledWith(err);
assert.equal(conn.state, 0); // SOCK_DISCONNECTED
});

it('should not emit close when a socket exists', function() {
// A bound socket emits its own 'close', so synthesising one here would schedule
// two reconnects for a single failure.
const conn = new Connection({});
const spy = sinon.spy();
conn.on('close', spy);
conn.socket = createMockSocket();
conn.onSocketError(new Error('ECONNRESET'));

expect(spy).to.not.have.been.called;
});
});

describe('onSocketTimeout()', function() {
Expand Down