From 864076eb39236da3c2112741f3a36a300254c14c Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Mon, 6 Oct 2014 10:02:23 +0200 Subject: [PATCH 1/2] do not use the thread pool if it's shutdown should fix https://github.com/koush/AndroidAsync/issues/256 --- .../com/koushikdutta/async/AsyncServer.java | 67 ++++++++++--------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/AndroidAsync/src/com/koushikdutta/async/AsyncServer.java b/AndroidAsync/src/com/koushikdutta/async/AsyncServer.java index 450396425..b17a61f57 100644 --- a/AndroidAsync/src/com/koushikdutta/async/AsyncServer.java +++ b/AndroidAsync/src/com/koushikdutta/async/AsyncServer.java @@ -122,17 +122,18 @@ public void removeAllCallbacks(Object scheduled) { } private static void wakeup(final SelectorWrapper selector) { - synchronousWorkers.execute(new Runnable() { - @Override - public void run() { - try { - selector.wakeupOnce(); - } - catch (Exception e) { - Log.i(LOGTAG, "Selector Exception? L Preview?"); + if (!synchronousWorkers.isShutdown()) { + synchronousWorkers.execute(new Runnable() { + @Override + public void run() { + try { + selector.wakeupOnce(); + } catch (Exception e) { + Log.i(LOGTAG, "Selector Exception? L Preview?"); + } } - } - }); + }); + } } public Object postDelayed(Runnable runnable, long delay) { @@ -399,29 +400,31 @@ public Cancellable connectSocket(final String host, final int port, final Connec private static ExecutorService synchronousWorkers = Executors.newFixedThreadPool(4); public Future getAllByName(final String host) { final SimpleFuture ret = new SimpleFuture(); - synchronousWorkers.execute(new Runnable() { - @Override - public void run() { - try { - final InetAddress[] result = InetAddress.getAllByName(host); - if (result == null || result.length == 0) - throw new HostnameResolutionException("no addresses for host"); - post(new Runnable() { - @Override - public void run() { - ret.setComplete(null, result); - } - }); - } catch (final Exception e) { - post(new Runnable() { - @Override - public void run() { - ret.setComplete(e, null); - } - }); + if (!synchronousWorkers.isShutdown()) { + synchronousWorkers.execute(new Runnable() { + @Override + public void run() { + try { + final InetAddress[] result = InetAddress.getAllByName(host); + if (result == null || result.length == 0) + throw new HostnameResolutionException("no addresses for host"); + post(new Runnable() { + @Override + public void run() { + ret.setComplete(null, result); + } + }); + } catch (final Exception e) { + post(new Runnable() { + @Override + public void run() { + ret.setComplete(e, null); + } + }); + } } - } - }); + }); + } return ret; } From 9057de28156f7e43f11f6f36b9ac1d472c6e6b34 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 10 Oct 2014 08:54:48 +0200 Subject: [PATCH 2/2] log an error when the thread pool is dead and report it to the Future --- AndroidAsync/src/com/koushikdutta/async/AsyncServer.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/AndroidAsync/src/com/koushikdutta/async/AsyncServer.java b/AndroidAsync/src/com/koushikdutta/async/AsyncServer.java index b17a61f57..9a1b02a1d 100644 --- a/AndroidAsync/src/com/koushikdutta/async/AsyncServer.java +++ b/AndroidAsync/src/com/koushikdutta/async/AsyncServer.java @@ -31,6 +31,7 @@ import java.util.WeakHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.Semaphore; public class AsyncServer { @@ -133,6 +134,8 @@ public void run() { } } }); + } else { + Log.i(LOGTAG, "Executor shutdown"); } } @@ -424,6 +427,9 @@ public void run() { } } }); + } else { + Log.i(LOGTAG, "Executor shutdown"); + ret.setComplete(new RejectedExecutionException("Executor shutdown"), null); } return ret; }