OSX Mavericks - Getting ECONNRESET #329

Closed
brandon-bethke-neudesic opened this Issue Oct 30, 2013 · 12 comments

Projects

None yet

6 participants

@brandon-bethke-neudesic

After upgrading to Mavericks for Mac, I am getting the following error. It seems to occur after an imap.search. The error doesn't always happen, it sometimes takes 3 - 5 times running the script, but it does happen and it never happened until I upgraded to Mavericks.

{ [Error: read ECONNRESET]
code: 'ECONNRESET',
errno: 'ECONNRESET',
syscall: 'read',
source: 'socket' }

Node v0.10.21
Here is my script. Before running, just replace the username and password.

var Imap = require('imap');

var doSomething = function(emailAddress, callback) {
    var error;
    var imap = new Imap({
        user: 'xxxxxxxx@gmail.com',
        password: 'xxxxxxxxxx',
        host: 'imap.gmail.com',
        port: 993,
        tls: true,
        tlsOptions: { rejectUnauthorized: false }
    });

    var messagesToDelete = [];
    imap.once('ready', function() {  
        console.log('ready');
        imap.openBox('INBOX', function(err, box) {
            console.log('inbox opened');

            if (err) { 
                error = err; 
                return imap.end();
            }

            imap.search([ 'ALL', ['TO', emailAddress], ['HEADER', 'SUBJECT', 'Reset your password'] ], function(err, results) {
                console.log("search done");
                if (err) { 
                    error = err; 
                    return imap.end();
                }

                if(results.length == 0) {
                    console.log('no results');
                    return imap.end();
                }

                var f = imap.fetch(results, { bodies: '' });

                f.on('message', function(msg, seqno) {
                    msg.on('body', function(stream, info) {
                        var buffer = '';
                        stream.on('data', function(chunk) {
                            buffer += chunk.toString('utf8');
                        });
                        stream.once('end', function() {                            
                        });
                    });
                    msg.once('attributes', function(attrs) {   
                        messagesToDelete.push(attrs.uid);
                    });
                    msg.once('end', function() {   
                    });
                });

                f.once('error', function(err) {
                    error = err; 
                    //return imap.end();
                });

                f.once('end', function() {
                    imap.setFlags(messagesToDelete, 'Deleted', function(err) { 
                        if(err) error = err;
                        imap.closeBox(function(err) {
                            imap.end();
                        });
                    });
                });
            });
        });
    });

    imap.once('error', function(err) {
        error = err;
        console.log(error);
        imap.end();
    });

    imap.once('close', function(err) {
        console.log("close");
    });

    imap.once('end', function() {
        console.log('imap end');
        callback(error);
    });

    imap.connect();
};

doSomething('xxx@gmail.com', function(err) {
    if(err) {
        console.log(err);
        process.exit(1);
    }
    else console.log('done');
});
@mscdex
Owner
mscdex commented Oct 30, 2013

Node version? node-imap version?

Can you set debug: console.log in the constructor object and upload the output somewhere (for when it does disconnect unexpectedly)?

@brandon-bethke-neudesic

node v0.10.21
node-imap v0.8.4

Here is the debug output from my script with the error. ( I edited the username and password )

neudesics-imac:test-groupplace Neudesic$ node ./src/test.js
[connection] Connected to host
<= '* OK Gimap ready for requests from 70.165.44.194 j8if3294862oax.73'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN'
<= 'A0 OK Thats all she wrote! j8if3294862oax.73'
=> 'A1 LOGIN "xxxxxxxx@gmail.com" "xxxxxxxxxx"'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH'
<= 'A1 OK xxxxxxxx@gmail.com GroupPlace Users authenticated (Success)'
=> 'A2 NAMESPACE'
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A2 OK Success'
=> 'A3 LIST "" ""'
<= '* LIST (\\Noselect) "/" "/"'
<= 'A3 OK Success'
ready
=> 'A4 SELECT "INBOX" (CONDSTORE)'
<= '* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)'
<= '* OK [PERMANENTFLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen \\*)] Flags permitted.'
<= '* OK [UIDVALIDITY 1] UIDs valid.'
<= '* 0 EXISTS'
<= '* 0 RECENT'
<= '* OK [UIDNEXT 2266] Predicted next UID.'
<= '* OK [HIGHESTMODSEQ 172664]'
<= 'A4 OK [READ-WRITE] INBOX selected. (Success)'
inbox opened
=> 'A5 UID SEARCH ALL TO "xxx@gmail.com" HEADER "SUBJECT" "Reset your password"'
<= '* SEARCH'
<= 'A5 OK SEARCH completed (Success)'
search done
no results
=> 'A6 LOGOUT'
<= '* BYE LOGOUT Requested'
<= 'A6 OK 73 good day (Success)'
[connection] Error: Error: read ECONNRESET
{ [Error: read ECONNRESET]
  code: 'ECONNRESET',
  errno: 'ECONNRESET',
  syscall: 'read',
  source: 'socket' }
[connection] Closed
close
@mscdex
Owner
mscdex commented Oct 30, 2013

Can you try this patch and see if it helps any?

@brandon-bethke-neudesic

Sorry. Those changes did not help. The same error occurs. Additionally, I printed out this._stream.readable, and the value is true.

@mscdex
Owner
mscdex commented Oct 30, 2013

I'm at a loss then. You might post on node's github issue tracker. Maybe it's some weird TLS-related edge case? shrug

@joshrouwhorst

I just started getting this, too. I was playing with the code late last night on my Mac (OSX Mavericks) and didn't get this error at all and throughout the day (Windows 7) didn't have this issue but now tonight I'm getting it on and off with my Mac. I think it's something on Gmail's end. My theory is that they get swamped at certain times of the day and requests get kicked back.

I setup a timeout in the event of this error to retry the process again and that seems to have worked, so far at least. Hope that helps!

Related question for mscdex, should I have imap.end() in the error handler or is it unneeded?

imap.once('error', function(err) {
  if ( err.toString() === "Error: read ECONNRESET" && 
      ++attempts <= maximumAttempts){
    console.log( "ERROR: Attempt " + attempts);
    setTimeout( collectFunction, 500 );
  }

  imap.end();
});

Edit: Yeah, sometimes it works first attempt. Sometimes it takes one or two but eventually I do get it to collect the data.

@mscdex
Owner
mscdex commented Nov 1, 2013

You shouldn't need imap.end(); for the 'error' event.

@mscdex mscdex closed this Dec 28, 2013
@Zugwalt
Zugwalt commented Dec 30, 2013

I'm still getting this error every time I call imap.end(). OSX 10.9, node 0.10.24, imap 0.8.7.

@mscdex mscdex reopened this Dec 31, 2013
@mscdex
Owner
mscdex commented Dec 31, 2013

@Zugwalt What if you try applying this patch on top of master? I don't have an OSX 10.9 system available to me at the moment, so I'm not sure what else to try.

@Zugwalt
Zugwalt commented Dec 31, 2013

Unfortunately that patch didn't help--I'll see if I can dive into it on my OSX system after the new year.

@lifeisfoo

Solved capturing all imap error event, see #303 .

@mscdex mscdex closed this Jul 28, 2014
@the1mills

I get this error, probably because I run chmod everywhere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment