Skip to content

Commit

Permalink
[FIXED JENKINS-22932]
Browse files Browse the repository at this point in the history
If the thread that serves NioChannelHub.run() leaves for any reason, stop accepting the new connection as the channel will never be serviced.
It is indicative of a problem in the code.

This is the 3rd and the final part of the fix to the problem.
  • Loading branch information
kohsuke committed Jun 9, 2014
1 parent 4228cf8 commit 23f8178
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/main/java/org/jenkinsci/remoting/nio/NioChannelHub.java
Expand Up @@ -66,6 +66,17 @@ public class NioChannelHub implements Runnable, Closeable {
*/
private ExecutorService commandProcessor;

/**
* Counts the # of select loops. Ocassionally useful for diagnosing whether the selector
* thread is spending too much CPU time.
*/
private long gen;

/**
* Sets to the thread that's in the {@link #run()} method.
*/
private volatile Thread selectorThread;


/**
* Bi-directional NIO channel used as the transport of a {@link Channel}.
Expand Down Expand Up @@ -431,6 +442,9 @@ protected CommandTransport makeTransport(InputStream is, OutputStream os, Mode m
if (r==null) r = factory.create(is);
if (w==null) w = factory.create(os);
if (r!=null && w!=null && mode==Mode.BINARY && cap.supportsChunking()) {
if (selectorThread==null)
throw new IOException("NioChannelHub is not currently running");

NioTransport t;
if (r==w) t = new MonoNioTransport(r,cap);
else t = new DualNioTransport(r,w,cap);
Expand Down Expand Up @@ -461,9 +475,8 @@ public void close() throws IOException {
* This method returns when {@link #close()} is called and the selector is shut down.
*/
public void run() {
final Thread thread = Thread.currentThread();
final String oldName = thread.getName();
long gen=0;
selectorThread = Thread.currentThread();
final String oldName = selectorThread.getName();

try {
while (true) {
Expand All @@ -479,7 +492,7 @@ public void run() {
}
}

thread.setName("NioChannelHub keys="+selector.keys().size()+" gen="+(gen++)+": "+oldName);
selectorThread.setName("NioChannelHub keys=" + selector.keys().size() + " gen=" + (gen++) + ": " + oldName);
selector.select();
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to select", e);
Expand Down Expand Up @@ -584,7 +597,8 @@ public void run() {
LOGGER.log(WARNING, "Unexpected shutdown of the selector thread", e);
throw e;
} finally {
thread.setName(oldName);
selectorThread.setName(oldName);
selectorThread = null;
}
}

Expand Down

0 comments on commit 23f8178

Please sign in to comment.