Clean up shutdown handling - stop processing incoming messages

This commit is contained in:
Scott Nonnenberg 2019-02-19 12:10:26 -08:00
parent ce9a52dde3
commit 77ae717b9b
2 changed files with 31 additions and 17 deletions

View file

@ -274,6 +274,19 @@
}, },
shutdown: async () => { shutdown: async () => {
// Stop background processing
window.Signal.AttachmentDownloads.stop();
if (idleDetector) {
idleDetector.stop();
}
// Stop processing incoming messages
if (messageReceiver) {
await messageReceiver.stopProcessing();
messageReceiver = null;
}
// Shut down the data interface cleanly
await window.Signal.Data.shutdown(); await window.Signal.Data.shutdown();
}, },
}; };
@ -428,16 +441,6 @@
} }
}); });
Whisper.events.on('shutdown', async () => {
if (idleDetector) {
idleDetector.stop();
}
if (messageReceiver) {
await messageReceiver.close();
}
Whisper.events.trigger('shutdown-complete');
});
Whisper.events.on('setupWithImport', () => { Whisper.events.on('setupWithImport', () => {
const { appView } = window.owsDesktopApp; const { appView } = window.owsDesktopApp;
if (appView) { if (appView) {
@ -549,13 +552,6 @@
window.getSyncRequest = () => window.getSyncRequest = () =>
new textsecure.SyncRequest(textsecure.messaging, messageReceiver); new textsecure.SyncRequest(textsecure.messaging, messageReceiver);
Whisper.events.on('start-shutdown', async () => {
if (messageReceiver) {
await messageReceiver.close();
}
Whisper.events.trigger('shutdown-complete');
});
let disconnectTimer = null; let disconnectTimer = null;
function onOffline() { function onOffline() {
window.log.info('offline'); window.log.info('offline');
@ -1327,6 +1323,11 @@
) { ) {
Whisper.events.trigger('unauthorized'); Whisper.events.trigger('unauthorized');
if (messageReceiver) {
await messageReceiver.stopProcessing();
messageReceiver = null;
}
window.log.warn( window.log.warn(
'Client is no longer authorized; deleting local configuration' 'Client is no longer authorized; deleting local configuration'
); );

View file

@ -192,6 +192,11 @@ MessageReceiver.prototype.extend({
// all cached envelopes are processed. // all cached envelopes are processed.
this.incoming = [this.pending]; this.incoming = [this.pending];
}, },
stopProcessing() {
window.log.info('MessageReceiver: stopProcessing requested');
this.stoppingProcessing = true;
return this.close();
},
shutdown() { shutdown() {
if (this.socket) { if (this.socket) {
this.socket.onclose = null; this.socket.onclose = null;
@ -614,6 +619,9 @@ MessageReceiver.prototype.extend({
// messages which were successfully decrypted, but application logic didn't finish // messages which were successfully decrypted, but application logic didn't finish
// processing. // processing.
handleDecryptedEnvelope(envelope, plaintext) { handleDecryptedEnvelope(envelope, plaintext) {
if (this.stoppingProcessing) {
return Promise.resolve();
}
// No decryption is required for delivery receipts, so the decrypted field of // No decryption is required for delivery receipts, so the decrypted field of
// the Unprocessed model will never be set // the Unprocessed model will never be set
@ -626,6 +634,10 @@ MessageReceiver.prototype.extend({
throw new Error('Received message with no content and no legacyMessage'); throw new Error('Received message with no content and no legacyMessage');
}, },
handleEnvelope(envelope) { handleEnvelope(envelope) {
if (this.stoppingProcessing) {
return Promise.resolve();
}
if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) { if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) {
return this.onDeliveryReceipt(envelope); return this.onDeliveryReceipt(envelope);
} }
@ -1416,6 +1428,7 @@ textsecure.MessageReceiver = function MessageReceiverWrapper(
this.downloadAttachment = messageReceiver.downloadAttachment.bind( this.downloadAttachment = messageReceiver.downloadAttachment.bind(
messageReceiver messageReceiver
); );
this.stopProcessing = messageReceiver.stopProcessing.bind(messageReceiver);
messageReceiver.connect(); messageReceiver.connect();
}; };