question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IllegalStateException: Timer was canceled

See original GitHub issue

First 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Sdghasemicommented, Jan 10, 2019

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.

0reactions
gotevcommented, Jan 19, 2019

@Sdghasemi if you need some guidance I’m happy to help

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android relaunching Timers after they are canceled
It means that if you once cancel() Timer you can't to run it again. You need to create new instance of Timer, for...
Read more >
Strange IllegalStateException: "Timer already cancelled"
I'm getting this exception in a class of mine which uses a java.util.Timer. The thing is I'm not canceling the Timer. There's no...
Read more >
java.lang.IllegalStateException: Timer already cancelled
java.lang.IllegalStateException: Timer already cancelled. ... i am getting the above exception while sending a SNMP get request on hrSystemDate object . what are ......
Read more >
LI73560: JAVA EXCEPTION - ILLEGALSTATEEXCEPTION - IBM
Java Exception: java.lang.IllegalStateException: Timer already cancelled. at java.util.Timer.sched(Timer.java:354) at java.util.Timer.
Read more >
2195 ("Timer already cancelled" when trying to log in) - BASE
When trying to log in I get an stack trace: Timer already cancelled. java.lang.IllegalStateException ...at java.base/java.util.Timer.sched(Timer.java:398) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found