0

I have implemented node imap (https://github.com/mscdex/node-imap). Listening for emails in an opened inbox. Works fine and without any problem. However, while working with a very unstable internet connection, it seems that the connection is lost and some error appears:

Error: This socket is closed
    at Socket._writeGeneric (net.js:733:18)
    at Socket._write (net.js:787:8)
    at doWrite (_stream_writable.js:396:12)
    at clearBuffer (_stream_writable.js:523:7)
    at Socket.Writable.uncork (_stream_writable.js:312:7)
    at JSStreamWrap.doWrite (internal/wrap_js_stream.js:119:17)
    at JSStreamWrap.handle.onwrite (internal/wrap_js_stream.js:37:42)
    at shutdownSocket (net.js:286:23)
    at defaultTriggerAsyncIdScope (internal/async_hooks.js:281:19)
    at TLSSocket.Socket._final (net.js:312:13)
    at callFinal (_stream_writable.js:584:10)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)

I have handlers for all the relevants events as far as I know

  imap.once('error', function(err) {

  });

  imap.once('close', function(err) {

  });

  imap.once('end', function(err) {

  });

However the error is not falling into any of those. Furthermore, I have everything wrapped inside a try/catch, still the error crashes the node process and I have to restart it.

Any idea how to go around this? Ideally catch the error and give it a chance to end/restart the imap connection.

yBrodsky
  • 4,981
  • 3
  • 20
  • 31
  • Some NAT middleboxes will discard connections that have been idle for a minute, or for ten minutes, an tell neither end about it. If you're using such a middlebox, the connection will appear to suffer 100% packet loss after a while, and there's no other sign of problem. The only way I can suggest to deal with this is that if you might want to use the connection and haven't received anything in a minute, send a NOOP command first and then immediately the command you want to send. If the NOOP hasn't completed in a second or two, you may assume that this is the problem. – arnt Mar 14 '20 at 20:57
  • @arnt but I am not doing anything. "NOOP command first and then immediately the command you want to send". I just open an inbox and define a listener for new emails. And that's it. I only do something if the listener fires. Anyway, even if as you say this is the problem, how can I catch this error to prevent it from crashing the whole app? – yBrodsky Mar 16 '20 at 13:35
  • If this is the problem, then it is a problem precisely because you're not doing anything. Such NAT boxes interrupt connection after a bit of *idleness*, not after some number of connections or after some specific connection. – arnt Mar 16 '20 at 15:16
  • Oh, okay I see where you are going. Anyway, if this was the case wouldnt this happen all the time. This only started being an issue while working with a crappy internet connection. Otherwise there's no error. – yBrodsky Mar 16 '20 at 18:06
  • It sounds as if you think that the crappy connection involves exactly the same kinds of middleboxes as the connections where you have no problem. – arnt Mar 16 '20 at 18:40

0 Answers0