Skip to content

Commit

Permalink
Clean up shutdown handling - stop processing incoming messages
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Feb 21, 2019
1 parent ce9a52d commit 77ae717
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
35 changes: 18 additions & 17 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,19 @@
},

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();
},
};
Expand Down Expand Up @@ -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', () => {
const { appView } = window.owsDesktopApp;
if (appView) {
Expand Down Expand Up @@ -549,13 +552,6 @@
window.getSyncRequest = () =>
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;
function onOffline() {
window.log.info('offline');
Expand Down Expand Up @@ -1327,6 +1323,11 @@
) {
Whisper.events.trigger('unauthorized');

if (messageReceiver) {
await messageReceiver.stopProcessing();
messageReceiver = null;
}

window.log.warn(
'Client is no longer authorized; deleting local configuration'
);
Expand Down
13 changes: 13 additions & 0 deletions libtextsecure/message_receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ MessageReceiver.prototype.extend({
// all cached envelopes are processed.
this.incoming = [this.pending];
},
stopProcessing() {
window.log.info('MessageReceiver: stopProcessing requested');
this.stoppingProcessing = true;
return this.close();
},
shutdown() {
if (this.socket) {
this.socket.onclose = null;
Expand Down Expand Up @@ -614,6 +619,9 @@ MessageReceiver.prototype.extend({
// messages which were successfully decrypted, but application logic didn't finish
// processing.
handleDecryptedEnvelope(envelope, plaintext) {
if (this.stoppingProcessing) {
return Promise.resolve();
}
// No decryption is required for delivery receipts, so the decrypted field of
// the Unprocessed model will never be set

Expand All @@ -626,6 +634,10 @@ MessageReceiver.prototype.extend({
throw new Error('Received message with no content and no legacyMessage');
},
handleEnvelope(envelope) {
if (this.stoppingProcessing) {
return Promise.resolve();
}

if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) {
return this.onDeliveryReceipt(envelope);
}
Expand Down Expand Up @@ -1416,6 +1428,7 @@ textsecure.MessageReceiver = function MessageReceiverWrapper(
this.downloadAttachment = messageReceiver.downloadAttachment.bind(
messageReceiver
);
this.stopProcessing = messageReceiver.stopProcessing.bind(messageReceiver);

messageReceiver.connect();
};
Expand Down

0 comments on commit 77ae717

Please sign in to comment.