From efd835aceaa58ceefa29057b21644653bff70ae3 Mon Sep 17 00:00:00 2001 From: Tony Mantler Date: Mon, 14 Oct 2013 12:25:04 -0700 Subject: [PATCH] Fix threading issues in EmailDownloadService b/7257927 Change-Id: I390ced0eee18ff324032481ae55c31f5d4b26ffd --- .../service/AttachmentDownloadService.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/com/android/email/service/AttachmentDownloadService.java b/src/com/android/email/service/AttachmentDownloadService.java index 1e65f3027..357e9e3e8 100644 --- a/src/com/android/email/service/AttachmentDownloadService.java +++ b/src/com/android/email/service/AttachmentDownloadService.java @@ -921,7 +921,15 @@ public class AttachmentDownloadService extends Service implements Runnable { // Loop until stopped, with a 30 minute wait loop while (!mStop) { // Here's where we run our attachment loading logic... - mConnectivityManager.waitForConnectivity(); + // Make a local copy of the variable so we don't null-crash on service shutdown + final EmailConnectivityManager ecm = mConnectivityManager; + if (ecm != null) { + ecm.waitForConnectivity(); + } + if (mStop) { + // We might be bailing out here due to the service shutting down + break; + } mDownloadSet.processQueue(); if (mDownloadSet.isEmpty()) { LogUtils.d(TAG, "*** All done; shutting down service"); @@ -938,8 +946,10 @@ public class AttachmentDownloadService extends Service implements Runnable { } // Unregister now that we're done - if (mConnectivityManager != null) { - mConnectivityManager.unregister(); + // Make a local copy of the variable so we don't null-crash on service shutdown + final EmailConnectivityManager ecm = mConnectivityManager; + if (ecm != null) { + ecm.unregister(); } } @@ -975,6 +985,7 @@ public class AttachmentDownloadService extends Service implements Runnable { } if (mConnectivityManager != null) { mConnectivityManager.unregister(); + mConnectivityManager.stopWait(); mConnectivityManager = null; } }