Skip to content

Commit ad9d888

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 63a7fe1 commit ad9d888

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
@@ -73,7 +73,22 @@ protected Void doInBackground(Void... params) {
7373
* Even if we are cancelled for any reason, it should still service all items in the queue if it can.
7474
*
7575
*/
76-
while (!(cancelled = isCancelled()) && (work=mParams.dequeueWork()) != null) {
76+
while (!(cancelled = isCancelled())) {
77+
try {
78+
// This is pertaining to this issue:
79+
// https://issuetracker.google.com/issues/63622293
80+
// The service was probabably destroyed but we didn't cancel the
81+
// processor. It causes an exception in dequeueWork.
82+
// We are also now calling cancel in onDestroy
83+
if ((work = mParams.dequeueWork()) == null) {
84+
return null;
85+
}
86+
}
87+
catch (Exception e) {
88+
logger.error("Exception in JobWorkService:doInBackground mParams.dequeueWork() ", e);
89+
return null;
90+
}
91+
7792
final String componentClass = work.getIntent().getComponent().getClassName();
7893
Class<?> clazz = null;
7994
logger.info("Processing work: " + work + ", component: " + componentClass);
@@ -175,6 +190,13 @@ public void onCreate() {
175190

176191
@Override
177192
public void onDestroy() {
193+
// This is pertaining to this issue:
194+
// https://issuetracker.google.com/issues/63622293
195+
// The service was probabably destroyed but we didn't cancel the
196+
// processor.
197+
if (mCurProcessor != null) {
198+
mCurProcessor.cancel(true);
199+
}
178200
}
179201

180202
@Override

0 commit comments

Comments
 (0)