From af2ce56c8dd6dcc40e8619fb9304d8bb410a4372 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Tue, 25 Jul 2017 16:23:13 -0700 Subject: [PATCH] Reset MessageReciever queue whenever possible (like we do with the conversation queue already) FREEBIE --- js/libtextsecure.js | 36 ++++++++++++++++++++----------- libtextsecure/message_receiver.js | 36 ++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index ef0650c9d..dfe6d3eb8 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -38349,6 +38349,24 @@ MessageReceiver.prototype.extend({ return this.dispatchAndWait(ev); }.bind(this))); }, + addToQueue: function(task) { + var count = this.count += 1; + var current = this.pending = this.pending.then(task, task); + + var cleanup = function() { + this.updateProgress(count); + // We want to clear out the promise chain whenever possible because it could + // lead to large memory usage over time: + // https://github.com/nodejs/node/issues/6673#issuecomment-244331609 + if (this.pending === current) { + this.pending = Promise.resolve(); + } + }.bind(this); + + current.then(cleanup, cleanup); + + return current; + }, onEmpty: function() { var incoming = this.incoming; this.incoming = []; @@ -38362,7 +38380,7 @@ MessageReceiver.prototype.extend({ // resetting count to zero so everything queued after this starts over again this.count = 0; - this.pending = this.pending.then(dispatchEmpty, dispatchEmpty); + this.addToQueue(dispatchEmpty); }.bind(this); Promise.all(incoming).then(scheduleDispatch, scheduleDispatch); @@ -38460,34 +38478,26 @@ MessageReceiver.prototype.extend({ return textsecure.storage.unprocessed.remove(id); }, queueDecryptedEnvelope: function(envelope, plaintext) { - var count = this.count += 1; var id = this.getEnvelopeId(envelope); console.log('queueing decrypted envelope', id); var task = this.handleDecryptedEnvelope.bind(this, envelope, plaintext); var taskWithTimeout = textsecure.createTaskWithTimeout(task, 'queueEncryptedEnvelope ' + id); + var promise = this.addToQueue(taskWithTimeout); - this.pending = this.pending.then(taskWithTimeout, taskWithTimeout); - - return this.pending.then(function() { - this.updateProgress(count); - }.bind(this), function(error) { + return promise.catch(function(error) { console.log('queueDecryptedEnvelope error handling envelope', id, ':', error && error.stack ? error.stack : error); }); }, queueEnvelope: function(envelope) { - var count = this.count += 1; var id = this.getEnvelopeId(envelope); console.log('queueing envelope', id); var task = this.handleEnvelope.bind(this, envelope); var taskWithTimeout = textsecure.createTaskWithTimeout(task, 'queueEnvelope ' + id); + var promise = this.addToQueue(taskWithTimeout); - this.pending = this.pending.then(taskWithTimeout, taskWithTimeout); - - return this.pending.then(function() { - this.updateProgress(count); - }.bind(this), function(error) { + return promise.catch(function(error) { console.log('queueEnvelope error handling envelope', id, ':', error && error.stack ? error.stack : error); }); }, diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index e14d1fb00..0504165be 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -107,6 +107,24 @@ MessageReceiver.prototype.extend({ return this.dispatchAndWait(ev); }.bind(this))); }, + addToQueue: function(task) { + var count = this.count += 1; + var current = this.pending = this.pending.then(task, task); + + var cleanup = function() { + this.updateProgress(count); + // We want to clear out the promise chain whenever possible because it could + // lead to large memory usage over time: + // https://github.com/nodejs/node/issues/6673#issuecomment-244331609 + if (this.pending === current) { + this.pending = Promise.resolve(); + } + }.bind(this); + + current.then(cleanup, cleanup); + + return current; + }, onEmpty: function() { var incoming = this.incoming; this.incoming = []; @@ -120,7 +138,7 @@ MessageReceiver.prototype.extend({ // resetting count to zero so everything queued after this starts over again this.count = 0; - this.pending = this.pending.then(dispatchEmpty, dispatchEmpty); + this.addToQueue(dispatchEmpty); }.bind(this); Promise.all(incoming).then(scheduleDispatch, scheduleDispatch); @@ -218,34 +236,26 @@ MessageReceiver.prototype.extend({ return textsecure.storage.unprocessed.remove(id); }, queueDecryptedEnvelope: function(envelope, plaintext) { - var count = this.count += 1; var id = this.getEnvelopeId(envelope); console.log('queueing decrypted envelope', id); var task = this.handleDecryptedEnvelope.bind(this, envelope, plaintext); var taskWithTimeout = textsecure.createTaskWithTimeout(task, 'queueEncryptedEnvelope ' + id); + var promise = this.addToQueue(taskWithTimeout); - this.pending = this.pending.then(taskWithTimeout, taskWithTimeout); - - return this.pending.then(function() { - this.updateProgress(count); - }.bind(this), function(error) { + return promise.catch(function(error) { console.log('queueDecryptedEnvelope error handling envelope', id, ':', error && error.stack ? error.stack : error); }); }, queueEnvelope: function(envelope) { - var count = this.count += 1; var id = this.getEnvelopeId(envelope); console.log('queueing envelope', id); var task = this.handleEnvelope.bind(this, envelope); var taskWithTimeout = textsecure.createTaskWithTimeout(task, 'queueEnvelope ' + id); + var promise = this.addToQueue(taskWithTimeout); - this.pending = this.pending.then(taskWithTimeout, taskWithTimeout); - - return this.pending.then(function() { - this.updateProgress(count); - }.bind(this), function(error) { + return promise.catch(function(error) { console.log('queueEnvelope error handling envelope', id, ':', error && error.stack ? error.stack : error); }); },