jayunit100
jayunit100

Reputation: 17640

Executor cannot be shutdown from the Thread acquired from itself

Hi guys : This question involves threads, clients, Netty, and Ning.

CONTEXT

The Ning library is an Asynchronous HTTP request tool which allows us to build requests, queue them, and handle them asynchronously. It relies on some libraries, such as the JBoss/Netty code pasted below.

PROBLEM

I recently ran across a Thread closing exception which was thrown by this bit of code in the JBoss/Netty ExecutorUtil class.

This class appears to be essentially a utility for ending threads in Netty.

This method was causing a bug in a web client (powered by Ning) that I was using, by the fact that I was attempting to close an HttpClient inside of a handler for that client's response.

Question

What is the significance of the deadlock this block of code is attempting to avoid ? For further details, the class can be seen at this code url.

// Check dead lock.
    final Executor currentParent = DeadLockProofWorker.PARENT.get();
    if (currentParent != null) {
        for (Executor e: executorsCopy) {
            if (e == currentParent) {
                throw new IllegalStateException(
                        "An Executor cannot be shut down from the thread " +
                        "acquired from itself.  Please make sure you are " +
                        "not calling releaseExternalResources() from an " +
                        "I/O worker thread.");
            }
        }
    }

Upvotes: 0

Views: 1833

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23567

In netty you are not allowed to shutdown the Executor from within an IO-Thread (as the exception is telling you already ;)), this is because you would have the risk of see a deadlock. If you really want to shutdown the Executor from within a handler you need to do it in a new thread. Something like for example:

public void messageReceived(...) {
    // in IO-Thread
    new Thread(new Runnable() {
        public void run() {
            //....shutdown here
        }
    }).start();
}

Upvotes: 2

Related Questions