From 0b3e3d2f39587fb0a5e4792fe2f2abc1bd2bb86c Mon Sep 17 00:00:00 2001 From: Karim Hamidou Date: Tue, 3 Jan 2017 16:03:27 -0800 Subject: [PATCH] Make K2 recover from connectivity losses. Summary: I've found a pretty annoying bug --- N1 would stop syncing all accounts after the Internet connection dropped. It seems that deep inside node-imap or NodeJS itself, connections aren't timing out the right way. To work around this, this diff unilaterally restarts the sync every `nextSyncIn` milliseconds. Test Plan: Tested manually by cutting internet access and checking that K2 recovered. Reviewers: evan, juan Reviewed By: evan Differential Revision: https://phab.nylas.com/D3573 --- packages/isomorphic-core/src/imap-connection.js | 2 ++ packages/local-sync/src/local-sync-worker/sync-worker.js | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/isomorphic-core/src/imap-connection.js b/packages/isomorphic-core/src/imap-connection.js index ac64d3286..1b9cc456e 100644 --- a/packages/isomorphic-core/src/imap-connection.js +++ b/packages/isomorphic-core/src/imap-connection.js @@ -21,6 +21,7 @@ const Capabilities = { } const ONE_HOUR_SECS = 60 * 60; +const SOCKET_TIMEOUT_SECS = 60; class IMAPConnection extends EventEmitter { @@ -81,6 +82,7 @@ class IMAPConnection extends EventEmitter { user: this._settings.imap_username, password: this._settings.imap_password, tls: this._settings.ssl_required, + socketTimeout: SOCKET_TIMEOUT_SECS, } // This account uses XOAuth2, and we have the client_id + refresh token diff --git a/packages/local-sync/src/local-sync-worker/sync-worker.js b/packages/local-sync/src/local-sync-worker/sync-worker.js index 73ba62722..6904b7954 100644 --- a/packages/local-sync/src/local-sync-worker/sync-worker.js +++ b/packages/local-sync/src/local-sync-worker/sync-worker.js @@ -286,8 +286,6 @@ class SyncWorker { } this._syncStart = Date.now() - clearTimeout(this._syncTimer); - this._syncTimer = null; this._interrupted = false this._syncAttemptsWhileInProgress = 0 this._syncInProgress = true @@ -306,7 +304,9 @@ class SyncWorker { try { await this._account.update({syncError: null}); await this.ensureConnection(); + await this.runNewSyncbackTasks(); + await this._conn.runOperation(new FetchFolderList(this._account, this._logger)); // TODO prioritize syncing all of inbox first if there's a ton of folders (e.g. imap @@ -435,6 +435,7 @@ class SyncWorker { console.log(`🔃 🔜 in ${nextSyncIn}ms`) this._syncTimer = setTimeout(() => { + this.closeConnection(); this.syncNow({reason}); }, nextSyncIn); }