Skip to content

Commit 14c84f9

Browse files
wrap dequeue with try catch in case of exception (#193)
* wrap dequeue with try catch in case of exception * fix for issue with Android O JobWorkService failing on dequeue * add a null pointer check
1 parent d566e8f commit 14c84f9

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

shared/src/main/java/com/optimizely/ab/android/shared/JobWorkService.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,22 @@ protected Void doInBackground(Void... params) {
7070
* Even if we are cancelled for any reason, it should still service all items in the queue if it can.
7171
*
7272
*/
73-
while (!(cancelled = isCancelled()) && (work=mParams.dequeueWork()) != null) {
73+
while (!(cancelled = isCancelled())) {
74+
try {
75+
// This is pertaining to this issue:
76+
// https://issuetracker.google.com/issues/63622293
77+
// The service was probabably destroyed but we didn't cancel the
78+
// processor. It causes an exception in dequeueWork.
79+
// We are also now calling cancel in onDestroy
80+
if ((work = mParams.dequeueWork()) == null) {
81+
return null;
82+
}
83+
}
84+
catch (Exception e) {
85+
logger.error("Exception in JobWorkService:doInBackground mParams.dequeueWork() ", e);
86+
return null;
87+
}
88+
7489
final String componentClass = work.getIntent().getComponent().getClassName();
7590
Class<?> clazz = null;
7691
logger.info("Processing work: " + work + ", component: " + componentClass);
@@ -129,6 +144,13 @@ public void onCreate() {
129144

130145
@Override
131146
public void onDestroy() {
147+
// This is pertaining to this issue:
148+
// https://issuetracker.google.com/issues/63622293
149+
// The service was probabably destroyed but we didn't cancel the
150+
// processor.
151+
if (mCurProcessor != null) {
152+
mCurProcessor.cancel(true);
153+
}
132154
}
133155

134156
@Override

0 commit comments

Comments
 (0)