IllegalStateException: Timer was canceled
See original GitHub issueFirst of all thank you for the great library. We’ve been experiencing an issue lately where the library calls shutdownIfThereArentAnyActiveTasks
method. Apparently there is a timer instance which you try to reschedule but it has already been canceled so it throws the exception I mentioned in the title.
What did you expect?
To not use a canceled timer instance.
What happened instead?
Using the canceled timer instance to schedule for another time throws exception.
Version info
- Android Upload Service version: 3.4.2
- Android version and API version: All of our supported versions (17 to 26 - Android 4 to 8)
- HTTP stack (e.g. HurlStack or OkHttpStack): OkHttpStack
Steps to reproduce (if applicable):
I don’t know exactly but it’s worth it to mention that we’re using the library in three different places with three different upload id patterns which can be run simultaneously.
Library initialization code:
OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(300, TimeUnit.SECONDS)
.readTimeout(300, TimeUnit.SECONDS);
UploadService.NAMESPACE = BuildConfig.APPLICATION_ID;
UploadService.HTTP_STACK = new OkHttpStack(okHttpBuilder.build());
Request code:
The following is one of the three places I mentioned which has the most usage:
public class NewPostRequest extends UploadServiceBroadcastReceiver {
private void createNewPost(PostingMedia media) {
try {
String uploadId = String.valueOf(media.getId());
MultipartUploadRequest multipartUploadRequest = new MultipartUploadRequest(MainApplication.getAppContext(), uploadId, Endpoints.getInstance().CREATE_POST).setUtf8Charset();
multipartUploadRequest.addParameter("type", String.valueOf(media.getType()));
multipartUploadRequest.addParameter("requestId", uploadId);
if (media.getComposeBundle() != null) multipartUploadRequest.addFileToUpload(media.getComposeBundle().getFinalVideoUri().getPath(), "video");
multipartUploadRequest.addFileToUpload(media.getPicture().getPath(), "picture");
if (media.hasText()) multipartUploadRequest.addParameter("text", media.getText());
if (media.getSponsoringUnits() != 0) multipartUploadRequest.addParameter("sponsoringUnits", String.valueOf(media.getSponsoringUnits()));
multipartUploadRequest.addParameter("flags", String.valueOf(media.getFlags()));
mUploadId = multipartUploadRequest
.addHeader("Authorization", "Bearer " + Config.makePref(MainApplication.getAppContext()).getString(Config.SharedPrefFields.ACCESS_TOKEN, ""))
.addHeader("X-Client-Version", String.valueOf(BuildConfig.VERSION_CODE))
.setMethod("POST")
.setMaxRetries(0)
.setDelegate(this)
.setAutoDeleteFilesAfterSuccessfulUpload(media.getComposeBundle() == null || media.getComposeBundle().isDisposable())
.setNotificationConfig(Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? null :
new UploadNotificationConfig()
.setNotificationChannelId("upload")
.setTitleForAllStatuses(MainApplication.getAppContext().getString(R.string.sending_post))
.setIconForAllStatuses(R.drawable.ic_notif)
.setIconColorForAllStatuses(Color.RED)
.setRingToneEnabled(false))
.startUpload();
} catch (Exception exc) {
Logger.log(TAG, "createPostError: ", exc);
}
}
@Override
protected boolean shouldAcceptEventFrom(UploadInfo uploadInfo) {
try {
for (PostingMedia media : NewPostPresenter.sPostingMedias)
if (media.getId() == Integer.parseInt(uploadInfo.getUploadId())) return true;
} catch (Exception ignored) {}
return false;
}
}
Where have you added the request code?
Fragment -> Presenter -> Request class which extends UploadServiceBroadcastReceiver
to be the request delegate (Request code here)
LogCat output
Fatal Exception: java.lang.IllegalStateException: Timer was canceled
at java.util.Timer.scheduleImpl(Timer.java:558)
at java.util.Timer.schedule(Timer.java:456)
at net.gotev.uploadservice.UploadService.shutdownIfThereArentAnyActiveTasks(UploadService.java:305)
at net.gotev.uploadservice.UploadService.taskCompleted(UploadService.java:413)
at net.gotev.uploadservice.UploadTask.broadcastError(UploadTask.java:439)
at net.gotev.uploadservice.UploadTask.run(UploadTask.java:162)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Server side
Microsoft .NetCore
Additional info
The exception report from Crashlytics: http://crashes.to/s/0d8902486ff
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
That’s the exact point, it’s totally random. We’re using your perfect library as one of our core features (social network app), but I myself have never got this error. Our users on the other hand might do a lot of uploading stuff which makes it more probable to happen. So yes, it happens randomly.
@Sdghasemi if you need some guidance I’m happy to help