mscdex / node-imap

An IMAP client module for node.js.
MIT License
2.17k stars 380 forks source link

Timed out while authenticating with server #432

Closed celalo closed 5 years ago

celalo commented 10 years ago

Hello,

Very Rarely, I come across the error "Timed out while authenticating with server". Is there a way to handle this error gracefully? Connection event imap.once('error', function(err) {}) does not help. I am on node 0.8.27 with node-imap 0.8.14. Error details are below.

Error: Timed out while authenticating with server
 at Object._onTimeout (/app/node_modules/imap/lib/Connection.js:137:17)
 at Timer.list.ontimeout (timers.js:101:19)
mscdex commented 10 years ago

I'm not sure why listening for error doesn't work for you, that is how it is emitted (error on the imap connection instance).

celalo commented 10 years ago

It seems listening for error does work, vast majority of these errors are handled that way. Only very rarely I noticed they end up at process.on("uncaughtException", function(exception) {}); I will monitor it further and update here if I observe anything else.

mscdex commented 10 years ago

The only errors that should be thrown are errors that can be detected immediately (user error). If you are see non-user errors ending up in uncaughtException let me know what they are.

celalo commented 10 years ago

Here is what I found so far. please check inline comments.

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

imap.once('ready', function() {
  /*
  Very rarely, I come across `imap.state` is 'connected' at this point. According to docs` 'ready' event
  is emitted when a connection to the server has been made and authentication was successful.
  I suppose `imap.state` should have been 'authenticated' here.
  */
  if(imap.state != 'authenticated') { //So, I check here if the state is 'authenticated' to make sure, otherwise the execution fails at some point.
    imap.connect() //So, I try to make the connection again.
  /*
  Trying to connect again as above sometimes works.
  However if it does not work and if a "Timed out while authenticating with server" error occurs
  at this second connection attempt `imap.once('error', function(err) {})` does not catch the error.
  Instead an uncaught exception occurs.
  */
  }
})
mscdex commented 10 years ago

If you're re-using the same imap instance like that, you need to change imap.once('error', ...) to imap.on('error', ...) because otherwise. Same with imap.once('ready', ...).

However if you are seeing a ready event and it's not authenticated, that's a bug.

GeorgS commented 9 years ago

I am getting the same uncaught exception.

mscdex commented 9 years ago

@GeorgS Do you have an error event handler set up on the imap connection instance?

GeorgS commented 9 years ago

I sometimes get Uncaught Exception: Imap not authenticated. When I reconnect with

this.client.on('ready', function() {
        if (_this.client.state != 'authenticated') {
            _this.client.connect();
        }
});

I get ...imap/lib/Connection.js:1484 Uncaught TypeError: Cannot read property 'bodyEmitter' of undefined.

@mscdex Yes, I handle errors with

this.client.on('error', function(err) {
        console.error(err);
});

I get Error: Timed out while authenticating with server errors sometimes.

celalo commented 9 years ago

Changing imap.once('error', ...) to imap.on('error', ...) solves the uncaughtException, that was my bad. Now the error is handled gracefully.

Though, Timed out while authenticating with server error still persists. I see it much more often recently. increasing authTimeout (even up to 30s) does not help.

[connection] Connected to host
<= '* OK Gimap ready for requests from <redacted this part> n18mb3700662qhc'
=> '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! n18mb3700662qhc'
=> 'A1 AUTHENTICATE XOAUTH2 <redacted this part>'

Then authentication attempt stalls at this point. Starting all over again with a fresh connection does not help.

mscdex commented 9 years ago

@celalo Do you see more output after the AUTHENTICATE XOAUTH2 if you apply this patch?

celalo commented 9 years ago

@mscdex no more output, it is the same.

mscdex commented 9 years ago

If that's the case, it seems like there is some problem with Google's server or the connection between. After sending that command, it's waiting for a response of some kind, successful or not. I'm not sure what else this module can do about that.

celalo commented 9 years ago

That's what I suspected. The error occurs somewhat intermittently. In the same instance, few accounts end up with this authentication error although large majority works alright. Changing the instance, using a different IP address to connect does not help. Oddly, the issue does not persist. After a while, it starts working again for that particular accounts. Nevertheless, it is very likely the problem recurs again after an uncertain period of time.

onelesd commented 9 years ago

I am occasionally getting the same uncaughtException as @GeorgS when connecting to a yahoo.com account.

...imap/lib/Connection.js:1484 Uncaught TypeError: Cannot read property 'bodyEmitter' of undefined.

kaldimar commented 9 years ago

I've been using the library for some days, and until today we got this error, but we're getting it repeatedly:

/mnt/task/node_modules/imap/lib/Connection.js:1484
  if (req.bodyEmitter) {
         ^
TypeError: Cannot read property 'bodyEmitter' of undefined
    at Connection._resTagged (/mnt/task/node_modules/imap/lib/Connection.js:1484:10)
    at Parser.<anonymous> (/mnt/task/node_modules/imap/lib/Connection.js:177:10)
    at Parser.emit (events.js:95:17)
    at Parser._resTagged (/mnt/task/node_modules/imap/lib/Parser.js:175:10)
    at Parser._parse (/mnt/task/node_modules/imap/lib/Parser.js:139:16)
    at Parser._tryread (/mnt/task/node_modules/imap/lib/Parser.js:82:15)
    at CleartextStream.Parser._cbReadable (/mnt/task/node_modules/imap/lib/Parser.js:53:12)
    at CleartextStream.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:410:10)
    at _stream_readable.js:403:7

I feel like hijacking this issue thread, which was opened for timeout issues, however a web search brought me here because @GeorgS and @onelesd quoted the same error than the one I'm having. @mscdex if this should have its own thread feel free to move our messages or let us know to open a new one.

mscdex commented 9 years ago

@kaldimar Can you set debug: console.log or similar and post the output leading up to that error (including the initial FETCH sent to the server)?

kaldimar commented 9 years ago

I haven't had the issue again in the last ~24 hours however if it happens again I will post back with the debug info. Thanks.

Yubin commented 9 years ago

hi @mscdex

Any update on this, i ran into this several times, especially there is a error when connect. Here is the debug output:

[connection] Connected to host <= '* OK [CAPABILITY IMAP4 IMAP4rev1 IDLE XAPPLEPUSHSERVICE ID UIDPLUS AUTH=LOGIN NAMESPACE] QQMail IMAP4Server ready' => 'A0 CAPABILITY' <= '* CAPABILITY IMAP4 IMAP4rev1 IDLE XAPPLEPUSHSERVICE AUTH=LOGIN NAMESPACE CHILDREN ID UIDPLUS' <= 'A0 OK CAPABILITY Completed' => 'A1 LOGIN "369163493@qq.com" "xxxxx"' <= 'A1 NO [ALERT] Login failure. Incrrect passowd or your account is not enabled for IMAP. [��¼ʧ��, ������������IMAP����δ��ͨ]' <= 'A1 NO ��¼ʧ��[������������IMAP����δ��ͨ]' ./node_modules/imap/lib/Connection.js:1502 if (req.bodyEmitter) { ^ TypeError: Cannot read property 'bodyEmitter' of undefined at Connection._resTagged (./node_modules/imap/lib/Connection.js:1502:10) at Parser. (./node_modules/imap/lib/Connection.js:193:10) at Parser.emit (events.js:107:17) at Parser._resTagged (./node_modules/imap/lib/Parser.js:175:10) at Parser._parse (./node_modules/imap/lib/Parser.js:139:16) at Parser._tryread (./node_modules/imap/lib/Parser.js:82:15) at TLSSocket.Parser._cbReadable (./nodemodules/imap/lib/Parser.js:53:12) at TLSSocket.emit (events.js:104:17) at emitReadable (_stream_readable.js:424:10) at emitReadable (_stream_readable.js:418:7)

mscdex commented 9 years ago

@Yubin Should be fixed with 808565e97913.

Yubin commented 9 years ago

@mscdex awesome, it works for me now! Thanks

Maples7 commented 7 years ago

I got these 3 errors at the same time in most cases:

Timed out while authenticating with server          
This socket is closed        
write EPIPE    

I think maybe that's a bug of node, see EPIPE errors thrown when piping to a closed stream #947

chandnisoni commented 5 years ago

I am getting same error: Error: Timed out while authenticating with server at Object._onTimeout (/app/node_modules/imap/lib/Connection.js:137:17) at Timer.list.ontimeout (timers.js:101:19)

nareshgithub123 commented 5 years ago

I am getting same error: Error: Timed out while authenticating with server at Object._onTimeout (/app/node_modules/imap/lib/Connection.js:137:17) at Timer.list.ontimeout (timers.js:101:19)

Yes, me too getting the same error. Shall we know on what basis timedout error occurs.

mscdex commented 5 years ago

@nareshgithub123 I'm not sure what you mean. You can configure the connection and authentication timeouts in the connection config.

gsaravanakumar932 commented 2 years ago

imap version: imap@0.8.19

@mscdex, I am getting the below issue, It is working fine with LocalHost, but it is not working in server. My config as below, What should i do, to make it work ?

{
  errno: -104,
  code: 'ECONNRESET',
  syscall: 'read',
  source: 'socket',
  level: 'error'
}
{ source: 'timeout', level: 'error' }
{ source: 'timeout', level: 'error' }
{
  message: 'Error: Timed out while connecting to server\n' +
    '    at Timeout._onTimeout (/apps/smartexpense/mailread/node_modules/imap/lib/Connection.js:280:15)\n' +
    '    at listOnTimeout (internal/timers.js:551:17)\n' +
    '    at processTimers (internal/timers.js:494:7)',
  level: 'error'
}

{ message: 'Process will exit with code: 0', level: 'error' }
{ source: 'timeout', level: 'error' }

my config: export const imapConfig = { imap: { user: 'my@email', password: 'password', host: 'outlook.office365.com', port: 993, tls: true, authTimeout: 10000, connTimeout: 30000, keepalive: true, tlsOptions: { rejectUnauthorized: false } };