koush / AndroidAsync

Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.
Other
7.52k stars 1.56k forks source link

RejectedExecutionException on an internal task scheduling #256

Open robUx4 opened 10 years ago

robUx4 commented 10 years ago

I get this error on 4.4.4 devices (non rooted). This is happening early in the lifetime of the process, within the first second the app is launched.

I noticed synchronousWorkers is not final, but I don't think it will make much of a difference.

java.util.concurrent.RejectedExecutionException: Task com.koushikdutta.async.AsyncServer$1@6531fc10 rejected from java.util.concurrent.ThreadPoolExecutor@650341c0[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
       at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2011)
       at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:793)
       at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1339)
       at com.koushikdutta.async.AsyncServer.wakeup(SourceFile:126)
       at com.koushikdutta.async.AsyncServer.postDelayed(SourceFile:160)
       at com.koushikdutta.async.AsyncServer.post(SourceFile:167)
       at com.koushikdutta.async.http.AsyncHttpClient.execute(SourceFile:185)
       at com.koushikdutta.async.http.AsyncHttpClient.execute(SourceFile:124)
       at com.koushikdutta.ion.loader.HttpLoader.load(SourceFile:24)
       at com.koushikdutta.ion.IonRequestBuilder.loadRequest(SourceFile:376)
       at com.koushikdutta.ion.IonRequestBuilder.invokeLoadRequest(SourceFile:370)
       at com.koushikdutta.ion.IonRequestBuilder$4.run(SourceFile:360)
       at com.koushikdutta.async.AsyncServer$RunnableWrapper.run(SourceFile:53)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5001)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
robUx4 commented 10 years ago

Looking at the ThreadPoolExecutor code it seems the "Terminated" string indicates the executor is dead. There is no call to shutdown() in your code, so the other option could be that the executor finalize() has been called (thus shutdown() being called). Maybe due to a premature death or the app being killed soon after a background startup in the background.

I think before calling execute() you should check the isShutdown() state of the executor. Silently doing nothing if they're dead.