Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Weird bug caused by on.('error') being called too long after net.createConnection #26

Closed
wants to merge 2 commits into from

2 participants

@addrummond

No description provided.

addrummond added some commits
@addrummond addrummond .on('error') handler needs to be set right after net.createConnection…
… is called.

Previouisly, if ImapConnection.connect was called with a non-existent hostname, an error would be raised before the error handler was set, and would percolate to the top level as an unhandled exception.

(Q: Is there ever any guarantee that the error handler will be set before an error is raised? It's not clear to me how node could guarantee this, but it seems like something one would like to guarantee.)
3b13a9f
@addrummond addrummond Set 'error' handler first. 5ae4138
@mscdex mscdex closed this pull request from a commit
@mscdex Fix sequence number-based functions and do not connect to the server …
…before all event handlers are attached first

Fixes #46
Fixes #26
b0e88cf
@mscdex mscdex closed this in b0e88cf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 8, 2011
  1. @addrummond

    .on('error') handler needs to be set right after net.createConnection…

    addrummond authored
    … is called.
    
    Previouisly, if ImapConnection.connect was called with a non-existent hostname, an error would be raised before the error handler was set, and would percolate to the top level as an unhandled exception.
    
    (Q: Is there ever any guarantee that the error handler will be set before an error is raised? It's not clear to me how node could guarantee this, but it seems like something one would like to guarantee.)
  2. @addrummond

    Set 'error' handler first.

    addrummond authored
This page is out of date. Refresh to see the latest.
Showing with 26 additions and 26 deletions.
  1. +26 −26 imap.js
View
52 imap.js
@@ -98,6 +98,32 @@ ImapConnection.prototype.connect = function(loginCb) {
this._reset();
this._state.conn = net.createConnection(this._options.port, this._options.host);
+ this._state.conn.on('error', function(err) {
+ clearTimeout(self._state.tmrConn);
+ if (self._state.status === STATES.NOCONNECT)
+ loginCb(new Error('Unable to connect. Reason: ' + err));
+ self.emit('error', err);
+ debug('Error occurred: ' + err);
+ });
+ this._state.conn.on('connect', function() {
+ clearTimeout(self._state.tmrConn);
+ debug('Connected to host.');
+ self._state.conn.cleartext.write('');
+ self._state.status = STATES.NOAUTH;
+ });
+ this._state.conn.on('ready', function() {
+ fnInit();
+ });
+ this._state.conn.on('end', function() {
+ self._reset();
+ debug('FIN packet received. Disconnecting...');
+ self.emit('end');
+ });
+ this._state.conn.on('close', function(had_error) {
+ self._reset();
+ debug('Connection forcefully closed.');
+ self.emit('close', had_error);
+ });
this._state.tmrConn = setTimeout(this._fnTmrConn.bind(this),
this._options.connTimeout, loginCb);
@@ -115,15 +141,6 @@ ImapConnection.prototype.connect = function(loginCb) {
this._state.conn.cleartext = this._state.conn;
}
- this._state.conn.on('connect', function() {
- clearTimeout(self._state.tmrConn);
- debug('Connected to host.');
- self._state.conn.cleartext.write('');
- self._state.status = STATES.NOAUTH;
- });
- this._state.conn.on('ready', function() {
- fnInit();
- });
this._state.conn.cleartext.on('data', function(data) {
if (data.length === 0) return;
var trailingCRLF = false, literalInfo;
@@ -479,23 +496,6 @@ ImapConnection.prototype.connect = function(loginCb) {
// unknown response
}
});
- this._state.conn.on('end', function() {
- self._reset();
- debug('FIN packet received. Disconnecting...');
- self.emit('end');
- });
- this._state.conn.on('error', function(err) {
- clearTimeout(self._state.tmrConn);
- if (self._state.status === STATES.NOCONNECT)
- loginCb(new Error('Unable to connect. Reason: ' + err));
- self.emit('error', err);
- debug('Error occurred: ' + err);
- });
- this._state.conn.on('close', function(had_error) {
- self._reset();
- debug('Connection forcefully closed.');
- self.emit('close', had_error);
- });
};
ImapConnection.prototype.isAuthenticated = function() {
Something went wrong with that request. Please try again.