Skip to content

Commit b5779f8

Browse files
authored
Merge pull request #895 from matrix-org/travis/stop-client-on-logout
Stop syncing when the token is invalid
2 parents 3fba683 + f7d1984 commit b5779f8

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/sync.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,16 @@ SyncApi.prototype._wasLazyLoadingToggled = async function(lazyLoadMembers) {
445445
return false;
446446
};
447447

448+
SyncApi.prototype._shouldAbortSync = function(error) {
449+
if (err.errcode === "M_UNKNOWN_TOKEN") {
450+
// The logout already happened, we just need to stop.
451+
console.warn("Token no longer valid - assuming logout");
452+
self.stop();
453+
return true;
454+
}
455+
return false;
456+
};
457+
448458
/**
449459
* Main entry point
450460
*/
@@ -479,6 +489,7 @@ SyncApi.prototype.sync = function() {
479489
client.pushRules = result;
480490
} catch (err) {
481491
console.error("Getting push rules failed", err);
492+
if (self._shouldAbortSync(err)) return;
482493
// wait for saved sync to complete before doing anything else,
483494
// otherwise the sync state will end up being incorrect
484495
debuglog("Waiting for saved sync before retrying push rules...");
@@ -564,6 +575,7 @@ SyncApi.prototype.sync = function() {
564575
);
565576
} catch (err) {
566577
console.error("Getting filter failed", err);
578+
if (self._shouldAbortSync(err)) return;
567579
// wait for saved sync to complete before doing anything else,
568580
// otherwise the sync state will end up being incorrect
569581
debuglog("Waiting for saved sync before retrying filter...");
@@ -877,6 +889,10 @@ SyncApi.prototype._onSyncError = function(err, syncOptions) {
877889
console.error("/sync error %s", err);
878890
console.error(err);
879891

892+
if(this._shouldAbortSync(err)) {
893+
return;
894+
}
895+
880896
this._failedSyncCount++;
881897
console.log('Number of consecutive failed sync requests:', this._failedSyncCount);
882898

0 commit comments

Comments
 (0)