From 0f4bd6347ff7ea0f69c300ff8aea3efba23b2874 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 24 Apr 2020 01:56:07 +0200 Subject: [PATCH 1/5] stop threads and call close() on switching account the imap-/mvbox-/sentbox-/smtp-threads needs to be terminated before dc_close() can be called successfully. if this is not done, they are hanging around, and, even worse, if an account is opened _again_, there are now two threads working on the database which lead to duplicated messages. note, that this issue is only related to duplicated messages if ever an account was selected, which, however, might be the same as the current. --- jni/dc_wrapper.c | 6 ++++ src/com/b44t/messenger/DcContext.java | 1 + .../securesms/connect/AccountManager.java | 2 ++ .../connect/ApplicationDcContext.java | 35 ++++++++++++++++--- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/jni/dc_wrapper.c b/jni/dc_wrapper.c index b023e08a2..ef74c388b 100644 --- a/jni/dc_wrapper.c +++ b/jni/dc_wrapper.c @@ -387,6 +387,12 @@ JNIEXPORT void Java_com_b44t_messenger_DcContext_performSmtpIdle(JNIEnv *env, jo } +JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptSmtpIdle(JNIEnv *env, jobject obj) +{ + dc_interrupt_smtp_idle(get_dc_context(env, obj)); +} + + JNIEXPORT void Java_com_b44t_messenger_DcContext_maybeNetwork(JNIEnv *env, jobject obj) { dc_maybe_network(get_dc_context(env, obj)); diff --git a/src/com/b44t/messenger/DcContext.java b/src/com/b44t/messenger/DcContext.java index 3bb29b025..4848b9ae5 100644 --- a/src/com/b44t/messenger/DcContext.java +++ b/src/com/b44t/messenger/DcContext.java @@ -106,6 +106,7 @@ public class DcContext { public native void performSmtpJobs (); public native void performSmtpIdle (); + public native void interruptSmtpIdle (); public native void maybeNetwork (); public native void setConfig (String key, String value); diff --git a/src/org/thoughtcrime/securesms/connect/AccountManager.java b/src/org/thoughtcrime/securesms/connect/AccountManager.java index 32aa176ee..203646f02 100644 --- a/src/org/thoughtcrime/securesms/connect/AccountManager.java +++ b/src/org/thoughtcrime/securesms/connect/AccountManager.java @@ -93,6 +93,8 @@ public class AccountManager { // create an empty DcContext object - this will be set up then, starting with // getSelectedAccount() ApplicationContext appContext = (ApplicationContext)context.getApplicationContext(); + appContext.dcContext.stopThreads(); + appContext.dcContext.close(); appContext.dcContext = new ApplicationDcContext(context); } diff --git a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java index 7c69fa9fe..86cb71bb6 100644 --- a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java +++ b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java @@ -296,6 +296,8 @@ public class ApplicationDcContext extends DcContext { public final static int INTERRUPT_IDLE = 0x01; // interrupt idle if the thread is already running + public boolean run = true; + public void startThreads(int flags) { synchronized (threadsCritical) { @@ -303,7 +305,7 @@ public class ApplicationDcContext extends DcContext { imapThread = new Thread(() -> { Log.i(TAG, "###################### IMAP-Thread started. ######################"); - while (true) { + while (run) { imapWakeLock.acquire(); performImapJobs(); performImapFetch(); @@ -313,6 +315,7 @@ public class ApplicationDcContext extends DcContext { } performImapIdle(); } + Log.i(TAG, "!!!!!!!!!!!! IMAP-Thread stopped"); }, "imapThread"); imapThread.setPriority(Thread.NORM_PRIORITY); imapThread.start(); @@ -327,7 +330,7 @@ public class ApplicationDcContext extends DcContext { mvboxThread = new Thread(() -> { Log.i(TAG, "###################### MVBOX-Thread started. ######################"); - while (true) { + while (run) { mvboxWakeLock.acquire(); performMvboxJobs(); performMvboxFetch(); @@ -337,6 +340,7 @@ public class ApplicationDcContext extends DcContext { } performMvboxIdle(); } + Log.i(TAG, "!!!!!!!!!!!! MVBOX-Thread stopped"); }, "mvboxThread"); mvboxThread.setPriority(Thread.NORM_PRIORITY); mvboxThread.start(); @@ -351,13 +355,14 @@ public class ApplicationDcContext extends DcContext { sentboxThread = new Thread(() -> { Log.i(TAG, "###################### SENTBOX-Thread started. ######################"); - while (true) { + while (run) { sentboxWakeLock.acquire(); performSentboxJobs(); performSentboxFetch(); sentboxWakeLock.release(); performSentboxIdle(); } + Log.i(TAG, "!!!!!!!!!!!! SENTBOX-Thread stopped"); }, "sentboxThread"); sentboxThread.setPriority(Thread.NORM_PRIORITY-1); sentboxThread.start(); @@ -370,7 +375,7 @@ public class ApplicationDcContext extends DcContext { if (smtpThread == null || !smtpThread.isAlive()) { smtpThread = new Thread(() -> { Log.i(TAG, "###################### SMTP-Thread started. ######################"); - while (true) { + while (run) { smtpWakeLock.acquire(); performSmtpJobs(); smtpWakeLock.release(); @@ -379,6 +384,7 @@ public class ApplicationDcContext extends DcContext { } performSmtpIdle(); } + Log.i(TAG, "!!!!!!!!!!!! SMTP-Thread stopped"); }, "smtpThread"); smtpThread.setPriority(Thread.MAX_PRIORITY); smtpThread.start(); @@ -397,6 +403,27 @@ public class ApplicationDcContext extends DcContext { } } + public void stopThreads() { + Log.i(TAG, "!!!!!!!!!!!! Stopping threads ..."); + run = false; + synchronized (threadsCritical) { + if (imapThread!=null) { interruptImapIdle(); } + if (mvboxThread!=null) { interruptMvboxIdle(); } + if (sentboxThread!=null) { interruptSentboxIdle(); } + if (smtpThread!=null) { interruptSmtpIdle(); } + while (true) { + if ( (imapThread==null || !imapThread.isAlive()) + && (mvboxThread==null || !mvboxThread.isAlive()) + && (sentboxThread==null || !sentboxThread.isAlive()) + && (smtpThread==null || !smtpThread.isAlive())) { + break; + } + Util.sleep(100); + } + } + Log.i(TAG, "!!!!!!!!!!!! threads stopped"); + } + /*********************************************************************************************** * Tools From 36c4f7ae2b9306931c9a3bceafc0cff870bcbaca Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 24 Apr 2020 02:03:47 +0200 Subject: [PATCH 2/5] tweak deletion of not-created-accounts --- src/org/thoughtcrime/securesms/connect/AccountManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/thoughtcrime/securesms/connect/AccountManager.java b/src/org/thoughtcrime/securesms/connect/AccountManager.java index 203646f02..227dc64c6 100644 --- a/src/org/thoughtcrime/securesms/connect/AccountManager.java +++ b/src/org/thoughtcrime/securesms/connect/AccountManager.java @@ -183,6 +183,8 @@ public class AccountManager { sharedPreferences.edit().putString("prev_account_db_name", "").apply(); sharedPreferences.edit().putString("curr_account_db_name", prevDbName).apply(); + resetDcContext(context); + // delete the previous account, however, as a resilience check, make sure, // we do not delete already configured accounts (just in case sth. changes the flow of activities) DcContext testContext = new DcContext(null); @@ -192,8 +194,6 @@ public class AccountManager { deleteAccount(context, inCreationDbName); } } - - resetDcContext(context); } From 6c8d6a804d40a5c57a819dee36d462cab357ed93 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 24 Apr 2020 12:34:23 +0200 Subject: [PATCH 3/5] continue interrupting running threads --- .../connect/ApplicationDcContext.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java index 86cb71bb6..3a01cc4f6 100644 --- a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java +++ b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java @@ -404,21 +404,26 @@ public class ApplicationDcContext extends DcContext { } public void stopThreads() { - Log.i(TAG, "!!!!!!!!!!!! Stopping threads ..."); run = false; synchronized (threadsCritical) { - if (imapThread!=null) { interruptImapIdle(); } - if (mvboxThread!=null) { interruptMvboxIdle(); } - if (sentboxThread!=null) { interruptSentboxIdle(); } - if (smtpThread!=null) { interruptSmtpIdle(); } while (true) { - if ( (imapThread==null || !imapThread.isAlive()) - && (mvboxThread==null || !mvboxThread.isAlive()) + + // in theory, interrupting once outside the loop should be sufficient, + // but there are some corner cases, see https://github.com/deltachat/deltachat-core-rust/issues/925 + Log.i(TAG, "!!!!!!!!!!!! Stopping threads ..."); + if (imapThread!=null && imapThread.isAlive()) { interruptImapIdle(); } + if (mvboxThread!=null && mvboxThread.isAlive()) { interruptMvboxIdle(); } + if (sentboxThread!=null && sentboxThread.isAlive()) { interruptSentboxIdle(); } + if (smtpThread!=null && smtpThread.isAlive()) { interruptSmtpIdle(); } + + Util.sleep(300); + + if ( (imapThread==null || !imapThread.isAlive()) + && (mvboxThread==null || !mvboxThread.isAlive()) && (sentboxThread==null || !sentboxThread.isAlive()) - && (smtpThread==null || !smtpThread.isAlive())) { + && (smtpThread==null || !smtpThread.isAlive())) { break; } - Util.sleep(100); } } Log.i(TAG, "!!!!!!!!!!!! threads stopped"); From 2eae56a415fb061e3746999a9acc8e3b3630e08e Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 24 Apr 2020 13:49:47 +0200 Subject: [PATCH 4/5] do account switching in async task --- .../securesms/connect/AccountManager.java | 53 +++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/src/org/thoughtcrime/securesms/connect/AccountManager.java b/src/org/thoughtcrime/securesms/connect/AccountManager.java index 227dc64c6..a4182fae3 100644 --- a/src/org/thoughtcrime/securesms/connect/AccountManager.java +++ b/src/org/thoughtcrime/securesms/connect/AccountManager.java @@ -16,8 +16,10 @@ import org.thoughtcrime.securesms.ConversationListActivity; import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.WelcomeActivity; import org.thoughtcrime.securesms.util.Prefs; +import org.thoughtcrime.securesms.util.task.ProgressDialogAsyncTask; import java.io.File; +import java.lang.ref.WeakReference; import java.util.ArrayList; public class AccountManager { @@ -212,6 +214,44 @@ public class AccountManager { } + // helper class for switching accounts gracefully + + private static class SwitchAccountAsyncTask extends ProgressDialogAsyncTask { + private final WeakReference activityWeakReference; + private final @Nullable Account destAccount; // null creates a new account + + public SwitchAccountAsyncTask(Activity activity, int title, @Nullable Account destAccount) { + super(activity, title, R.string.one_moment); + this.activityWeakReference = new WeakReference<>(activity); + this.destAccount = destAccount; + } + @Override + protected Void doInBackground(Integer... recipientIds) { + Activity activity = activityWeakReference.get(); + if (activity!=null) { + if (destAccount==null) { + AccountManager.getInstance().beginAccountCreation(activity); + } else { + AccountManager.getInstance().switchAccount(activity, destAccount); + } + } + return null; + } + + @Override + protected void onPostExecute(Void result) { + Activity activity = activityWeakReference.get(); + if (activity!=null) { + activity.finishAffinity(); + if (destAccount==null) { + activity.startActivity(new Intent(activity, WelcomeActivity.class)); + } else { + activity.startActivity(new Intent(activity.getApplicationContext(), ConversationListActivity.class)); + } + } + } + } + // ui public void handleSwitchAccount(Activity activity) { @@ -236,18 +276,13 @@ public class AccountManager { .setTitle(R.string.switch_account) .setNegativeButton(R.string.cancel, null) .setSingleChoiceItems(menu.toArray(new String[menu.size()]), presel, (dialog, which) -> { + dialog.dismiss(); if (which==addAccount) { - beginAccountCreation(activity); - activity.finishAffinity(); - activity.startActivity(new Intent(activity, WelcomeActivity.class)); + new SwitchAccountAsyncTask(activity, R.string.add_account, null).execute(0); } else { // switch account Account account = accounts.get(which); - if (account.isCurrent()) { - dialog.dismiss(); - } else { - switchAccount(activity, account); - activity.finishAffinity(); - activity.startActivity(new Intent(activity.getApplicationContext(), ConversationListActivity.class)); + if (!account.isCurrent()) { + new SwitchAccountAsyncTask(activity, R.string.switch_account, account).execute(0); } } }); From 61192e90e40c7263730a583c166037ada9f94b9f Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 24 Apr 2020 14:49:10 +0200 Subject: [PATCH 5/5] use SwitchAccountAsyncTask also for aborting account-creation --- .../securesms/WelcomeActivity.java | 2 - .../securesms/connect/AccountManager.java | 46 +++++++++++-------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/org/thoughtcrime/securesms/WelcomeActivity.java b/src/org/thoughtcrime/securesms/WelcomeActivity.java index 27b52651c..0e1268acc 100644 --- a/src/org/thoughtcrime/securesms/WelcomeActivity.java +++ b/src/org/thoughtcrime/securesms/WelcomeActivity.java @@ -251,8 +251,6 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen AccountManager accountManager = AccountManager.getInstance(); if (accountManager.canRollbackAccountCreation(this)) { accountManager.rollbackAccountCreation(this); - finish(); - startActivity(new Intent(getApplicationContext(), ConversationListActivity.class)); } else { super.onBackPressed(); } diff --git a/src/org/thoughtcrime/securesms/connect/AccountManager.java b/src/org/thoughtcrime/securesms/connect/AccountManager.java index a4182fae3..31504bc98 100644 --- a/src/org/thoughtcrime/securesms/connect/AccountManager.java +++ b/src/org/thoughtcrime/securesms/connect/AccountManager.java @@ -177,24 +177,14 @@ public class AccountManager { return !prevDbName.isEmpty(); } - public void rollbackAccountCreation(Context context) { - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + public void rollbackAccountCreation(Activity activity) { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity); String prevDbName = sharedPreferences.getString("prev_account_db_name", ""); String inCreationDbName = sharedPreferences.getString("curr_account_db_name", ""); - sharedPreferences.edit().putString("prev_account_db_name", "").apply(); - sharedPreferences.edit().putString("curr_account_db_name", prevDbName).apply(); - - resetDcContext(context); - - // delete the previous account, however, as a resilience check, make sure, - // we do not delete already configured accounts (just in case sth. changes the flow of activities) - DcContext testContext = new DcContext(null); - if (testContext.open(new File(context.getFilesDir(), inCreationDbName).getAbsolutePath()) != 0) { - if (testContext.isConfigured() == 0) { - testContext.close(); - deleteAccount(context, inCreationDbName); - } + Account prevAccount = maybeGetAccount(new File(activity.getFilesDir(), prevDbName)); + if (prevAccount!=null) { + new SwitchAccountAsyncTask(activity, R.string.switch_account, prevAccount, inCreationDbName).execute(); } } @@ -216,23 +206,39 @@ public class AccountManager { // helper class for switching accounts gracefully - private static class SwitchAccountAsyncTask extends ProgressDialogAsyncTask { + private static class SwitchAccountAsyncTask extends ProgressDialogAsyncTask { private final WeakReference activityWeakReference; private final @Nullable Account destAccount; // null creates a new account + private final @Nullable String deleteDbName; - public SwitchAccountAsyncTask(Activity activity, int title, @Nullable Account destAccount) { + public SwitchAccountAsyncTask(Activity activity, int title, @Nullable Account destAccount, @Nullable String deleteDbName) { super(activity, title, R.string.one_moment); this.activityWeakReference = new WeakReference<>(activity); this.destAccount = destAccount; + this.deleteDbName = deleteDbName; } @Override - protected Void doInBackground(Integer... recipientIds) { + protected Void doInBackground(Void... voids) { Activity activity = activityWeakReference.get(); if (activity!=null) { if (destAccount==null) { AccountManager.getInstance().beginAccountCreation(activity); } else { AccountManager.getInstance().switchAccount(activity, destAccount); + + if (deleteDbName!=null) { + // used to delete the previous account, however, as a resilience check, make sure, + // we do not delete already configured accounts (just in case sth. changes the flow of activities) + DcContext testContext = new DcContext(null); + if (testContext.open(new File(activity.getFilesDir(), deleteDbName).getAbsolutePath()) != 0) { + if (testContext.isConfigured() == 0) { + testContext.close(); + AccountManager.getInstance().deleteAccount(activity, deleteDbName); + PreferenceManager.getDefaultSharedPreferences(activity) + .edit().putString("prev_account_db_name", "").apply(); + } + } + } } } return null; @@ -278,11 +284,11 @@ public class AccountManager { .setSingleChoiceItems(menu.toArray(new String[menu.size()]), presel, (dialog, which) -> { dialog.dismiss(); if (which==addAccount) { - new SwitchAccountAsyncTask(activity, R.string.add_account, null).execute(0); + new SwitchAccountAsyncTask(activity, R.string.add_account, null, null).execute(); } else { // switch account Account account = accounts.get(which); if (!account.isCurrent()) { - new SwitchAccountAsyncTask(activity, R.string.switch_account, account).execute(0); + new SwitchAccountAsyncTask(activity, R.string.switch_account, account, null).execute(); } } });