eBay / parallec

Fast Parallel Async HTTP/SSH/TCP/UDP/Ping Client Java Library. Aggregate 100,000 APIs & send anywhere in 20 lines of code. Ping/HTTP Calls 8000 servers in 12 seconds. (Akka) www.parallec.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ParallelTaskBuilder.execute hangs forever waiting for a task completion in sync mode after resources released

kefasb opened this issue · comments

When ParallelClient.releaseExternalResources is called while ParallelTaskBuilder.execute is waiting for a task to complete is sync mode then ParallelTaskBiulder hangs forever in this while:

while (task != null && !task.isCompleted()) {

Even calling interrupt on executing thread (or shutdownNow on executor) does not have a result, because interrupts are swallowed.

Code snippet to reproduce:

    private static final Duration RELEASE_TIMEOUT = Duration.ofSeconds(3);
    private static final AtomicInteger PING_THREAD_ID_GEN = new AtomicInteger();

    public static void main(String[] args) {

        final ExecutorService pingExecutor = Executors.newSingleThreadExecutor(
            r -> new Thread(r, "PingThread-" + PING_THREAD_ID_GEN.incrementAndGet()));
        pingExecutor.submit(() -> ping());

        final ScheduledExecutorService releaseScheduledExecutor = Executors.newSingleThreadScheduledExecutor();
        releaseScheduledExecutor.schedule(() -> {
            new ParallelClient().releaseExternalResources();
            pingExecutor.shutdownNow();
            releaseScheduledExecutor.shutdownNow();
        }, RELEASE_TIMEOUT.getSeconds(), TimeUnit.SECONDS);
    }

    private static void ping() {
       new ParallelClient().preparePing()
                            .setPingNumRetries(0)
                            .setPingTimeoutMillis((int) RELEASE_TIMEOUT.plus(Duration.ofMillis(1000)).toMillis())
                            .setTargetHostsFromList(Collections.singletonList("1.2.3.4"))
                            .execute((result, responseContext) -> System.out.println(result));
    }

Result: PingThread-1 is sleeping forever.

Could you add !Thread.currentThread().isInterrupted() condition to the while (if this will not break anything else :) )? Or react on interruption in some way.

@kefasb thanks so much for trying parallec. Normally i do not need to run the releaseExternalResources if running as a server. what is the use case we need to release all the resources?