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.
This commit is contained in:
B. Petersen
2020-04-24 01:56:07 +02:00
parent a81bfcf40d
commit 0f4bd6347f
4 changed files with 40 additions and 4 deletions
+6
View File
@@ -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));
+1
View File
@@ -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);
@@ -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);
}
@@ -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